如何在 RecyclerView Adapter 中添加固定按钮? [英] How to add fixed Button in RecyclerView Adapter?
问题描述
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true">
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="30dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:textSize="15dp"
android:textStyle="bold"
android:focusable="true"
android:outlineProvider="bounds" />
</RelativeLayout>
现在我想在 nav_draw_row
的末尾添加一个固定的 Button
但是每当我在 TextView
之后在上面的代码中添加按钮时.它以以下方式显示:
Now I want to add a fixed Button
in the end of nav_draw_row
but whenever I am adding button in above code just after TextView
. It is showing in following way:
我知道为什么会发生这种情况,但无法找到在 Bar
末尾修复 Button
的解决方案.这是我的 Adapter
代码:
I am aware why this is happening but not able to find out the solution to make Button
fix in the end of Bar
. Here is my Adapter
code:
public class NavigationDrawerAdapter extends RecyclerView.Adapter<NavigationDrawerAdapter.MyViewHolder> {
List<NavDrawerItem> data = Collections.emptyList();
private LayoutInflater inflater;
private Context context;
public NavigationDrawerAdapter(Context context, List<NavDrawerItem> data) {
this.context = context;
inflater = LayoutInflater.from(context);
this.data = data;
}
public void delete(int position) {
data.remove(position);
notifyItemRemoved(position);
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.nav_drawer_row, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
NavDrawerItem current = data.get(position);
holder.title.setText(current.getTitle());
}
@Override
public int getItemCount() {
return data.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView title;
public MyViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.title);
}
}
}
如何解决这个问题?
推荐答案
在回收视图中构建加载更多按钮.
要做的第一件事是使用您的按钮创建布局并修改您的视点以获取按钮 ID:
The first thing to do is to create a layout with your button and modify your viewhold to get the button id:
然后制作按钮的一个简单方法是在 getItemCount() 和一个标志(即布尔值)中添加 +1 以了解是否需要绘制按钮.
Then a simple way to make a button is to add +1 to the getItemCount() and a flag (i.e. boolean) to know if you need to draw the button.
带有标志的新 getItemCount 将如下所示:
The new getItemCount with the flag will look like this:
private boolean hasLoadButton = true;
public boolean isHasLoadButton() {
return hasLoadButton;
}
public void setHasLoadButton(boolean hasLoadButton) {
this.hasLoadButton = hasLoadButton;
notifyDataSetChanged();
}
@Override
public int getItemCount() {
if (hasLoadButton) {
return data.size() + 1;
} else {
return data.size();
}
}
然后你必须在适配器中重写获取视图类型的方法:
Then you have to override into the adapter the method to get the type of the view:
private final int TITLE = 0;
private final int LOAD_MORE = 1;
@Override
public int getItemViewType(int position) {
if (position < getItemCount()) {
return TITLE;
} else {
return LOAD_MORE;
}
}
如果位置在 0 和 data.size()-1 之间,您将拥有标题的 typeView,否则加载更多按钮的 typeview.
If the position is between 0 and data.size()-1, you will have a typeView for the title, else a typeview for the load more button.
现在我们必须创建视图持有者:
Now we have to create the view holder:
在方法 onCreateViewHolder(ViewGroup parent, int viewType) 中,
In the method onCreateViewHolder(ViewGroup parent, int viewType),
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TITLE) {
return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.nav_draw_row, parent, false));
} else if (view type == LOAD_MORE) {
return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.load_more_row, parent, false));
} else {
return null;
}
}
load_more_row
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<Button
android:id="@+id/load_more"
android:text="load more !"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
不要忘记更改您的视图以包含新按钮.
最后是方法 onBindViewHolder :
And finally the method onBindViewHolder :
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
if(position >= getItemCount()) {
holder.loadMore....
} else {
NavDrawerItem current = data.get(position);
holder.title.setText(current.getTitle());
}
}
这篇关于如何在 RecyclerView Adapter 中添加固定按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!