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

package com.bawie.recy1;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.bawie.recy1.adapter.NewsAdapter;
import com.bawie.recy1.bean.News;
import com.bawie.recy1.utils.OkHttpUtils;
import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.List;

/**
 * 1.导入依赖 初始化控件 加入网络权限
 * 2.封装网络请求OkHttpUtils 使用网络请求  封装bean
 * 3.布局管理器  Gson解析
 * 4.创建Adapter
 */
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

package com.bawie.recy1.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.bawie.recy1.R;
import com.bawie.recy1.base.BaseJilei;
import com.bawie.recy1.bean.News;
import com.bawie.recy1.type.TypeOne;
import com.bawie.recy1.type.TypeThree;
import com.bawie.recy1.type.TypeTwo;

import java.util.List;

/**
 * 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

package com.bawie.recy1.utils;

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

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * date:2018/12/1
 * author:别来无恙(别来无恙)
 * function:
 */
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.基类

package com.bawie.recy1.base;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.bawie.recy1.bean.News;

/**
 * date:2018/12/1
 * author:别来无恙(别来无恙)
 * function:
 * 1.抽象的方法
 * 2.继承RecyclerView.ViewHolder 并且实现他的方法
 * 3.手写一个抽象的没有返回值的方法
 *
 */
public abstract class BaseJilei extends RecyclerView.ViewHolder {

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

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

TypeOne

package com.bawie.recy1.type;

import android.support.annotation.NonNull;
import android.view.View;

import com.bawie.recy1.R;
import com.bawie.recy1.base.BaseJilei;
import com.bawie.recy1.bean.News;
import com.facebook.drawee.view.SimpleDraweeView;

/**
 * date:2018/12/1
 * author:别来无恙(别来无恙)
 * function:
 */
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

package com.bawie.recy1.type;

import android.support.annotation.NonNull;
import android.view.View;
import android.widget.TextView;

import com.bawie.recy1.R;
import com.bawie.recy1.base.BaseJilei;
import com.bawie.recy1.bean.News;
import com.facebook.drawee.view.SimpleDraweeView;

/**
 * date:2018/12/1
 * author:别来无恙(别来无恙)
 * function:
 */
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

package com.bawie.recy1.type;

import android.support.annotation.NonNull;
import android.view.View;
import android.widget.TextView;

import com.bawie.recy1.R;
import com.bawie.recy1.base.BaseJilei;
import com.bawie.recy1.bean.News;
import com.facebook.drawee.view.SimpleDraweeView;

/**
 * date:2018/12/1
 * author:别来无恙(别来无恙)
 * function:
 */
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"

发布了57 篇原创文章 · 获赞 12 · 访问量 8355
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览