如何添加按钮固定在RecyclerView适配器? [英] How to add fixed Button in RecyclerView Adapter?

查看:529
本文介绍了如何添加按钮固定在RecyclerView适配器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?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 的末尾加上一个固定的按钮,但每当我在上面添加按钮刚过的TextView code。它被表示在以下的方式:

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:

我知道为什么发生这种情况,但无法找出解决方案,使按钮酒吧。这里是我的适配器 code:

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);
        }
    }
   }

如何解决这个问题呢?

How to fix this problem?

推荐答案

构建Load更多按钮进入recyclerview。

做的第一件事就是与你的按钮创建布局和修改viewhold拿到按钮ID:

The first thing to do is to create a layout with your button and modify your viewhold to get the button id:

然后一个简单的方法,使一个按钮来添加+1到getItemCount()和标志(即布尔)知道,如果你需要绘制按钮。

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,您将有浮雕观看一个为标题,否则浮雕观看一个为负载更多按钮

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中的父母,INT viewType),Y

In the method onCreateViewHolder(ViewGroup parent, int viewType), y

@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>

不要忘记改变你的viewholder,包括新的按钮。

和最后的方法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什么!

Hope you can do what you want with the recyclerview !

这篇关于如何添加按钮固定在RecyclerView适配器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆