xRecyClerView列表展示多条目,下拉刷新,上拉加载,长按删除

例:


A.使用OkHttp做网络请求,使用单例模式封装OkHttp,包括Get请求和Post请求,添加日志拦截器,自定义回调接口并回调到主线程

B.使用MVP框架搭建,分包明确,V层和M层解耦,通过接口完成V层和P层以及P层和M层通信,解决内存泄漏问题

C.使用xRecyClerView做列表展示页面,并实现下拉刷新,上拉加载更多的功能

D.根据条目数据中的序号来实现多条目加载,每两条显示3张图片,第三条显示1张图片,依次排列,一张图片时显示在条目左侧,两条或三条时显示在条目下方,图片水平排列

E.自行选择图片加载框架完成图片加载

F.使用SQLite对新闻条目进行缓存,进入页面时,先从数据库进行读取数据展示,当有网时,从网络加载数据并更新列表数据,没有网络时,弹出吐司提示。

G.长按条目,弹出确认删除对话框,点击确认按钮时移除所选条目,添加默认的删除动画,并局部刷新

H.当确认删除后,将缓存到数据库的条目状态标记为已删除,下次从数据库读取数据时,被标记为已删除的条目不再进行展示,并且从网络请求数据时,如果加载的数据已经被删除过,则不展示该已经删除过的数据



加依赖:

dependencies {
    implementation 'com.github.bumptech.glide:glide:4.7.1'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
    implementation 'com.jcodecraeer:xrecyclerview:1.5.9'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0'
    implementation 'com.squareup.okhttp3:okhttp:3.10.0'
    implementation 'com.google.code.gson:gson:2.8.5'
}
configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion '27.1.1'
            }
        }
    }
}

加权限:

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



主方法类:

public class MainActivity extends AppCompatActivity implements INews {
    private XRecyclerView rv;
    private NewsPresenter presenter;
    private int page = 5010;
    private List<News.Data> data;
    private boolean isRefresh = true;//判断是下啦刷新还是上啦加载
    private NewsAdapter newsAdapter;
    private DbHelper dbHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }
    private void initData() {
        if (getIntent().getExtras() != null) {
            page = 5010 + Integer.parseInt(getIntent().getExtras().getString("id"));
        }
        dbHelper = new DbHelper(this);
        presenter = new NewsPresenter(this);
        data = new ArrayList<>();
        request();
    }
    public void request() {
        if (NetWorkUtil.hasWifiConnection(this) || NetWorkUtil.hasGPRSConnection(this)) {
            Map<String, String> p = new HashMap<>();
            p.put("type", page + "");
            presenter.getData(Constants.GET_URL, p);
        } else {
            Toast.makeText(this, "网络不通畅,请稍后再试!", Toast.LENGTH_SHORT).show();
            String json = null;
//
            //获取数据库对象,可读
            SQLiteDatabase db = dbHelper.getReadableDatabase();
            //获取数据库的游标
            Cursor cursor = db.rawQuery("select * from news", null);
            while (cursor.moveToNext()){
                json = cursor.getString(cursor.getColumnIndex("json"));
            }
            //本地列表刷新
            fillLocalData(json);
        }
    }

    /**
     * 本地列表刷新
     * @param json
     */
    private void fillLocalData(String json) {
        News news = new Gson().fromJson(json,News.class);
        newsAdapter = new NewsAdapter(news.data, this,news);
        rv.setAdapter(newsAdapter);
    }

    private void initView() {
        rv = findViewById(R.id.rv);
        //设置局部刷新动画
        rv.setItemAnimator(new DefaultItemAnimator());
        rv.setPullRefreshEnabled(true);//刷新配置
        rv.setLoadingMoreEnabled(true);//上拉配置
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
//                rv.refreshComplete();
                isRefresh = true;
                //下拉刷新
                page = 5010;
                request();
            }
            @Override
            public void onLoadMore() {
                isRefresh = false;
                page++;
                request();
//                rv.loadMoreComplete();
            }
        });
    }
    @Override
    public void success(News news) {
        //转换json        String json = new Gson().toJson(news);
        System.out.println("size:" + news.data.size());
        data = news.data;
        if (isRefresh) {
            newsAdapter = new NewsAdapter(data, this,news);
            rv.setAdapter(newsAdapter);
            rv.refreshComplete();
            //保存json串数据
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            ContentValues contentValues = new ContentValues();
            contentValues.put("json", json);
            db.insert(DbHelper.NEWS_TABLE_NAME, null, contentValues);
        } else {
            if (newsAdapter != null) {
                //上啦加载更多,刷新
                newsAdapter.loadMore(news.data);
            }
            rv.loadMoreComplete();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.detach();
    }
}


activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <com.jcodecraeer.xrecyclerview.XRecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>


INews 接口:
public interface INews {
    void success(News news);
}

News 类:
public class News {
    public String stat;
    public List<Data> data;

    public class Data {
        public String topic;
        public String source;
        public List<IMG> miniimg;

        public class IMG {
            public String src;
        }
    }
}



NewsPresenter  类:
public class NewsPresenter {
    private INews iNews;
    private NewsModel model;
    public NewsPresenter(INews iNews) {
        model = new NewsModel();
        attach(iNews);
    }
    /**
     * 绑定view
     * @param iNews
     */
    public void attach(INews iNews){
        this.iNews = iNews;
    }
    /**
     * 获取数据的方法
     * @param getUrl
     */
    public void getData(String getUrl, Map<String ,String> params) {
        model.getData(getUrl, params,new NewsModel.ResponseCallback() {
            @Override
            public void success(String result) {
                if (!TextUtils.isEmpty(result)) {
                    String s = result.replace("null(","")
                            .replace(")","");

                    News news = new Gson().fromJson(s, News.class);
                    iNews.success(news);
                }
            }
            @Override
            public void fail(String msg) {
            }
        });

//        OkhttpUtils.getInstance().getData(getUrl, new OkhttpUtils.ICallback() {
//            @Override
//            public void success(String result) {
//
//            }
//
//            @Override
//            public void fail(String msg) {
//
//            }
//        });
    }
    /**
     * 解绑
     */
    public void detach(){
        this.iNews = null;
    }
}


NewsModel 类:
public class NewsModel {

    /**
     * 请求数据
     * @param getUrl
     */
    public void getData(String getUrl, Map<String,String> params, final ResponseCallback responseCallback) {

        OkhttpUtils.getInstance().postData(getUrl,params, new OkhttpUtils.ICallback() {
            @Override
            public void success(String result) {
                responseCallback.success(result);

            }

            @Override
            public void fail(String msg) {

                responseCallback.fail(msg);
            }
        });
    }

    public interface  ResponseCallback{
        void success(String result);
        void fail(String msg);
    }
}


OkhttpUtils  类:
public class OkhttpUtils {
    private static OkhttpUtils okhttpUtils;
    private OkHttpClient okHttpClient;
    private Handler handler;
    private OkhttpUtils() {
        okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
                .build();
        handler = new Handler();
    }

    public static OkhttpUtils getInstance() {
        if (okhttpUtils == null) {
            okhttpUtils = new OkhttpUtils();
        }
        return okhttpUtils;
    }

    /**
     * get方式
     */
    public void getData(String url, final ICallback callback){
        final Request request = new Request.Builder()
                .url(url).build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

                if (callback!=null){
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            callback.fail("请求失败");
                        }
                    });
                }
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (callback!=null){
                    if (response.isSuccessful()&&response.code()==200){
                        final String result = response.body().string();
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                callback.success(result);
                            }
                        });
                    }
                }
            }
        });
    }

    /**
     * post方式
     */
    public void postData(String url, Map<String,String> params, final ICallback callback){
        FormBody.Builder builder = new FormBody.Builder();
        for (Map.Entry<String, String> bean : params.entrySet()) {
            builder.add(bean.getKey(),bean.getValue());
        }
        final Request request = new Request.Builder()
                .url(url).post(builder.build()).build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                if (callback!=null){
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            callback.fail("请求失败");
                        }
                    });
                }
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (callback!=null){
                    if (response.isSuccessful()&&response.code()==200){
                        final String result = response.body().string();
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                callback.success(result);
                            }
                        });
                    }
                }
            }
        });
    }
    public interface ICallback{
        void success(String result);
        void fail(String msg);
    }
}


NewsAdapter   适配器类:
 
 
public class NewsAdapter extends com.jcodecraeer.xrecyclerview.XRecyclerView.Adapter<com.jcodecraeer.xrecyclerview.XRecyclerView.ViewHolder> {

    private List<News.Data> list;
    private Context context;
    private  News news;

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

    public void loadMore(List<News.Data> data) {
        if (list != null) {
            list.addAll(data);
            notifyDataSetChanged();
        }
    }


    @NonNull
    @Override
    public com.jcodecraeer.xrecyclerview.XRecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == Constants.TYPE1) {
            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) {
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("删除");
                builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        int pos = holder.getLayoutPosition();//得到下标
                        System.out.println("pos----"+pos);
                        list.remove(position);//删除集合的数据
                        news.data = list;
                        String json =  new Gson().toJson(news);
                        notifyDataSetChanged();
                    }
                });
                builder.setNeutralButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                    }
                });
                builder.show();
                return true;
            }
        });

        News.Data data = list.get(position);
        if (holder instanceof Type1ViewHolder) {//1张图片
            ((Type1ViewHolder) holder).title.setText(data.topic);
        } else if (holder instanceof Type2ViewHolder) {//三张图片
            ((Type2ViewHolder) holder).title.setText(data.topic);
            if (data.miniimg != null && data.miniimg.size() > 0) {
                if (data.miniimg.size() == 1) {
                    Glide.with(context).load(data.miniimg.get(0).src).into(((Type2ViewHolder) holder).iv1);
                    Glide.with(context).load(data.miniimg.get(0).src).into(((Type2ViewHolder) holder).iv2);
                    Glide.with(context).load(data.miniimg.get(0).src).into(((Type2ViewHolder) holder).iv3);
                } else if (data.miniimg.size() == 2) {
                    Glide.with(context).load(data.miniimg.get(0).src).into(((Type2ViewHolder) holder).iv1);
                    Glide.with(context).load(data.miniimg.get(1).src).into(((Type2ViewHolder) holder).iv2);
                    Glide.with(context).load(data.miniimg.get(1).src).into(((Type2ViewHolder) holder).iv3);
                } else {
                    Glide.with(context).load(data.miniimg.get(0).src).into(((Type2ViewHolder) holder).iv1);
                    Glide.with(context).load(data.miniimg.get(1).src).into(((Type2ViewHolder) holder).iv2);
                    Glide.with(context).load(data.miniimg.get(2).src).into(((Type2ViewHolder) holder).iv3);
                }
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        return position % 2 == 0 ? Constants.TYPE1 : Constants.TYPE2;
    }

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

    class Type1ViewHolder extends com.jcodecraeer.xrecyclerview.XRecyclerView.ViewHolder {
        private TextView title;
        public Type1ViewHolder(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title1);
        }
    }

    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);
        }
    }
}


Constants  类:
public class Constants {
    public static final String GET_URL = "http://ttpc.dftoutiao.com/jsonpc/refresh";
    public static final  int TYPE1 = 3;//条目是三张图
    public static final  int TYPE2 = 1;//条目是1张图
}


news_item_layout  适配器布局:
<?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="wrap_content"
    android:orientation="vertical"
    android:padding="10dp">
    <TextView
        android:id="@+id/title3"
        android:text="我是标题"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="10dp">
        <ImageView
            android:id="@+id/img1"
            android:src="@mipmap/ic_launcher"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:scaleType="centerCrop"
            android:layout_height="70dp"/>
        <ImageView
            android:id="@+id/img2"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:src="@mipmap/ic_launcher"
            android:layout_width="0dp"
            android:scaleType="centerCrop"
            android:layout_weight="1"
            android:layout_height="70dp"/>
        <ImageView
            android:id="@+id/img3"
            android:src="@mipmap/ic_launcher"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:scaleType="centerCrop"
            android:layout_height="70dp"/>
    </LinearLayout>
</LinearLayout>
news_item2_layout  适配器布局:
<?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="wrap_content"
    android:orientation="horizontal"
    android:padding="10dp">
    <ImageView
        android:id="@+id/img1"
        android:src="@mipmap/ic_launcher"
        android:layout_width="wrap_content"
        android:scaleType="centerCrop"
        android:layout_height="wrap_content"/>
    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginTop="10dp">
        <TextView
            android:id="@+id/title1"
            android:layout_marginLeft="10dp"
            android:text="我是标题"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</LinearLayout>


DbHelper   类:
public class DbHelper extends SQLiteOpenHelper {
    //数据库文件名称
    private static final String DB_NAME = "news.db";
    public static final String NEWS_TABLE_NAME = "news";
    private static final int VERSION = 1;
    public DbHelper(Context context) {
        super(context, DB_NAME, null, VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        String sql = "create table " + NEWS_TABLE_NAME + " (_id Integer PRIMARY KEY ,json text)";
        sqLiteDatabase.execSQL(sql);
    }
    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
    }
}

NetWorkUtil 类:
public class NetWorkUtil {
    /**
     * @return 是否有活动的网络连接
     */
    public final static boolean hasNetWorkConnection(Context context){
        //获取连接活动管理器
        final ConnectivityManager connectivityManager= (ConnectivityManager) context.
                getSystemService(Context.CONNECTIVITY_SERVICE);
        //获取链接网络信息
        final NetworkInfo networkInfo=connectivityManager.getActiveNetworkInfo();
        return (networkInfo!= null && networkInfo.isAvailable());
    }
    /**
     * @return 返回boolean ,是否为wifi网络
     */
    public final static boolean hasWifiConnection(Context context)
    {
        final ConnectivityManager connectivityManager= (ConnectivityManager) context.
                getSystemService(Context.CONNECTIVITY_SERVICE);
        final NetworkInfo networkInfo=connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        //是否有网络并且已经连接
        return (networkInfo!=null&& networkInfo.isConnectedOrConnecting());
    }

    /**
     * @return 返回boolean,判断网络是否可用,是否为移动网络
     */
    public final static boolean hasGPRSConnection(Context context){
        //获取活动连接管理器
        final ConnectivityManager connectivityManager= (ConnectivityManager) context.
                getSystemService(Context.CONNECTIVITY_SERVICE);
        final NetworkInfo networkInfo=connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        return (networkInfo!=null && networkInfo.isAvailable());
    }
    /**
     * @return  判断网络是否可用,并返回网络类型,ConnectivityManager.TYPE_WIFIConnectivityManager.TYPE_MOBILE,不可用返回-1
     */
    public static final int getNetWorkConnectionType(Context context){
        final ConnectivityManager connectivityManager=(ConnectivityManager) context.
                getSystemService(Context.CONNECTIVITY_SERVICE);
        final NetworkInfo wifiNetworkInfo=connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        final NetworkInfo mobileNetworkInfo=connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if(wifiNetworkInfo!=null &&wifiNetworkInfo.isAvailable())
        {
            return ConnectivityManager.TYPE_WIFI;
        }
        else if(mobileNetworkInfo!=null &&mobileNetworkInfo.isAvailable())
        {
            return ConnectivityManager.TYPE_MOBILE;
        }
        else {
            return -1;
        }
    }
}




猜你喜欢

转载自blog.csdn.net/jun_tong/article/details/80518659