属性动画,网络登录,RecyclerView多条目展示

在这里插入图片描述
加入网络权限

<uses-permission android:name="android.permission.INTERNET"/>

导入依赖

implementation 'com.squareup.okhttp3:okhttp:3.12.0'
implementation 'com.github.bumptech.glide:glide:3.8.0'
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

导入jar包
Gson.jar,ImageLoder.jar
搜索RecyclerView导入 implementation ‘com.android.support:recyclerview-v7:27.1.1’
1在Utils包中写HttpUtils

public class HttpUtils {
private static HttpUtils httpUtils = new HttpUtils();

private HttpUtils() {
}

public static HttpUtils getHttpUtils() {
    synchronized (httpUtils){
        if (httpUtils == null){
            httpUtils = new HttpUtils();
        }
    }
    return httpUtils;
}
public String get(String url){
    OkHttpClient client = new OkHttpClient();

    Request request = new Request.Builder()
            .url(url)
            .get()
            .build();
    try {
        Response response = client.newCall(request).execute();
        return response.body().string();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
}

2在core包中写接口BaseCore

public interface BaseCore<T> {
void loadSuccess(T data);
void loadError(Result result);
}

3.在app包中写MyApp

public class MyApp extends Application {
private static Context context;

@Override
public void onCreate() {
    super.onCreate();
    context=this;
}
public static Context getContext() {
    return context;
}
}

写Bean包里的
Result

public class Result<T> {
private String msg;
private String code;
private T data;

public String getMsg() {
    return msg;
}

public void setMsg(String msg) {
    this.msg = msg;
}

public String getCode() {
    return code;
}

public void setCode(String code) {
    this.code = code;
}

public T getData() {
    return data;
}

public void setData(T data) {
    this.data = data;
}
}

User
封装接口http://www.zhaoapi.cn/user/login?mobile=15210369901&

password=123456
 private Object age;
private String appkey;
private String appsecret;
private String createtime;
private Object email;
private Object fans;
private Object follow;
private Object gender;
private Object icon;
private Object latitude;
private Object longitude;
private String mobile;
private Object money;
private Object nickname;
private String password;
private Object praiseNum;
private String token;
private int uid;
private Object userId;
private String username;

NewsBean
http://www.xieast.com/api/news/news.php?page=1

private String uniquekey;
private String title;
private String date;
private String category;
private String author_name;
private String url;
private String thumbnail_pic_s;
private String thumbnail_pic_s02;
private String thumbnail_pic_s03;

在model里写BaseModel

public class BaseModel {
private static final String TAG = "BaseModel-------";
public static Result LoginData(String name, String pwd) {
    String url = "http://www.zhaoapi.cn/user/login?mobile="+name+"&password="+pwd;
    HttpUtils httpUtils = HttpUtils.getHttpUtils();
    String json = httpUtils.get(url);
    Log.i(TAG, "LoginData: "+json);
    Gson gson = new Gson();
    Type type = new TypeToken<Result<User>>() {}.getType();
    Result result = gson.fromJson(json, type);
    return result;
}

public static Result NewData(String page) {
    String url = "http://www.xieast.com/api/news/news.php?page="+page;
    HttpUtils httpUtils = HttpUtils.getHttpUtils();
    String json = httpUtils.get(url);
    Log.i(TAG, "LoginData: "+json);
    Gson gson = new Gson();
    Type type = new TypeToken<Result<List<NewsBean>>>() {}.getType();
    Result result = gson.fromJson(json, type);
    return result;
}
}

在persenter写
BasePresenter

public abstract class BasePresenter {
private BaseCore baseCore;

public BasePresenter(BaseCore baseCore) {
    this.baseCore = baseCore;
}
public void unBaseCore(){
    baseCore = null;
}
public void readData(String... strings){
    new MyTask().execute(strings);
}
class MyTask extends AsyncTask<String,Void,Result> {

    @Override
    protected Result doInBackground(String... strings) {
        Result result = loadData(strings);
        return result;
    }

    @Override
    protected void onPostExecute(Result result) {
        super.onPostExecute(result);
        if (result.getCode().equals("0")){
            baseCore.loadSuccess(result.getData());
        }else {
            baseCore.loadError(result);
        }
    }
} public void readNewData(String... strings){
    new MyNewTask().execute(strings);
}
class MyNewTask extends AsyncTask<String,Void,Result>{

    @Override
    protected Result doInBackground(String... strings) {
        Result result = loadData(strings);
        return result;
    }

    @Override
    protected void onPostExecute(Result result) {
        super.onPostExecute(result);
        if (result.getCode().equals("1")){
            baseCore.loadSuccess(result.getData());
        }else {
            baseCore.loadError(result);
        }
    }
}

public abstract Result loadData(String... strings);

}

LoginPresenter

public class LoginPresenter extends BasePresenter {
public LoginPresenter(BaseCore baseCore) {
    super(baseCore);
}

@Override
public Result loadData(String... strings) {
    Result result = BaseModel.LoginData(strings[0], strings[1]);
    return result;
}


}

NewPresenter

public class NewPresenter extends BasePresenter {
public NewPresenter(BaseCore baseCore) {
    super(baseCore);
}

@Override
public Result loadData(String... strings) {
    Result result = BaseModel.NewData(strings[0]);
    return result;
}
}

注册界面布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:gravity="center"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MaActivity">
<EditText
    android:id="@+id/edit_name"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:hint="用户名"
    />
<EditText
    android:id="@+id/edit_pwd"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:hint="密码"
    />
<Button
    android:id="@+id/but_d"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="登录"/>
</LinearLayout>

注册界面

public class MaActivity extends AppCompatActivity implements BaseCore {

@BindView(R.id.edit_name)
EditText editName;
@BindView(R.id.edit_pwd)
EditText editPwd;
@BindView(R.id.but_d)
Button butD;
private LoginPresenter presenter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ma);
    ButterKnife.bind(this);
    presenter = new LoginPresenter(this);
}

@OnClick(R.id.but_d)
public void onViewClicked() {
    String name = editName.getText().toString();
    String pwd = editPwd.getText().toString();
    presenter.readData(name,pwd);
}
@Override
public void loadSuccess(Object data) {
    User user = (User) data;
    Intent intent = new Intent();
    intent.putExtra("name",user.getMobile());
    setResult(110,intent);
    finish();
}

@Override
public void loadError(Result result) {
    Toast.makeText(this, result.getMsg(), Toast.LENGTH_SHORT).show();
}
@Override
protected void onDestroy() {
    super.onDestroy();
    presenter.unBaseCore();
}
}

主页面布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:background="#f00"
    android:gravity="center_vertical">

    <ImageView
        android:id="@+id/image_icon"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/text_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginStart="100dp"
        android:layout_marginLeft="100dp"
        android:text="昵称"
        android:textSize="20sp" />
    <Button
        android:id="@+id/but_deng"
        android:layout_width="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_height="wrap_content"
        android:text="登录"/>
</RelativeLayout>
<com.handmark.pulltorefresh.library.PullToRefreshScrollView
    android:id="@+id/pull"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycle"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </LinearLayout>
</com.handmark.pulltorefresh.library.PullToRefreshScrollView>
</LinearLayout>

多条目适配器
NormalAdapter

public class NormalAdapter extends RecyclerView.Adapter<NormalAdapter.VH> {
private ClickListener clickListener;
private LongClickListener longClickListener;

//② 创建ViewHolder
public static class VH extends RecyclerView.ViewHolder{
    public final TextView title;
    public final TextView title_1;
    public final ImageView imageView;
    public final ImageView imageView_1;
    public final ImageView imageView_2;
    public final ImageView imageView_3;
    public VH(View v) {
        super(v);
        title = (TextView) v.findViewById(R.id.text_title);
        title_1 = (TextView) v.findViewById(R.id.text_title_1);
        imageView = (ImageView) v.findViewById(R.id.image_view);
        imageView_1 = (ImageView) v.findViewById(R.id.image_view_1_1);
        imageView_2 = (ImageView) v.findViewById(R.id.image_view_1_2);
        imageView_3 = (ImageView) v.findViewById(R.id.image_view_1_3);
    }
}

private List<NewsBean> mDatas;

public List<NewsBean> getmDatas() {
    return mDatas;
}

public void setmDatas(List<NewsBean> mDatas) {
    this.mDatas = mDatas;
}
public NewsBean getBean(int position){
    NewsBean newBean = mDatas.get(position);
    return newBean;
}

@NonNull

//③ 在Adapter中实现3个方法
@Override
public void onBindViewHolder(VH holder, final int position) {
    /*holder.title.setText(mDatas.get(position).getTitle());
    Glide.with(MyApp.getContext()).load(mDatas.get(position).getUrl()).into(holder.imageView);
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //item 点击事件
        }
    });*/
    switch (holder.getItemViewType()){
        case 0:
            holder.title.setText(mDatas.get(position).getTitle());
            Glide.with(MyApp.getContext()).load(mDatas.get(position).getThumbnail_pic_s()).into(holder.imageView);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //item 点击事件
                    clickListener.onItmeClickListener(v,position);
                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    longClickListener.onLongItmeClickListener(v,position);
                    return true;
                }
            });
            break;
        case 1:
            holder.title_1.setText(mDatas.get(position).getTitle());
            Glide.with(MyApp.getContext()).load(mDatas.get(position).getThumbnail_pic_s()).into(holder.imageView_1);
            Glide.with(MyApp.getContext()).load(mDatas.get(position).getThumbnail_pic_s02()).into(holder.imageView_2);
            Glide.with(MyApp.getContext()).load(mDatas.get(position).getThumbnail_pic_s03()).into(holder.imageView_3);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //item 点击事件
                    clickListener.onItmeClickListener(v,position);


                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    longClickListener.onLongItmeClickListener(v,position);
                    return true;
                }
            });
            break;
    }
}

@Override
public int getItemCount() {
    return mDatas.size();
}

@Override
public VH onCreateViewHolder(ViewGroup parent, int viewType) {
    //LayoutInflater.from指定写法
    if (viewType==0){
        View o = LayoutInflater.from(parent.getContext()).inflate(R.layout.itme_layout, parent, false);
        return new VH(o);
    }else if(viewType == 1){
        View t = LayoutInflater.from(parent.getContext()).inflate(R.layout.itme_layout1, parent, false);
        return new VH(t);
    }

    return null;
}

@Override
public int getItemViewType(int position) {

    String thumbnail_pic_s = mDatas.get(position).getThumbnail_pic_s();
    String thumbnail_pic_s02 = mDatas.get(position).getThumbnail_pic_s02();
    String thumbnail_pic_s03 = mDatas.get(position).getThumbnail_pic_s03();
    if (TextUtils.isEmpty(thumbnail_pic_s02)&& TextUtils.isEmpty(thumbnail_pic_s03)){
        return 0;
    }else {
        return 1;
    }

}
public interface ClickListener{
    void onItmeClickListener(View view,int position);
}
public void setOnItmeClickListener(ClickListener clickListener){
    this.clickListener = clickListener;
}
public interface LongClickListener{
    void onLongItmeClickListener(View view,int position);
}
public void setOnLongItmeClickListener(LongClickListener longClickListener){
    this.longClickListener = longClickListener;
}
}

itme_layoutn布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:gravity="center_vertical"
android:layout_height="wrap_content">
<ImageView
    android:id="@+id/image_view"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:src="@mipmap/ic_launcher"/>
<TextView
    android:id="@+id/text_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="标题"/>
</LinearLayout>

itme_layout1布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">

<TextView
    android:id="@+id/text_title_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="标题"/>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/image_view_1_1"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="80dp"
        android:scaleType="fitXY"
        android:src="@mipmap/ic_launcher"/>
    <ImageView
        android:id="@+id/image_view_1_2"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="80dp"
        android:scaleType="fitXY"
        android:src="@mipmap/ic_launcher"/>
    <ImageView
        android:id="@+id/image_view_1_3"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="80dp"
        android:scaleType="fitXY"
        android:src="@mipmap/ic_launcher"/>
</LinearLayout>
</LinearLayout>

最后主界面MainActivity

public class MainActivity extends AppCompatActivity implements BaseCore {

@BindView(R.id.image_icon)
ImageView imageIcon;
@BindView(R.id.text_name)
TextView textName;
@BindView(R.id.but_deng)
Button butDeng;
@BindView(R.id.recycle)
android.support.v7.widget.RecyclerView recycle;
@BindView(R.id.pull)
PullToRefreshScrollView pull;

private int page = 1;
private int type = 0;
private NewPresenter presenter;
private List<NewsBean> lists = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);
    presenter = new NewPresenter(this);
    presenter.readNewData(page + "");
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    recycle.setLayoutManager(linearLayoutManager);
    pull.setScrollingWhileRefreshingEnabled(true);
    pull.setMode(PullToRefreshBase.Mode.BOTH);
    pull.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
        @Override
        public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
            page = 1;
            type = 0;
            presenter.readNewData(page + "");
            pull.onRefreshComplete();
        }

        @Override
        public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
            page++;
            type = 1;
            presenter.readNewData(page + "");
            pull.onRefreshComplete();
        }
    });
}

@OnClick({R.id.image_icon, R.id.but_deng})
public void onViewClicked(View view) {
    switch (view.getId()) {
        case R.id.image_icon:
            ObjectAnimator scaleX = ObjectAnimator.ofFloat(imageIcon, "scaleX", 1, 2, 1);
            ObjectAnimator scaleY = ObjectAnimator.ofFloat(imageIcon, "scaleY", 1, 2, 1);
            AnimatorSet animatorSet = new AnimatorSet();
            animatorSet.playTogether(scaleX, scaleY);
            animatorSet.setDuration(2000);
            animatorSet.setInterpolator(new LinearInterpolator());
            animatorSet.start();
            break;

        case R.id.but_deng:
            Intent intent = new Intent(MainActivity.this, MaActivity.class);
            startActivityForResult(intent, 100);
            break;
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 100 && resultCode == 110) {
        String name = data.getStringExtra("name");
        textName.setText(name);
    }
}

@Override
public void loadSuccess(Object data) {
    final List<NewsBean> list = (List<NewsBean>) data;
    // Toast.makeText(this, list.toString(), Toast.LENGTH_SHORT).show();

    final NormalAdapter normalAdapter = new NormalAdapter();
    normalAdapter.setOnItmeClickListener(new NormalAdapter.ClickListener() {
        @Override
        public void onItmeClickListener(View view, int position) {
            //Toast.makeText(MainActivity.this, position+"", Toast.LENGTH_SHORT).show();
            /*list.remove(position);
            normalAdapter.setmDatas(list);
            recycle.setAdapter(normalAdapter);*/
            ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view, "alpha", 1f, 0, 1f);
            objectAnimator.setDuration(3000);
            objectAnimator.setInterpolator(new LinearInterpolator());
            objectAnimator.start();
        }
    });
    normalAdapter.setOnLongItmeClickListener(new NormalAdapter.LongClickListener() {
        @Override
        public void onLongItmeClickListener(View view, final int position) {
            Toast.makeText(MainActivity.this, position + "", Toast.LENGTH_SHORT).show();
            NewsBean bean = normalAdapter.getBean(position);
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("删除" + bean.getTitle());
            builder.setMessage("确认删除");
            builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    lists.remove(position);
                    /*normalAdapter.setmDatas(lists);
                    recycle.setAdapter(normalAdapter);*/
                    normalAdapter.notifyDataSetChanged();
                }
            });
            builder.setNegativeButton("取消", null);
            builder.show();
        }
    });
    switch (type){
        case 0:
            lists = new ArrayList<>();
            lists.addAll(list);
            normalAdapter.setmDatas(lists);
            recycle.setAdapter(normalAdapter);
            break;
        case 1:
            lists.addAll(list);
            normalAdapter.setmDatas(lists);
            recycle.setAdapter(normalAdapter);
            break;
    }

}



@Override
public void loadError(Result result) {
    Toast.makeText(this, result.getMsg(), Toast.LENGTH_SHORT).show();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    presenter.unBaseCore();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值