动态地将视图添加到RecyclerView的项目 [英] Add a view dynamically to the item of the RecyclerView

查看:65
本文介绍了动态地将视图添加到RecyclerView的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据发送到RecyclerView.Adapter

示例:

我有一个带isAddText变量的模态对象.在onCreateViewHolder中,将发送itemView作为参数来创建Holder对象.

I had a modal object with isAddText variable. In onCreateViewHolder will create the Holder Object with sending the itemView as an parameter.

@Override
public SolventViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {
    View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.solvent_list, null);
    SolventViewHolders rcv = new SolventViewHolders(layoutView);
    return rcv;
}

SolventViewHolders.java

SolventViewHolders.java

public SolventViewHolders(View itemView) {
    super(itemView);
    itemView.setOnClickListener(this);
    textView = (TextView) itemView.findViewById(R.id.country_name);
    imageView = (ImageView) itemView.findViewById(R.id.country_photo);
    //  Now i need add a view to the itemView parent based on ModalObject isAddView property and how to do that. 
}

推荐答案

问题

通常,您需要执行itemView.addView(view,layoutParams)以编程方式向布局中添加另一个视图,但是使用ViewHolders的事情是,RecyclerView会重用它们以绑定适配器中的其他项目,因此您可以否则,否则当另一个项再次绑定到此viewHolder实例时,您希望添加的新TextView将出现.

The problem

Normally, you would need to execute itemView.addView(view, layoutParams) to add another view programatically to your layout, but thing with ViewHolders is, they will be reused by RecyclerView to bind different items from the adapter, so you can't do that or this new TextView you wish to add will appear when another item is bound to this viewHolder instance again.

我可以想到几种实现目标的方法:

I can think of a few ways you can achieve your goal:

第一种方法是将这个textView添加到您的布局中,并且默认情况下默认为GONE,并且在绑定需要此TextView的一个项目时将其设置为VISIBLE,例如:

The first way is to add this textView to your layout and have its visibility by GONE by default, and set it to VISIBLE when binding that one item you need this TextView for, such as:

@Override
public void onBindViewHolder(AbstractViewHolder holder, int position) {
    holder.extraTextView.setVisibility(View.GONE);
    if(shouldShowExtraTextView) {
        holder.extraTextView.setVisibility(View.VISIBLE);
        holder.extraTextView.setText(/* your text */);
    }
    // proceed to bind item to holder
}


2.实施不同的ViewHolders

第二种方法更优雅,它涉及创建不同的ViewHolder类型.首先,您需要为两种类型的视图(由RecyclerView显示)创建int常量.在您的适配器中放置以下代码:


2. Implement different ViewHolders

The second way is more elegant, it involves creating different ViewHolder type. First you need to create int constants for the two types of view to be shown by RecyclerView. Place in your adapter the following code:

private static final int VIEW_ORDINARY = 0;
private static final int VIEW_WITH_EXTRA_TEXT_VIEW = 1;

然后实现适配器的getItemViewType(int position)方法:

Then implement the getItemViewType(int position) method of the adapter:

@Override
public int getItemViewType(int position) {
    if(position == /* position of item that needs extra text view */) {
        return VIEW_WITH_EXTRA_TEXT_VIEW;
    } else {
        return VIEW_ORDINARY;
    }
}

下一步是根据返回的类型创建不同的Holder:

Next step is to create different Holder depending on returned type:

@Override
public SolventViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if(viewType == VIEW_WITH_EXTRA_TEXT_VIEW) {
        View itemView = LayoutInflater.from(parent.getContext())
                                      .inflate(R.layout.solvent_list_with_extra_text_view, parent, false);
        return new SolventViewHolderWithExtraTextView(itemView);
    } else {
        View itemView = LayoutInflater.from(parent.getContext())
                                      .inflate(R.layout.solvent_list, parent, false);
        return new SolventViewHolder(itemView);
    }
}

当然,您需要定义SolventViewHolderWithExtraTextView:

Of course you need to define SolventViewHolderWithExtraTextView:

class SolventViewHolderWithExtraTextView extends SolventViewHolder {
    TextView extraTextView;

    public SolventViewHolderWithExtraTextView(View itemView) {
        super(itemView);
        this.extraTextView = (TextView) findViewById(R.id.extra_text_view);
    }
}

然后在onBindViewHolder()中:

And then in onBindViewHolder():

@Override
public void onBindViewHolder(SolventViewHolder holder, int position) {
    // you can use inheritance to handle binding logic or check the item view type again:
    // bind ordinary view
    if(getItemViewType(position)) {
        // bind the extra textView
        ((SolventViewHolderWithExtraTextView)holder).extraTextView.setText("I hope this works");
    }
}

这样,对于需要另一个TextView的项目,您可以使用另一种ViewHolder,它将在该类别中重复使用.这是做您想做的事的首选方法.

This way you have another type of ViewHolder for your items that need another TextView, and it will be reused within that category. It's a preferred way to do what you want to.

另一个答案(如果您问我,这确实是个丑陋的解决方案)是动态添加和删除视图,例如:

Another answer (which is really ugly solution if you ask me) is to add and remove view dynamically, such as:

@Override
public void onBindViewHolder(AbstractViewHolder holder, int position) {
    if(shouldDisplayExtraTextView) {
        if(!holder.displaysExtraTextView() {
            holder.addExtraTextView();
        }
        holder.extraTextView.setText("This solution is really ugly");
        } else {
        if(holder.displaysExtraTextView() {
            holder.removeExtraTextView();
        }
    }
}

class SolventViewHolders extends RecyclerView.ViewHolder {
    TextView extraTextView;
    boolean viewAdded = false;

    public SolventViewHolders(View itemView) {
        super(itemView);
        itemView.setOnClickListener(this);
        textView = (TextView) itemView.findViewById(R.id.country_name);
        imageView = (ImageView) itemView.findViewById(R.id.country_photo);
         //  Now i need add a view to the itemView parent based on ModalObject isAddView property and how to do that. 

        extraTextView = new TextView();
}

public void addExtraTextView() {
    ((ViewGroup)itemView).addView(extraTextView, layoutParams);
    viewAdded = true;
}

public void removeExtraTextView() {
    ((ViewGroup)itemView).removeView(extraTextView);
    viewAdded = false;
}

public boolean displaysExtraTextView() {
    return viewAdded;
}

这确实很丑陋,因为要将视图添加到itemView中,您需要将其强制转换为ViewGroup(不必强制如此),因此请确保它始终是ViewGroup或其子类.另外,您需要提供适当的layoutParams来将extraTextView添加到itemView.

This is really ugly, because to add view to itemView you need to cast it to ViewGroup (which it doesn't have to be), so ensure that it always will be a ViewGroup or its subclass. Also, you need to provide proper layoutParams to add the extraTextView to itemView.

这篇关于动态地将视图添加到RecyclerView的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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