集成步骤:
1.先在项目 build.gradle(Project:XXXX) 的 repositories 添加:
maven { url "https://jitpack.io" }
2.然后在 build.gradle(Module:app) 的 dependencies 添加:
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:3.0.1'
3.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:minHeight="65dp" android:orientation="vertical"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:orientation="horizontal"> <ImageView android:id="@+id/imgIcon" android:layout_width="50dp" android:layout_height="50dp"/> <TextView android:id="@+id/tvName" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="15dp" android:gravity="center_vertical" android:minHeight="46dp" android:text="张丹峰" android:textColor="@android:color/black" /> <TextView android:id="@+id/tvContent" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="15dp" android:gravity="center_vertical" android:minHeight="46dp" android:text="描述介绍" android:textColor="@android:color/black" /> </LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginStart="13dp" android:layout_marginTop="10dp" android:background="@android:color/holo_green_dark" /> </LinearLayout>
4.Activity
public class MainActivity extends AppCompatActivity { private RecyclerView recycleView; private MainAdapter mainAdapter; private List<HomeItem> homeList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getHomeList(); initViw(); } public void initViw() { recycleView = findViewById(R.id.recycleView); mainAdapter = new MainAdapter(R.layout.item_user, homeList); recycleView.setAdapter(mainAdapter); } public void getHomeList(){ homeList = new ArrayList<>(); HomeItem homeItem = new HomeItem(); homeItem.name = "张三丰"; homeItem.desc = "一代武术大师"; homeItem.age = 103; homeList.add(homeItem); homeItem = new HomeItem(); homeItem.name = "柯南"; homeItem.desc = "名侦探,走到哪里哪里就有命案发生"; homeItem.age = 20; homeList.add(homeItem); homeItem = new HomeItem(); homeItem.name = "鸣人"; homeItem.desc = "火影的主角,主角光环,一步一步提升"; homeItem.age = 103; homeList.add(homeItem); } }
5.Adapter
public class MainAdapter extends BaseQuickAdapter<HomeItem, BaseViewHolder> { public MainAdapter(int layoutResId, List<HomeItem> data) { super(layoutResId, data); } @Override protected void convert(BaseViewHolder baseViewHolder, HomeItem homeItem) { baseViewHolder.setText(R.id.tvName, homeItem.name); //输入文字 TextView tvContent = baseViewHolder.getView(R.id.tvContent); tvContent.setText("获取ID后赋值"); baseViewHolder.setImageResource(R.id.imgIcon ,R.mipmap.ic_launcher); //设置加载图片 // baseViewHolder.setBackgroundColor() //设置背景颜色 // baseViewHolder.setEnabled() //设置是否可以点击 // baseViewHolder.setGone() //是否隐藏 // baseViewHolder.setVisible() //是否显示 } }
6.设置Item点击事件
public void initViw() { recycleView = findViewById(R.id.recycleView); mainAdapter = new MainAdapter(R.layout.item_user, homeList); recycleView.setAdapter(mainAdapter); mainAdapter.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) { Log.e("rvAdapterHelper" ,"单击事件"); } }); mainAdapter.setOnItemLongClickListener(new OnItemLongClickListener() { @Override public boolean onItemLongClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) { Log.e("rvAdapterHelper" ,"长按事件"); return false; } }); /** * 绑定child的ViewID,用于点击事件 * 注意:设置子控件的事件,如果不在adapter中绑定,点击事件无法生效,因为无法找到你需要设置的控件。 * */ mainAdapter.addChildClickViewIds(R.id.imgIcon); mainAdapter.setOnItemChildClickListener(new OnItemChildClickListener() { @Override public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) { Log.e("rvAdapterHelper" ,"onItemChildClick"); } }); }
7.添加列表加载动画
开启动画(默认为渐显效果)adapter.openLoadAnimation();
默认提供5种方法(渐显、缩放、从下到上,从左到右、从右到左)
切换动画
quickAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
自定义动画
quickAdapter.openLoadAnimation(new BaseAnimation() {
@Override
public Animator[] getAnimators(View view) {
return new Animator[]{
ObjectAnimator.ofFloat(view, “scaleY”, 1, 1.1f, 1),
ObjectAnimator.ofFloat(view, “scaleX”, 1, 1.1f, 1)
};
}
});
动画默认只执行一次,如果想重复执行可设置
mQuickAdapter.isFirstOnly(false);
8.添加头部、尾部
添加
-
mQuickAdapter.addHeaderView(getView());
-
mQuickAdapter.addFooterView(getView());
删除指定view
-
mQuickAdapter.removeHeaderView(getView);
-
mQuickAdapter.removeFooterView(getView);
删除所有
-
mQuickAdapter.removeAllHeaderView();
-
mQuickAdapter.removeAllFooterView();
默认出现了头部就不会显示Empty,和尾部,配置以下方法也支持同时显示:
-
setHeaderAndEmpty
-
setHeaderFooterEmpty
默认头部尾部都是占满一行,如果需要不占满可以配置:
-
setHeaderViewAsFlow
-
setFooterViewAsFlow
还有更多的属性,可以点击链接:https://blog.csdn.net/chehec2010/article/details/83902263 查看
最后是交流公众号,大家可以关注一下
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/119171.html