首页 > 其他分享 >BaseRecyclerViewAdapterHelper学习实践

BaseRecyclerViewAdapterHelper学习实践

时间:2023-03-02 13:04:15浏览次数:49  
标签:layout item 实践 学习 BaseRecyclerViewAdapterHelper import android com id


一、概述

在github中搜索recyclerview关键字,点赞最多的就是(​​BaseRecyclerViewAdapterHelper​​)这个库,所以抽空写了个demo看下效果!

二、普通列表

BaseRecyclerViewAdapterHelper学习实践_android

FirstActivity.java

package com.czhappy.brvahdemo.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.czhappy.brvahdemo.model.FirstData;
import com.czhappy.brvahdemo.R;
import com.czhappy.brvahdemo.adapter.FirstAdapter;
import com.czhappy.brvahdemo.utils.Utils;
import com.czhappy.brvahdemo.view.ItemDivider;

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

/**
* author: chenzheng
* created on: 2017/8/30 8:49
* description:
*/

public class FirstActivity extends AppCompatActivity {

private List<FirstData> mDataList;
private RecyclerView mRecyclerView;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
initView();
initData();
initAdapter();
}

private void initView() {
mRecyclerView = (RecyclerView) findViewById(R.id.rv_list);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//添加分割线
mRecyclerView.addItemDecoration(new ItemDivider().setDividerWith(Utils.dip2px(this, 0.5f)).setDividerColor(0xffdddddd));

}

@SuppressWarnings("unchecked")
private void initAdapter() {
final BaseQuickAdapter firstAdapter = new FirstAdapter(R.layout.item_first_list, mDataList);
//firstAdapter.openLoadAnimation();
firstAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
mDataList.get(position).name = "更新--"+position;
firstAdapter.setNewData(mDataList);
}
});

mRecyclerView.setAdapter(firstAdapter);
}

private void initData() {
mDataList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
FirstData item = new FirstData();
item.drawable = R.mipmap.ic_launcher;
item.name = "张珊珊所所所所aaaaa343543543"+i;
mDataList.add(item);
}
}
}

FirstAdapter.java

package com.czhappy.brvahdemo.adapter;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.czhappy.brvahdemo.model.FirstData;
import com.czhappy.brvahdemo.R;

import java.util.List;

/**
* author: chenzheng
* created on: 2017/8/30 9:06
* description:
*/

public class FirstAdapter extends BaseQuickAdapter<FirstData, BaseViewHolder> {

public FirstAdapter(int layoutResId, List<FirstData> list) {
super(layoutResId, list);
}

@Override
protected void convert(BaseViewHolder helper, FirstData item) {

helper.setText(R.id.name_tv, item.name);
helper.setImageResource(R.id.image_iv, item.drawable);
}
}

activity_first.xml:

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

<android.support.v7.widget.RecyclerView
android:id="@+id/rv_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fadingEdge="none"/>

</LinearLayout>

item_first_list.xml

<?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:gravity="center_vertical"
android:padding="10dp">


<ImageView
android:id="@+id/image_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />

<TextView
android:id="@+id/name_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textColor="#333333"
android:textSize="16sp" />
</LinearLayout>

三、下拉刷新、上拉加载

BaseRecyclerViewAdapterHelper学习实践_android_02

SecondActivity.java

package com.czhappy.brvahdemo.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.czhappy.brvahdemo.adapter.SecondAdapter;
import com.czhappy.brvahdemo.model.FirstData;
import com.czhappy.brvahdemo.R;

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

/**
* author: chenzheng
* created on: 2017/8/30 10:08
* description:
*/

public class SecondActivity extends AppCompatActivity implements BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

private List<FirstData> mDataList = new ArrayList<>();;
private RecyclerView mRecyclerView;
private SecondAdapter secondAdapter;
private SwipeRefreshLayout mSwipeRefreshLayout;

private int page = 0;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
initView();
initData();
initAdapter();
}

private void initView() {
mRecyclerView = (RecyclerView) findViewById(R.id.rv_list);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeLayout);
mSwipeRefreshLayout.setOnRefreshListener(this);
mSwipeRefreshLayout.setColorSchemeColors(Color.rgb(47, 223, 189));
}

@SuppressWarnings("unchecked")
private void initAdapter() {
secondAdapter = new SecondAdapter(R.layout.item_second_list, mDataList);
secondAdapter.setOnLoadMoreListener(this, mRecyclerView);
//secondAdapter.openLoadAnimation();
View headView = getLayoutInflater().inflate(R.layout.top_view, (ViewGroup) mRecyclerView.getParent(), false);
secondAdapter.addHeaderView(headView);
secondAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
mDataList.get(position).name = "更新--" + position;
secondAdapter.setNewData(mDataList);
}
});

mRecyclerView.setAdapter(secondAdapter);
}

private void initData() {
for (int i = 0; i < 10; i++) {
FirstData item = new FirstData();
item.drawable = R.mipmap.ic_launcher;
item.name = "张珊珊所所所所aaaaa" + i;
mDataList.add(item);
}
}

@Override
public void onRefresh() {
secondAdapter.setEnableLoadMore(false);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {

mDataList.clear();
for (int i = 0; i < 10; i++) {
FirstData item = new FirstData();
item.drawable = R.mipmap.ic_launcher;
item.name = "张珊珊所所所所aaaaa" + i;
mDataList.add(item);
}
secondAdapter.setNewData(mDataList);
secondAdapter.setEnableLoadMore(true);
mSwipeRefreshLayout.setRefreshing(false);
}
}, 2000);
}

@Override
public void onl oadMoreRequested() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
page++;
for (int i = 10*page; i < (10*page+10); i++) {
FirstData item = new FirstData();
item.drawable = R.mipmap.ic_launcher;
item.name = "张珊珊所所所所aaaaa" + i;
mDataList.add(item);
}
secondAdapter.setNewData(mDataList);
secondAdapter.loadMoreComplete();
}
}, 2000);
}
}

SecondAdapter.java

package com.czhappy.brvahdemo.adapter;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.czhappy.brvahdemo.model.FirstData;
import com.czhappy.brvahdemo.R;

import java.util.List;

/**
* author: chenzheng
* created on: 2017/8/30 10:09
* description:
*/

public class SecondAdapter extends BaseQuickAdapter<FirstData, BaseViewHolder> {

public SecondAdapter(int layoutResId, List<FirstData> list) {
super(layoutResId, list);
}

@Override
protected void convert(BaseViewHolder helper, FirstData item) {

helper.setText(R.id.name_tv, item.name);
helper.setImageResource(R.id.image_iv, item.drawable);
}
}

activity_second.xml

<?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"
android:orientation="vertical">

<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v7.widget.RecyclerView
android:id="@+id/rv_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>

item_second_list.xml

<?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:gravity="center_vertical"
android:padding="10dp">

<ImageView
android:id="@+id/image_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />

<TextView
android:id="@+id/name_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textColor="#333333"
android:textSize="16sp" />
</LinearLayout>


标签:layout,item,实践,学习,BaseRecyclerViewAdapterHelper,import,android,com,id
From: https://blog.51cto.com/u_4427045/6095980

相关文章

  • Android学习之双向滑动菜单
    MainActivity.java: packagecom.example.bidirslidinglayout;importandroid.app.Activity;importandroid.os.Bundle;importandroid.view.View;importandroid.view.Vie......
  • Android学习之左侧滑动菜单
    MainActivity.java:packagecom.example.slidinglayout;importandroid.app.Activity;importandroid.os.Bundle;importandroid.view.View;importandroid.view.View.OnCli......
  • Android学习之ExpandableListView
    ExpListActivity.java: packageorg.lee.android;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.List;importjava.util.Map;importandroid.app......
  • CVPR 2023 | 基础模型推动语义分割的弱增量学习
    前言 语义分割的弱增量学习(WILSS)目的是学习从廉价和广泛可用的图像级标签中分割出新的类别,但图像级别的标签不能提供定位每个片段的细节。为了解决该问题,本文提出了一个新......
  • git常用命令学习笔记
    git常用命令学习笔记一、下载安装输入网址自动下载 ​​https://git-scm.com/download/win​​常用客户端:sourceTree二、基础配置配置用户信息gitconfig--global......
  • 社交泛娱乐之外,融云 IM 在商业沟通中的实践
    1973年,Talkomatic诞生,这是世界上第一款即时通讯系统。关注【融云全球互联网通信云】了解更多它拥有6个频道,每个频道可容纳5位用户在线聊天,当用户输入信息时,频道内其他......
  • Markdown学习
    Markdown学习标题字体Hello,worldHello,worldHello,worldHello,world引用引用分割线图片超链接我的博客列表1.A2.B3.CABC表格姓名性别......
  • 学习vue3遇到的问题
    1.toReftoRef是用来给抽离响应式对象(被reactive包裹的对象)中的某一个属性的,并且把这个属性包裹成ref对象,使其和原对象产生链接。或许有人就回有人有疑问了?这个toRef存在......
  • Mybatis-Plus学习
    mybatis-plus学习系统环境jdk8+mysql5.7+springBoot+mybatis最新版本+系统编码UTF-8文件配置application.properties#应用名称spring.application.name=mybatisplu......
  • Markdown学习
    Markdown学习标题一级标题:#+空格+标题名N级标题:N个#+空格+标题名字体字体两边+*=斜体这是斜体字体两边+**=粗体这是粗体字体两边+~~=删除......