一、概述
在github中搜索recyclerview关键字,点赞最多的就是(BaseRecyclerViewAdapterHelper)这个库,所以抽空写了个demo看下效果!
二、普通列表
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>
三、下拉刷新、上拉加载
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>