RecyclerView实现多条目展示

敲我们的多条目呢,首先做好准备工作,比如:导入我们的网络工具类
https://blog.csdn.net/mingxiangzhimin/article/details/85112424
再声明一下,本节里面的上传头像和登录的逻辑没有实现。。。。。。
好啦,开始啦

Bean类

package com.qh.***.recyclerviewmore.bean;

import java.util.List;

public class Root {

    private String msg;

    private int code;

    private List data ;

    public void setMsg(String msg){
        this.msg = msg;
    }
    public String getMsg(){
        return this.msg;
    }
    public void setCode(int code){
        this.code = code;
    }
    public int getCode(){
        return this.code;
    }
    public void setData(List data){
        this.data = data;
    }
    public List getData(){
        return this.data;
    }


}

package com.qh.***.recyclerviewmore.bean;

public class Data  {
    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;

    public void setUniquekey(String uniquekey){
        this.uniquekey = uniquekey;
    }
    public String getUniquekey(){
        return this.uniquekey;
    }
    public void setTitle(String title){
        this.title = title;
    }
    public String getTitle(){
        return this.title;
    }
    public void setDate(String date){
        this.date = date;
    }
    public String getDate(){
        return this.date;
    }
    public void setCategory(String category){
        this.category = category;
    }
    public String getCategory(){
        return this.category;
    }
    public void setAuthor_name(String author_name){
        this.author_name = author_name;
    }
    public String getAuthor_name(){
        return this.author_name;
    }
    public void setUrl(String url){
        this.url = url;
    }
    public String getUrl(){
        return this.url;
    }
    public void setThumbnail_pic_s(String thumbnail_pic_s){
        this.thumbnail_pic_s = thumbnail_pic_s;
    }
    public String getThumbnail_pic_s(){
        return this.thumbnail_pic_s;
    }
    public void setThumbnail_pic_s02(String thumbnail_pic_s02){
        this.thumbnail_pic_s02 = thumbnail_pic_s02;
    }
    public String getThumbnail_pic_s02(){
        return this.thumbnail_pic_s02;
    }
    public void setThumbnail_pic_s03(String thumbnail_pic_s03){
        this.thumbnail_pic_s03 = thumbnail_pic_s03;
    }
    public String getThumbnail_pic_s03(){
        return this.thumbnail_pic_s03;
    }



}

开始MVP

  1. View
package com.qh.***.recyclerviewmore.Data;

import com.qh.***.recyclerviewmore.bean.Data;

import java.util.List;

public interface DataView {

    void onDataSucess(List entityList);

    void onFailer(Exception e);



}

  1. Model
package com.qh.***.recyclerviewmore.Data;

import com.google.gson.Gson;
import com.qh.***.recyclerviewmore.bean.Root;
import com.qh.***.recyclerviewmore.http.Utils;

public class DataModel {

    public static Root getListData() {
        String listShowData = Utils.get("http://www.xieast.com/api/news/news.php?page=1");
        Gson gson = new Gson();
        Root root = gson.fromJson(listShowData, Root.class);
        return root;
    }


}

  1. Presenter
package com.qh.***.recyclerviewmore.Data;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import com.qh.***.recyclerviewmore.bean.Root;

public class DataPresenter {

    private DataView dataView;

    public DataPresenter(DataView dataView) {
        this.dataView = dataView;
    }


    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            Root root = (Root) msg.obj;
            dataView.onDataSucess(root.getData());
        }
    };


    public void getData() {
        new Thread() {
            @Override
            public void run() {
                Root listData = DataModel.getListData();
                Message mMessage = mHandler.obtainMessage();
                mMessage.obj = listData;
                mHandler.sendMessage(mMessage);
            }
        }.start();
    }


}

适配器

package com.qh.***.recyclerviewmore.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.qh.***.recyclerviewmore.R;
import com.qh.***.recyclerviewmore.bean.Data;

import java.util.List;

public class DataAdapter extends XRecyclerView.Adapter {


    //设置二种类型,对应我二种不同类型的Item,数字随意生成,主要是为了区分
    private static final int FristType = 0;
    private static final int TwoType = 1;

    private Context context;
    private List list;

    public DataAdapter(Context context, List list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public com.jcodecraeer.xrecyclerview.XRecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == FristType) {
            View view = LayoutInflater.from(context).inflate(R.layout.news_item2_layout, parent, false);
            return new Type1ViewHolder(view);
        } else {
            View view2 = LayoutInflater.from(context).inflate(R.layout.news_item_layout, parent, false);
            return new Type2ViewHolder(view2);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull final com.jcodecraeer.xrecyclerview.XRecyclerView.ViewHolder holder, final int position) {

        if (list.get(position).getThumbnail_pic_s02() != null && list.get(position).getThumbnail_pic_s03() != null) {
            Glide.with(context).load(list.get(position).getThumbnail_pic_s()).into(((Type2ViewHolder) holder).iv1);
            Glide.with(context).load(list.get(position).getThumbnail_pic_s02()).into(((Type2ViewHolder) holder).iv2);
            Glide.with(context).load(list.get(position).getThumbnail_pic_s03()).into(((Type2ViewHolder) holder).iv3);
            ((Type2ViewHolder) holder).title.setText(list.get(position).getTitle());
        } else {
            Glide.with(context).load(list.get(position).getThumbnail_pic_s()).into(((Type1ViewHolder) holder).image);
            ((Type1ViewHolder) holder).title.setText(list.get(position).getTitle());
        }


    }

    @Override
    public int getItemViewType(int position) {
        if (list.get(position).getThumbnail_pic_s02() != null && list.get(position).getThumbnail_pic_s03() != null) {
            return 1;
        }
        return 0;
    }


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


    //俩个内部类
    class Type1ViewHolder extends com.jcodecraeer.xrecyclerview.XRecyclerView.ViewHolder {
        private TextView title;
        private ImageView image;

        public Type1ViewHolder(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title1);
            image = itemView.findViewById(R.id.image1);
        }
    }

    class Type2ViewHolder extends com.jcodecraeer.xrecyclerview.XRecyclerView.ViewHolder {
        private TextView title;
        private ImageView iv1, iv2, iv3;

        public Type2ViewHolder(View itemView) {
            super(itemView);
            iv1 = itemView.findViewById(R.id.img1);
            iv2 = itemView.findViewById(R.id.img2);
            iv3 = itemView.findViewById(R.id.img3);
            title = itemView.findViewById(R.id.title3);
        }
    }


}

两个布局文件

  1. 布局



    

    

        

        

        
    



  1. 布局



    

    

        
    



我们的Activity

package com.qh.***.recyclerviewmore;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.qh.***.recyclerviewmore.Data.DataPresenter;
import com.qh.***.recyclerviewmore.Data.DataView;
import com.qh.***.recyclerviewmore.adapter.DataAdapter;
import com.qh.***.recyclerviewmore.bean.Data;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, DataView {

    private ImageView mMainHead;
    /**
     * 昵称
     */
    private TextView mMianName;
    /**
     * 请登录
     */
    private Button mBtnLogin;
    private XRecyclerView mXRecycleView;
    private DataPresenter mDataPresenter;
    private List mListData;
    private DataAdapter dataAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        mListData = new ArrayList<>();


        //实例化p层
        mDataPresenter = new DataPresenter(this);
        mDataPresenter.getData();

        //适配器
        dataAdapter = new DataAdapter(getBaseContext(), mListData);
        mXRecycleView.setAdapter(dataAdapter);

        //线性布局管理器
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(OrientationHelper.VERTICAL);
        mXRecycleView.setLayoutManager(layoutManager);

    }

    private void initView() {
        mMainHead = findViewById(R.id.main_head);
        mMianName = findViewById(R.id.mian_name);
        mBtnLogin = findViewById(R.id.btn_login);
        mBtnLogin.setOnClickListener(this);
        mXRecycleView = findViewById(R.id.x_recycle_view);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                break;
        }
    }


    //DataView 接口实现的方法
    @Override
    public void onDataSucess(List entityList) {
        if (entityList != null) {
            mListData.clear();
            mListData.addAll(entityList);
            dataAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onFailer(Exception e) {
		//可以给个吐司
    }
}

我们的主布局




    

        

        

        

好啦,最后呢,不要忘了加网络权限

网络权限

本节所用到的依赖
	//    xrecyclerview
    implementation 'com.jcodecraeer:xrecyclerview:1.2.0'
    //    okhttp
    implementation 'com.squareup.okhttp3:okhttp:3.11.0'
	//    Gson
    implementation 'com.google.code.gson:gson:2.8.1'
	//     Glide
    implementation 'com.github.bumptech.glide:glide:4.8.0'

你可能感兴趣的:(android)