文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Android RecyclerView使用方法解析

2022-06-06 07:00

关注

1.简介

  RecyclerView是一种新的视图组,目标是为任何基于适配器的视图提供相似的渲染方式。它被作为ListView和GridView控件的继承者,在最新的support-V7版本中提供支持。RecyclerView架构,提供了一种插拔式的体验,高度的解耦,异常的灵活,通过设置它提供的不同LayoutManager,ItemDecoration , ItemAnimator实现令人瞠目的效果。

总之ListView和GridView能做的RecyclerView都能做,并且能实现瀑布流效果

2.使用

在build.gradle配置RecyclerView的库
compile 'com.android.support:recyclerview-v7:23.3.0'


//设置RecyclerView的适配器
adapter = new MyRecyclerViewAdapter(RecyclerViewActivity.this,datas);
recyclerview.setAdapter(adapter);
//LayoutManager
recyclerview.setLayoutManager(new LinearLayoutManager(RecyclerViewActivity.this, LinearLayoutManager.VERTICAL, false));
// recyclerview.scrollToPosition(datas.size()-1);
//添加RecyclerView的分割线
recyclerview.addItemDecoration(new DividerListItemDecoration(RecyclerViewActivity.this,DividerListItemDecoration.VERTICAL_LIST));
//设置动画
recyclerview.setItemAnimator(new DefaultItemAnimator());

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
 private final Context context;
 private final ArrayList<String> datas;
 public MyAdapter(Context context,ArrayList<String> datas){
 this.context = context;
 this.datas = datas;
 }
 
 @Override
 public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
 View view = View.inflate(context, R.layout.item_hello, null);
 return new MyViewHolder(view);
 }
 @Override
 public void onBindViewHolder(MyViewHolder holder, int position) {
 holder.tv_text.setText(datas.get(position));
 holder.iv_icon.setBackgroundResource(R.mipmap.ic_launcher);
 }
 @Override
 public int getItemCount() {
 return datas.size();
 }
 class MyViewHolder extends RecyclerView.ViewHolder {
 private TextView tv_text;
 private ImageView iv_icon;
 public MyViewHolder(View itemView) {
  super(itemView);
  tv_text = (TextView) itemView.findViewById(R.id.tv_text);
  iv_icon = (ImageView) itemView.findViewById(R.id.iv_icon);
 }
 }
}

3.设置ListView&GridView&瀑布流类型效果


//设置List类型效果
recyclerview.setLayoutManager(new LinearLayoutManager(RecyclerViewActivity.this,LinearLayoutManager.VERTICAL,false));
//设置Grid类型效果
recyclerview.setLayoutManager(new GridLayoutManager(RecyclerViewActivity.this, 2, GridLayoutManager.VERTICAL, false));
//recyclerview.scrollToPosition(99);
//设置瀑布流类型效果
recyclerview.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL));

4.分割线

参照网址:http://blog.csdn.net/lmj623565791/article/details/45059587


//设置分割线-分割线需要自定义&还可以自定义分割线的样式
//没有提供默认的分割线
recyclerview.addItemDecoration(new DividerListItemDecoration(this, DividerListItemDecoration.VERTICAL_LIST));

public class DividerListItemDecoration extends RecyclerView.ItemDecoration {
 private static final int[] ATTRS = new int[]{
  android.R.attr.listDivider
 };
 public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
 public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
 private Drawable mDivider;
 private int mOrientation;
 public DividerListItemDecoration(Context context, int orientation) {
 final TypedArray a = context.obtainStyledAttributes(ATTRS);
 mDivider = a.getDrawable(0);
 a.recycle();
 setOrientation(orientation);
 }
 public void setOrientation(int orientation) {
 if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
  throw new IllegalArgumentException("invalid orientation");
 }
 mOrientation = orientation;
 }
 @Override
 public void onDraw(Canvas c, RecyclerView parent) {
// Log.e("recyclerview - itemdecoration", "onDraw()");
 if (mOrientation == VERTICAL_LIST) {
  drawVertical(c, parent);
 } else {
  drawHorizontal(c, parent);
 }
 }
 public void drawVertical(Canvas c, RecyclerView parent) {
 final int left = parent.getPaddingLeft();
 final int right = parent.getWidth() - parent.getPaddingRight();
 final int childCount = parent.getChildCount();
 for (int i = 0; i < childCount; i++) {
  final View child = parent.getChildAt(i);
  android.support.v7.widget.RecyclerView v = new android.support.v7.widget.RecyclerView(parent.getContext());
  final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
   .getLayoutParams();
  final int top = child.getBottom() + params.bottomMargin;
  final int bottom = top + mDivider.getIntrinsicHeight();
  mDivider.setBounds(left, top, right, bottom);
  mDivider.draw(c);
 }
 }
 public void drawHorizontal(Canvas c, RecyclerView parent) {
 final int top = parent.getPaddingTop();
 final int bottom = parent.getHeight() - parent.getPaddingBottom();
 final int childCount = parent.getChildCount();
 for (int i = 0; i < childCount; i++) {
  final View child = parent.getChildAt(i);
  final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
   .getLayoutParams();
  final int left = child.getRight() + params.rightMargin;
  final int right = left + mDivider.getIntrinsicHeight();
  mDivider.setBounds(left, top, right, bottom);
  mDivider.draw(c);
 }
 }
 @Override
 public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
 if (mOrientation == VERTICAL_LIST) {
  outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
 } else {
  outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
 }
 }
}
DividerListItemDecoration

设置分割线样式:

①application设置


<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
 <!-- Customize your theme here. -->
 <item name="colorPrimary">@color/colorPrimary</item>
 <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
 <item name="colorAccent">@color/colorAccent</item>
 <item name="android:listDivider">@drawable/divider_bg</item>
</style>

②在activity中


<activity android:name=".recyclerview.RecyclerViewActivity"
 android:theme="@style/listDividerTheme"/>
<style name="listDividerTheme" parent="Theme.AppCompat.Light.DarkActionBar">
 <!-- Customize your theme here. -->
 <item name="colorPrimary">@color/colorPrimary</item>
 <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
 <item name="colorAccent">@color/colorAccent</item>
 <item name="android:listDivider">@drawable/divider_bg</item>
</style>

5.自定义设置item的点击事件

RecyclerView默认是没有点击事件的,需要自定义点击事件
用到知识点:接口,getLayoutPosition()


public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{
 private final Context context;
 private final ArrayList<String> datas;
 //设置点击某个item的监听
 public interface OnItemClickListener{
  void onItemClick(View view,int position,String content);
 }
 private OnItemClickListener onItemClickListener;
 public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
 this.onItemClickListener = onItemClickListener;
 }
 //设置点击图片
 public interface OnImageViewClickListener{
 void onImageViewClick(View view,int position);
 }
 private OnImageViewClickListener onImageViewClickListener;
 public void setOnImageViewClickListener(OnImageViewClickListener onImageViewClickListener) {
 this.onImageViewClickListener = onImageViewClickListener;
 }
 ...................................
 class MyViewHolder extends RecyclerView.ViewHolder {
 private TextView tv_text;
 private ImageView iv_icon;
 public MyViewHolder(View itemView) {
  super(itemView);
  tv_text = (TextView) itemView.findViewById(R.id.tv_text);
  iv_icon = (ImageView) itemView.findViewById(R.id.iv_icon);
  //设置点击事件
  itemView.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
   if(onItemClickListener != null){
   onItemClickListener.onItemClick(v,getLayoutPosition(),datas.get(getLayoutPosition()));
   }
  }
  });
  //设置监听
  iv_icon.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
   if(onImageViewClickListener != null){
   onImageViewClickListener.onImageViewClick(v,getLayoutPosition());
   }
  }
  });
 }
 }
}

在Activity中使用自定义的点击事件


//设置点击item的点击事件
myAdapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() {
 @Override
 public void onItemClick(View view, int position, String content) {
 Toast.makeText(RecyclerViewActivity.this, "content=="+content+",--position=="+position, Toast.LENGTH_SHORT).show();
 }
});
//设置点击某张图片的点击事件
myAdapter.setOnImageViewClickListener(new MyAdapter.OnImageViewClickListener() {
 @Override
 public void onImageViewClick(View view, int position) {
 Toast.makeText(RecyclerViewActivity.this, "position=="+position+",view=="+view.toString(), Toast.LENGTH_SHORT).show();
 }
});

6.删除和增加数据

1_在适配器中新增加添加和删除两个方法


public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
 ........................
 public void addData(int position,String content){
 datas.add(position,content);
 notifyItemInserted(position);
 }
 public void removeData(int position){
 datas.remove(position);
 notifyItemRemoved(position);
 }
}

2_Activity中


btn_add.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
 myAdapter.addData(0,"Content NetData");
 //定位到第0个位置
 recyclerview.scrollToPosition(0);
 }
});
btn_remove.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
 myAdapter.removeData(0);
 }
});
您可能感兴趣的文章:学习Android开发之RecyclerView使用初探Android中RecyclerView布局代替GridView实现类似支付宝的界面Android RecyclerView添加头部和底部的方法Android RecyclerView实现下拉刷新和上拉加载Android使用RecyclerView实现自定义列表、点击事件以及下拉刷新学习Android Material Design(RecyclerView代替ListView)Android项目实战之仿网易新闻的页面(RecyclerView )Android RecyclerView加载不同布局简单实现Android代码实现AdapterViews和RecyclerView无限滚动Android RecyclerView滑动删除和拖动排序


阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-移动开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯