据onCreateViewHolder方法中的位置在Recycl

据onCreateViewHolder方法中的位置在Recycl

本文介绍了如何根据onCreateViewHolder方法中的位置在RecyclerView中膨胀不同的布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将ListView重构为RecyclerView,但我陷入了这种情况.

I am trying to refactor my ListView to RecyclerView and I stuck in this situation.

@Override
public int getItemViewType(int position) {
    if (mDecommissionMode && position == getCount() - 1)
        return 1;
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (getItemViewType(position) == 0)
        return getNormalView(position, convertView, parent);
    else
        return getManualDecommissionView(convertView, parent);

}

我不知道如何将其应用于RecyclerView适配器,因为我无权访问onCreateViewHolder方法中的位置.

I don't have an idea how can I apply this to my RecyclerView adapter because I don't have access to the position in onCreateViewHolder method, where the layout should be inflated.

@Override
public DevicesItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    DevicesRecyclerViewAdapter.DevicesItemViewHolder holder = new DevicesItemViewHolder(); //should pass some inflated view to ViewHolder instance
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mOnClickListener.onClick(view);
        }
    });
    return holder;
}


@Override
public void onBindViewHolder(DevicesItemViewHolder holder, int position) {


}

有办法应对吗?

推荐答案

为两个布局创建两个视图支架

遵循这三个简单步骤

Create Two View Holders for two Layouts

Follow these three simple steps

  • 覆盖getItemViewType(int position)

根据中的ViewType返回不同​​的ViewHoldersonCreateViewHolder()方法

Return different ViewHolders based on the ViewType inonCreateViewHolder() method

基于onBindViewHolder()中的itemViewType填充视图方法(这解决了您面临的位置问题)

Populate View based on the itemViewType in onBindViewHolder()method (This solves the position problem you are facing)

这是RecyclerView Adapter的一个小代码段
您可以查看完整的代码以供参考这里

Here is a small code snippet for RecyclerView Adapter
You can see the full code for reference Here

public class YourListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private static final int LAYOUT_ONE= 0;
private static final int LAYOUT_TWO= 1;

@Override
public int getItemViewType(int position)
{
    if(position==0)
        return LAYOUT_ONE;
    else
        return LAYOUT_TWO;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


    View view =null;
    RecyclerView.ViewHolder viewHolder = null;

    if(viewType==LAYOUT_ONE)
    {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.one,parent,false);
        viewHolder = new ViewHolderOne(view);
    }
    else
    {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.two,parent,false);
        viewHolder= new ViewHolderTwo(view);
    }

    return viewHolder;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {


    if(holder.getItemViewType()== LAYOUT_ONE)
    {

    }
    else {

        ViewHolderOne vaultItemHolder = (ViewHolderOne) holder;
        vaultItemHolder.name.setText(displayText);
        vaultItemHolder.name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                .......
            }
        });

    }


}



public class ViewHolderOne extends RecyclerView.ViewHolder {

    public TextView name;

    public ViewHolderOne(View itemView) {
        super(itemView);
        name = (TextView)itemView.findViewById(R.id.displayName);

    }
}

public class ViewHolderTwo extends RecyclerView.ViewHolder{

    public ViewHolderTwo(View itemView) {
        super(itemView);

        ..... Do something
    }
}
}

这篇关于如何根据onCreateViewHolder方法中的位置在RecyclerView中膨胀不同的布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:11