好吧,经过互联网的许多解释,我已经制作了这个适配器:
public class listadapter extends RecyclerVIEw.Adapter<listadapter.VIEwHolder> { private LayoutInflater layoutInflater; protected Vector<List> Lists; public listadapter(Context context,Vector Lists) { layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); this.Lists = Lists; } @OverrIDe public VIEwHolder onCreateVIEwHolder(VIEwGroup parent,int vIEwType) { VIEw vIEw = layoutInflater.inflate(R.layout.listadapter,null); return new VIEwHolder(vIEw); } @OverrIDe public voID onBindVIEwHolder(VIEwHolder holder,int position) { List List = Lists.elementAt(position); holder.name.setText(List.getname()); //holder.delete.set HOW DO I GET button HERE? } @OverrIDe public int getItemCount() { return Lists.size(); } public static class VIEwHolder extends RecyclerVIEw.VIEwHolder implements VIEw.OnClickListener { public TextVIEw name; public button edit; public button delete; public VIEwHolder(VIEw itemVIEw) { super(itemVIEw); name = (TextVIEw) itemVIEw.findVIEwByID(R.ID.Listname); edit = (button) itemVIEw.findVIEwByID(R.ID.edit); delete = (button) itemVIEw.findVIEwByID(R.ID.delete); edit.setonClickListener(this); } @OverrIDe public voID onClick(VIEw vIEw) { switch (vIEw.getID()) { case R.ID.edit: break; case R.ID.delete: break; default: break; } } }}
我没有找到在recyclervIEw的每个视图中有2个按钮(只有添加图像的例子)的例子,如第一张图片.我的意思是,猜测我的RecyclerVIEw中有10个List元素,我怎么能在每一个中都有一个带有List和2个按钮名称的TextVIEw并处理clickListener?.如下图所示,我亲手为你看看我在说什么:
这是将显示recyclerVIEw的活动;不要把它考虑在内,因为我很确定方法是错的.我从sqlite数据库获取列表信息:
public class ShowlistofLists extends AppCompatActivity {private RecyclerVIEw recyclerVIEw;private RecyclerVIEw.LayoutManager layoutManager;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_show_List_of_Lists); LocalDB localDB = new LocalDB(this,"localBD",null,1); Vector <List> listadapter = localDB.getAllPrivateList(); recyclerVIEw = (RecyclerVIEw) findVIEwByID(R.ID.recyclervIEwListas); recyclerVIEw.setAdapter(new listadapter(this,listadapter)); layoutManager = new linearlayoutmanager(this); recyclerVIEw.setLayoutManager(layoutManager);}
列表具有以下属性:
-IDList int-name String-Description String-creationDate Date-active int (0 or 1)-deactivationDate Date
先感谢您.
/*******************************编辑***************** *********************** /
谢谢你我已经能够显示recyclerVIEw并且它运行良好.但我明白了:
而不是:
这是适配器的XML代码和设计:
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"xmlns:app="http://schemas.androID.com/apk/res-auto"androID:layout_wIDth="match_parent"androID:layout_height="60dp"androID:background="@drawable/rectangle_bg"androID:orIEntation="horizontal"androID:weightSum="1"androID:layout_margintop="5dp"><TextVIEw androID:ID="@+ID/Listname" androID:layout_wIDth="0dp" androID:layout_height="wrap_content" androID:layout_margintop="20dp" androID:layout_weight="0.75" androID:gravity="center_vertical" androID:text="TextVIEw" androID:textSize="15sp"/><linearLayout androID:layout_wIDth="0dp" androID:layout_height="match_parent" androID:layout_weight="0.25" androID:gravity="end" androID:orIEntation="vertical"> <Imagebutton androID:ID="@+ID/delete" androID:layout_wIDth="match_parent" androID:layout_height="30dp" androID:background="@androID:color/transparent" androID:contentDescription="Delete" app:srcCompat="@androID:drawable/ic_menu_delete" /> <Imagebutton androID:ID="@+ID/edit" androID:layout_wIDth="match_parent" androID:layout_height="30dp" androID:background="@androID:color/transparent" androID:contentDescription="Edit" app:srcCompat="@androID:drawable/ic_menu_edit" /> </linearLayout></linearLayout>
这是recyclervIEw的XML
<?xml version="1.0" enCoding="utf-8"?><androID.support.v7.Widget.RecyclerVIEw xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="wrap_content" androID:layout_height="match_parent" tools:context="com.pc.kanayel.runoutof.ShowlistofLists" androID:orIEntation="vertical" androID:ID="@+ID/recyclervIEwListas"></androID.support.v7.Widget.RecyclerVIEw>解决方法 您只需要向listadapter添加一些代码即可.它就在那里,您必须实现onClickListener.您的案例代码应如下所示.
您可以传递所需的任何参数.这取决于您想要实现的功能.这里我演示了传递列表中单击项目的顺序.
选项1(功率/性能效率)
那么,下面的代码究竟意味着什么呢?您已经创建了VIEwHolder并实现了OnClickListener.那是正确的.现在您需要将OnClickListener设置为两个按钮.但点击时这些按钮的作用是由我们在VIEwHolder中创建的界面定义的.
当应用程序运行时,RecyclerVIEw将创建尽可能多的VIEwHolders,因为它需要通过为每个视图者调用onCreateVIEwHolder()方法来填充可用屏幕和列表项.向上/向下滚动时,这些VIEwHolders将被重用.不调用OnCreateVIEwHolder(),仅调用onBindVIEwHolder()来更新vIEwholder的内容.下面的代码使得,当创建VIEwHolder时,它还将创建一个将由VIEwholder的OnClickListener使用的MyClickListener,并且当重新使用vIEwholder时,它将不会创建新的OnClickListener.这意味着我们的方法具有高效性.
选项2(效率不高)
您还可以在onBindVIEwHolder()中设置setonClickListener().但是,正如我在上面的段落中所提到的,每次滚动以更新vIEwholder的内容时都会调用此方法.现在它每次都会创建新的OnClickListener对象.虽然选项1在每个VIEwHolder上都有OnClickListener,但它会重用它们.
选项1的代码
public class listadapter extends RecyclerVIEw.Adapter<listadapter.VIEwHolder> { private LayoutInflater layoutInflater; protected Vector<List> Lists; public listadapter(Context context,Vector Lists) { layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); this.Lists = Lists; } @OverrIDe public VIEwHolder onCreateVIEwHolder(VIEwGroup parent,null); VIEwHolder = holder = new VIEwHolder(vIEw,new VIEwHolder.MyClickListener() { @OverrIDe public voID onEdit(int p) { // Implement your functionality for onEdit here } @OverrIDe public voID onDelete(int p) { // Implement your functionality for onDelete here } }); return holder; } @OverrIDe public voID onBindVIEwHolder(VIEwHolder holder,int position) { List List = Lists.elementAt(position); holder.name.setText(List.getname()); } @OverrIDe public int getItemCount() { return Lists.size(); } public static class VIEwHolder extends RecyclerVIEw.VIEwHolder implements VIEw.OnClickListener { MyClickListener Listener; TextVIEw name; button edit; button delete; public VIEwHolder(VIEw itemVIEw,MyClickListener Listener) { super(itemVIEw); name = (TextVIEw) itemVIEw.findVIEwByID(R.ID.Listname); edit = (button) itemVIEw.findVIEwByID(R.ID.edit); delete = (button) itemVIEw.findVIEwByID(R.ID.delete); this.Listener = Listener; edit.setonClickListener(this); delete.setonClickListener(this); } @OverrIDe public voID onClick(VIEw vIEw) { switch (vIEw.getID()) { case R.ID.edit: Listener.onEdit(this.getLayoutposition()); break; case R.ID.delete: Listener.onDelete(this.getLayoutposition()); break; default: break; } } public interface MyClickListener { voID onEdit(int p); voID onDelete(int p); } }}
编辑第二个问题
要使列表项占据所有宽度,请将recyclervIEw的宽度设置为match_parent.将它作为某种布局的子视图也更好.例如,如下所示.
<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" tools:context="com.pc.kanayel.runoutof.ShowlistofLists"> <androID.support.v7.Widget.RecyclerVIEw androID:ID="@+ID/recyclervIEwListas" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"/><relativeLayout>
并在适配器内更改此代码:
VIEw vIEw = layoutInflater.inflate(R.layout.listadapter,null);
对此:
VIEw vIEw = layoutInflater.inflate(R.layout.listadapter,parent,false);总结
以上是内存溢出为你收集整理的android – 如何将OnClickListener设置为RecyclerView中的两个按钮?全部内容,希望文章能够帮你解决android – 如何将OnClickListener设置为RecyclerView中的两个按钮?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)