XRecyclerView多条目展示以及上拉刷新下拉加载

在这里插入图片描述

1.主页面布局Main

<com.jcodecraeer.xrecyclerview.XRecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/recyclerView"></com.jcodecraeer.xrecyclerview.XRecyclerView>

2.主页面代码MainActivity

public class MainActivity extends AppCompatActivity {

    public static final String url = "http://www.xieast.com/api/news/news.php";
    private XRecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //找控件
        initView();
        //
        initData();
        //上拉加载下拉刷新
        initRefresh();
    }

    /**
     * 刷新
     *  //上拉加载下拉刷新
     */
    private void initRefresh() {
        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                //更新UI
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        recyclerView.refreshComplete();
                        Toast.makeText(MainActivity.this, "刷新完成", Toast.LENGTH_SHORT).show();
                    }
                },2000);
            }

            @Override
            public void onLoadMore() {
                //更新UI
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        recyclerView.loadMoreComplete();
                        Toast.makeText(MainActivity.this, "加载完成", Toast.LENGTH_SHORT).show();
                    }
                },2000);
            }
        });
    }

    private void initData() {
        OkHttpUtils instance = OkHttpUtils.getInstance();
        instance.doGet(url, new OkHttpUtils.OkHttpUtilsInterface() {
            @Override
            public void success(String json) {
                //创建布局管理器
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity.this);
                recyclerView.setLayoutManager(linearLayoutManager);
                //Gson解析
                News news = new Gson().fromJson(json, News.class);
                List<News.DataBean> data = news.getData();
                //创建Adapter
                NewsAdapter newsAdapter = new NewsAdapter(data, MainActivity.this);
                recyclerView.setAdapter(newsAdapter);

            }

            @Override
            public void failed() {

            }
        });

    }

    private void initView() {
        recyclerView = (XRecyclerView) findViewById(R.id.recyclerView);
    }
}

3.Adapter

/**
 * date:2018/12/1
 * author:别来无恙(别来无恙)
 * function:
 * 1.写入上下文 以及list集合 写入有参构造
 * 2.实现他的方法
 * 3.写一个基类
 * 4.在onBindViewHolder把viewHolder转换成BaseJilei的类型,获取他手写的方法传入list集合
 * 5.创建一个getItemViewType方法 判断图片
 * 6.在onCreateViewHolder 判断下标是否和对应的TYPE相同
 *  a.创建新的对应的布局 继承基类 实现他的两个方法  写入他们的布局
 *  b.Fresco的初始化
 *
 */
public class NewsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<News.DataBean> list;
    private Context mContext;
    public static final int TYPE_ONE = 1;
    public static final int TYPE_TWO = 2;
    public static final int TYPE_THREE = 3;



    public NewsAdapter(List<News.DataBean> list, Context context) {
        this.list = list;
        mContext = context;
    }

    @Override
    public int getItemViewType(int position) {
        News.DataBean dataBean = list.get(position);
        if (dataBean.getThumbnail_pic_s() != null && dataBean.getThumbnail_pic_s02() == null && dataBean.getThumbnail_pic_s03() == null){
            //如果存在一张图片 返回1
            return TYPE_ONE;
        }else if (dataBean.getThumbnail_pic_s() != null && dataBean.getThumbnail_pic_s02() != null && dataBean.getThumbnail_pic_s03() != null){
            //如果存在三张图片 返回3
            return TYPE_THREE;
        }else {
            //最后一个结果是 只存在两张图片 返回2
            return TYPE_TWO;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if (i == TYPE_ONE){
            //布局1张图片
            return new TypeOne(View.inflate(mContext,R.layout.item_news1,null));
        }else if (i == TYPE_TWO){
            //布局2张图片
            return new TypeTwo(View.inflate(mContext,R.layout.item_news2,null));
        }else{
            //布局3张图片
            return new TypeThree(View.inflate(mContext,R.layout.item_news3,null));
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        ((BaseJilei)viewHolder).JileiAbstract(list.get(i));
    }


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

4.工具类Utils

public class OkHttpUtils {

    private final Handler mHandler;
    private final OkHttpClient mOkHttpClient;
    private static OkHttpUtils sOkHttpUtils;

    private OkHttpUtils(){
        mHandler = new Handler(Looper.getMainLooper());
        mOkHttpClient = new OkHttpClient.Builder()
                .writeTimeout(3000, TimeUnit.MILLISECONDS)
                .readTimeout(3000, TimeUnit.MILLISECONDS)
                .connectTimeout(3000, TimeUnit.MILLISECONDS)
                .build();
    }
    public static OkHttpUtils getInstance(){
        if (sOkHttpUtils == null){
            synchronized (OkHttpUtils.class){
                if (sOkHttpUtils == null){
                    return sOkHttpUtils = new OkHttpUtils();
                }
            }
        }
        return sOkHttpUtils;
    }
    public interface OkHttpUtilsInterface{
        void success(String json);
        void failed();
    }
    public void doGet(String url, final OkHttpUtilsInterface okHttpUtilsInterface){
        Request request = new Request.Builder()
                .url(url)
                .build();
        Call call = mOkHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        okHttpUtilsInterface.failed();
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String json = response.body().string();
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        okHttpUtilsInterface.success(json);
                    }
                });
            }
        });
    }


}

5.基类

public abstract class BaseJilei extends RecyclerView.ViewHolder {

    public abstract void JileiAbstract(News.DataBean list);
    public BaseJilei(@NonNull View itemView) {
        super(itemView);
    }
}

6.基类的三个孩子----

TypeOne

public class TypeOne extends BaseJilei {
    private final SimpleDraweeView mItem_image_one;
    public TypeOne(@NonNull View itemView) {
        super(itemView);
        //找控件
        mItem_image_one = itemView.findViewById(R.id.item_image_one);

    }

    @Override
    public void JileiAbstract(News.DataBean list) {
        mItem_image_one.setImageURI(list.getThumbnail_pic_s());
    }
}

TypeTwo

public class TypeTwo extends BaseJilei {

    private final SimpleDraweeView mItem_image_one;
    private final SimpleDraweeView mItem_image_two;
    private final TextView mItem_textview_title;
    private final TextView mItem_textview_data;
    public TypeTwo(@NonNull View itemView) {
        super(itemView);
        //找控件
        mItem_image_one = itemView.findViewById(R.id.item_image_one);
        mItem_image_two = itemView.findViewById(R.id.item_image_two);
        mItem_textview_title = itemView.findViewById(R.id.item_textview_title);
        mItem_textview_data = itemView.findViewById(R.id.item_textview_data);
    }

    @Override
    public void JileiAbstract(News.DataBean list) {
        mItem_image_one.setImageURI(list.getThumbnail_pic_s());
        mItem_image_two.setImageURI(list.getThumbnail_pic_s02());
        mItem_textview_title.setText(list.getTitle());
        mItem_textview_data.setText(list.getDate());
    }
}

TypeThree

public class TypeThree extends BaseJilei {

    private final SimpleDraweeView mItem_image_one;
    private final SimpleDraweeView mItem_image_two;
    private final SimpleDraweeView mItem_image_three;
    private final TextView mItem_textview_title;
    private final TextView mItem_textview_data;

    public TypeThree(@NonNull View itemView) {
        super(itemView);
        //找控件
        mItem_image_one = itemView.findViewById(R.id.item_image_one);
        mItem_image_two = itemView.findViewById(R.id.item_image_two);
        mItem_image_three = itemView.findViewById(R.id.item_image_three);
        mItem_textview_title = itemView.findViewById(R.id.item_textview_title);
        mItem_textview_data = itemView.findViewById(R.id.item_textview_data);
    }

    @Override
    public void JileiAbstract(News.DataBean list) {
        mItem_image_one.setImageURI(list.getThumbnail_pic_s());
        mItem_image_two.setImageURI(list.getThumbnail_pic_s02());
        mItem_image_three.setImageURI(list.getThumbnail_pic_s03());
        mItem_textview_title.setText(list.getTitle());
        mItem_textview_data.setText(list.getDate());
    }
}

7.全局初始化

public void onCreate() {
        super.onCreate();
        Fresco.initialize(this);
    }

8.三个条目的布局

news1

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


        <com.facebook.drawee.view.SimpleDraweeView
            android:id="@+id/item_image_one"
            android:layout_width="400dp"
            android:layout_height="100dp"
            app:placeholderImage="@mipmap/ic_launcher"/>

</LinearLayout>

news2

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/item_textview_title"
        android:text="AAAAAAAAA"
        android:textSize="22sp"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <com.facebook.drawee.view.SimpleDraweeView
            android:id="@+id/item_image_one"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_weight="1"
            app:placeholderImage="@mipmap/ic_launcher"/>
        <com.facebook.drawee.view.SimpleDraweeView
            android:id="@+id/item_image_two"
            android:layout_width="100dp"
            android:layout_weight="1"
            android:layout_height="100dp"
            app:placeholderImage="@mipmap/ic_launcher"/>
    </LinearLayout>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/item_textview_data"
        android:text="AAAAAAAAA"
        android:textSize="20sp"/>
</LinearLayout>

news3

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/item_textview_title"
        android:text="AAAAAAAAA"
        android:textSize="22sp"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <com.facebook.drawee.view.SimpleDraweeView
            android:id="@+id/item_image_one"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_weight="1"
            app:placeholderImage="@mipmap/ic_launcher"/>
        <com.facebook.drawee.view.SimpleDraweeView
            android:id="@+id/item_image_two"
            android:layout_width="100dp"
            android:layout_weight="1"
            android:layout_height="100dp"
            app:placeholderImage="@mipmap/ic_launcher"/>
        <com.facebook.drawee.view.SimpleDraweeView
            android:id="@+id/item_image_three"
            android:layout_width="100dp"
            android:layout_weight="1"
            android:layout_height="100dp"
            app:placeholderImage="@mipmap/ic_launcher"/>
    </LinearLayout>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/item_textview_data"
        android:text="AAAAAAAAA"
        android:textSize="20sp"/>
</LinearLayout>
注意:
    /*okHttp*/
    implementation 'com.squareup.okhttp3:okhttp:3.12.0'
    /*recyclerview*/
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    /*gson解析*/
    implementation 'com.google.code.gson:gson:2.2.4'
    /*fresco*/
    implementation 'com.facebook.fresco:fresco:1.11.0'
    /*xrecyclerview*/
    implementation 'com.jcodecraeer:xrecyclerview:1.2.0'

加入网络权限
<uses-permission android:name="android.permission.INTERNET"/>
注册Application
android:name=".application.MyApplication"

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值