BRVAH是GitHub上的一个RecyclerView 框架,非常强大 。
代码可以运行,GitHub仓库和依赖要加上去
其他的看代码注释
activity_main
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/id_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#000000"
android:dividerHeight="10dp" />
</RelativeLayout>
item1
<?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="50dp"
android:orientation="horizontal">
<TextView
android:id="@+id/id_num"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#44ff0000"
android:gravity="center"
android:text="1" />
</LinearLayout>
MainActivity
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
import android.view.View;
import android.widget.Toast;
import com.chad.library.adapter.base.BaseQuickAdapter;
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private List<String> mDatas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//模拟数据
initData();
//获取控件
mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview);
//方向
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//item布局、列表数据源
HomeAdapter mAdapter = new HomeAdapter(R.layout.item1, mDatas);
//适配器
mRecyclerView.setAdapter(mAdapter);
//动画
mAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
//单击事件
mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
@Override
public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
Toast.makeText(MainActivity.this, "单击" + mDatas.get(position), Toast.LENGTH_SHORT).show();
}
});
//长按事件
mAdapter.setOnItemChildLongClickListener(new BaseQuickAdapter.OnItemChildLongClickListener() {
@Override
public boolean onItemChildLongClick(BaseQuickAdapter adapter, View view, int position) {
Toast.makeText(MainActivity.this, "长按" + mDatas.get(position), Toast.LENGTH_SHORT).show();
return false;
}
});
}
protected void initData() {
mDatas = new ArrayList<>();
for (int i = 'A'; i < 'Z'; i++) {
mDatas.add("" + (char) i);
}
}
}
HomeAdapter
import android.graphics.Color;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import java.util.List;
class HomeAdapter extends BaseQuickAdapter<String, BaseViewHolder> {
HomeAdapter(int layoutResId, List<String> data) {
super(layoutResId, data);
}
@Override
protected void convert(BaseViewHolder helper, String data) {
helper.setText(R.id.id_num, data);
//添加监听事件
helper.addOnClickListener(R.id.id_num);
//getLayoutPosition() 获取当前item的position
if (helper.getAdapterPosition() % 2 == 0) {
helper.setTextColor(R.id.id_num, Color.RED);
} else {
helper.setTextColor(R.id.id_num, Color.YELLOW);
}
}
}
maven 仓库(Project : test)
allprojects {
repositories {
google()
jcenter()
maven { url "https://jitpack.io" }
}
}
添加依赖(Module:test)
dependencies {
...
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
implementation 'com.android.support:recyclerview-v7:28.0.0'
...
}
网友评论