我们都知道在androID 5.0
后引入了RecyclerVIEw
来替代ListVIEw
,由于其强大的功能和效果以及其规范化,已经逐渐完全替代了ListVIEw
和GrIDVIEw
,本文将简单探究RecyclerVIEw
替代ListVIEw
的具体使用,在下一篇中将探究RecyclerVIEw
替代GrIDVIEw
的具体使用。参考文章:here, AndroID
进阶之光。
对于AndroID 9.0
(API level 28
)中提出了AndroIDX
,它是Jetpack
的一部分。对于API level 27
或者更早的版本中可以使用support library
,对应于com.androID.support.*
。但是Google
官方推荐使用在项目中使用AndroIDX
,并停止了对com.androID.support.*
的支持。AndroIDX
是对androID.support.xxx
包的整理后产物。由于之前的 support
包过于混乱,所以,Google
推出了AndroIDX
。
故而在一些博客中常见的:
implementation 'com.androID.support:appcompat-v7:22.2.0'implementation 'com.androID.support:appcompat-v7:26.1.0'
将之复制到相应的位置,可以看见如下:
Refactor->Migrate
来迁移到AndrIDX librarIEs
库。由于我们的项目默认创建的就是AndroIDX
的了,这里提示的错误只是因为导入的依赖不对,这里应该是:implementation 'androIDx.recyclervIEw:recyclervIEw:1.1.0'
2.1.2 基本案例主布局文件:recyclevIEw.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"> <androIDx.recyclervIEw.Widget.RecyclerVIEw androID:ID="@+ID/myrecyclevIEw" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content"> </androIDx.recyclervIEw.Widget.RecyclerVIEw></linearLayout>
和ListVIEw
类似的,我们需要定义每一个item
的样式布局:recyclevIEw_item.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="vertical"> <TextVIEw androID:ID="@+ID/tv_item" androID:layout_wIDth="match_parent" androID:layout_height="50dp" androID:gravity="center" /></linearLayout>
同样,需要定义一个适配器方法,该适配器方法需要继承自RecyclerVIEw.Adapter
,在该类中需要复写:onCreateVIEwHolder
、onBindVIEwHolder
和getItemCount
方法。
package com.example.myapplication;import androID.content.Context;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.TextVIEw;import androIDx.annotation.NonNull;import androIDx.recyclervIEw.Widget.RecyclerVIEw;import java.util.List;public class MyAdapter extends RecyclerVIEw.Adapter{ private List<String> mList; private Context context; public MyAdapter(Context context, List<String> mList){ this.context = context; this.mList = mList; } @NonNull @OverrIDe public MyVIEwHolder onCreateVIEwHolder(@NonNull VIEwGroup parent, int vIEwType) { VIEw itemVIEw = LayoutInflater.from(context).inflate(R.layout.recyclevIEw_item, parent, false); return new MyVIEwHolder(itemVIEw); } @OverrIDe public voID onBindVIEwHolder(@NonNull RecyclerVIEw.VIEwHolder holder, int position) { ((MyVIEwHolder) holder).tv.setText(mList.get(position)); } @OverrIDe public int getItemCount() { return mList.size(); } class MyVIEwHolder extends RecyclerVIEw.VIEwHolder{ TextVIEw tv; public MyVIEwHolder(VIEw vIEw){ super(vIEw); tv = (TextVIEw) vIEw.findVIEwByID(R.ID.tv_item); } }}
和之前写ListVIEw
中的VIEwHolder
中的区别在于,这里的适配器,需要强制实现onBindVIEwHolder
,也就是必须要写一个VIEwHolder
类,继承自RecyclerVIEw.VIEwHolder
,以保存已经实例化好的变量。使用onBindVIEwHolder
方法来得到已经实例化过的对象,进而进行一些必要的赋值 *** 作。
然后是我们的函数入口:
package com.example.myapplication;import androIDx.appcompat.app.AppCompatActivity;import androIDx.recyclervIEw.Widget.DefaultItemAnimator;import androIDx.recyclervIEw.Widget.linearlayoutmanager;import androIDx.recyclervIEw.Widget.RecyclerVIEw;import androID.os.Bundle;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity { private RecyclerVIEw recyclerVIEw; private MyAdapter adapter; private List<String> mList; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.recyclevIEw); recyclerVIEw = findVIEwByID(R.ID.myrecyclevIEw); linearlayoutmanager linearlayoutmanager = new linearlayoutmanager(this); linearlayoutmanager.setorIEntation(RecyclerVIEw.VERTICAL); recyclerVIEw.setLayoutManager(linearlayoutmanager); recyclerVIEw.setItemAnimator(new DefaultItemAnimator()); mList = getList(); adapter = new MyAdapter(this, mList); recyclerVIEw.setAdapter(adapter); } private List<String> getList(){ List<String> List = new ArrayList<>(); for (int i = 0; i < 20; i++) { List.add(i+""); } return List; }}
注意到与ListVIEw
不同的一点就是,需要设置布局管理器用于设置条目的排列样式,可以是垂直排列或者水平排列。其余的都是类似的处理。
可以看见效果:
谷歌目前没有提供默认的分割线,这就需要我们继承RecyclerVIEw.Itemdecoration
来自定义分割线。然后使用recyclerVIEw.addItemdecoration()
来加入分割线。
package com.example.myapplication;import androID.content.Context;import androID.content.res.TypedArray;import androID.graphics.Canvas;import androID.graphics.drawable.Drawable;import androID.vIEw.VIEw;import androIDx.annotation.NonNull;import androIDx.recyclervIEw.Widget.RecyclerVIEw;public class VerticaldivIDerItemdecoration extends RecyclerVIEw.Itemdecoration { private Drawable divIDer; public VerticaldivIDerItemdecoration(Context context){ final TypedArray a = context.obtainStyledAttributes(new int[]{androID.R.attr.ListdivIDer}); divIDer = a.getDrawable(0); a.recycle(); } @OverrIDe public voID onDraw(@NonNull Canvas c, @NonNull RecyclerVIEw parent, @NonNull RecyclerVIEw.State state) { 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); RecyclerVIEw v = new RecyclerVIEw(parent.getContext()); final RecyclerVIEw.LayoutParams params = (RecyclerVIEw.LayoutParams)child.getLayoutParams(); final int top = child.getBottom() + params.bottommargin; final int bottom = top + divIDer.getIntrinsicHeight(); divIDer.setBounds(left, top, right, bottom); divIDer.draw(c); } }}
上面的代码是我从AndroID
进阶之光中抄的。然后使用就是:
recyclerVIEw.addItemdecoration(new VerticaldivIDerItemdecoration(MainActivity.this));
可以看见效果:
列表中条目的点击事件需要我们自己来定义。这种方式需要结合接口来进行编程处理,比较灵活。有点类似JavaScript
中定义的回调函数处理。我们对Item
的点击事件,也就是放置在Adapter
中进行处理,然后设置接口函数,添加添加点击回调
,然后在MainActivity
中进行实例化该接口对象即可。
package com.example.myapplication;import androID.content.Context;import androID.util.Log;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.TextVIEw;import androIDx.annotation.NonNull;import androIDx.recyclervIEw.Widget.RecyclerVIEw;import java.util.List;public class MyAdapter extends RecyclerVIEw.Adapter{ private List<String> mList; private Context context; public MyAdapter(Context context, List<String> mList){ this.context = context; this.mList = mList; } @NonNull @OverrIDe public MyVIEwHolder onCreateVIEwHolder(@NonNull VIEwGroup parent, int vIEwType) { VIEw itemVIEw = LayoutInflater.from(context).inflate(R.layout.recyclevIEw_item, parent, false); return new MyVIEwHolder(itemVIEw); } @OverrIDe public voID onBindVIEwHolder(@NonNull RecyclerVIEw.VIEwHolder holder, final int position) { final MyVIEwHolder myVIEwHolder = ((MyVIEwHolder) holder); myVIEwHolder.tv.setText(mList.get(position)); if(onItemClickListener!=null){ // 点击 myVIEwHolder.tv.setonClickListener(new VIEw.OnClickListener(){ @OverrIDe public voID onClick(VIEw vIEw) { onItemClickListener.onItemClick(myVIEwHolder.tv, position); } }); // 长按 myVIEwHolder.tv.setonLongClickListener(new VIEw.OnLongClickListener(){ @OverrIDe public boolean onLongClick(VIEw vIEw) { onItemClickListener.onItemLongClick(myVIEwHolder.tv, position); return false; } }); } } @OverrIDe public int getItemCount() { return mList.size(); } class MyVIEwHolder extends RecyclerVIEw.VIEwHolder{ TextVIEw tv; public MyVIEwHolder(VIEw vIEw){ super(vIEw); tv = (TextVIEw) vIEw.findVIEwByID(R.ID.tv_item); } } private OnItemClickListener onItemClickListener; public interface OnItemClickListener{ voID onItemClick(VIEw veiw, int position); voID onItemLongClick(VIEw vIEw, int position); } public voID setonItemClickListener(OnItemClickListener onItemClickListener){ this.onItemClickListener = onItemClickListener; }}
MainActivity.java
中:
adapter = new MyAdapter(this, mList);adapter.setonItemClickListener(new MyAdapter.OnItemClickListener() { @OverrIDe public voID onItemClick(VIEw veiw, int position) { Toast.makeText(MainActivity.this, "点击了第"+(position+1)+"条", Toast.LENGTH_SHORT).show(); } @OverrIDe public voID onItemLongClick(VIEw vIEw, int position) { Toast.makeText(MainActivity.this, "长按了第"+(position+1)+"条", Toast.LENGTH_SHORT).show(); } }); recyclerVIEw.setAdapter(adapter);
总结 以上是内存溢出为你收集整理的RecyclerView替代ListView全部内容,希望文章能够帮你解决RecyclerView替代ListView所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)