获取ViewHolder的具体位置 - RecyclerView

时间:2017-03-20 14:48:20

标签: java android nullpointerexception android-recyclerview

我需要获取特定位置的视图,但我收到此错误:

  

java.lang.NullPointerException:尝试从字段读取   “android.view.View   在null上的android.support.v7.widget.RecyclerView $ ViewHolder.itemView'   对象参考

这是我的适配器:

public class ConciergeAdapter extends RecyclerView.Adapter<ConciergeAdapter.MyViewHolder> {

    private Context context;
    private ArrayList<String> items;

    public class MyViewHolder extends RecyclerView.ViewHolder
    {
        public TextView text;
        public RelativeLayout maincon;

        public MyViewHolder(View view) {
            super(view);
            this.text = (TextView) view.findViewById(R.id.text);
            this.maincon = (RelativeLayout) view.findViewById(R.id.maincon);
        }
    }

    public ConciergeAdapter(Context context, ArrayList<String> items)
    {
        this.context = context;
        this.items = items;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.fragment_concierge_top_items, parent, false);

        return new MyViewHolder(itemView);
    }


    @Override
    public void onBindViewHolder(ConciergeAdapter.MyViewHolder holder, int position)
    {
        Random rnd = new Random();
        int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
        holder.maincon.setBackgroundColor(color);
    }

    @Override
    public int getItemCount() {
        return items.size();
    }
}

这是我调用适配器并尝试获取ViewHolder的代码:

final LinearLayoutManager layoutManager= new LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL, false);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(layoutManager);

        ArrayList<String> itemslist = new ArrayList<String>();
        itemslist.add("1");
        itemslist.add("1");
        itemslist.add("1");
        itemslist.add("1");
        itemslist.add("1");
        itemslist.add("1");
        itemslist.add("1");
        itemslist.add("1");
        itemslist.add("1");
        itemslist.add("1");
        itemslist.add("1");

        ConciergeAdapter adapter = new ConciergeAdapter(getActivity(),itemslist);
        mRecyclerView.setAdapter(adapter);

        //RecyclerView.ViewHolder viewholder =  mRecyclerView.findViewHolderForAdapterPosition(1); 

        View holder = layoutManager.getChildAt(0);

        RelativeLayout middle = (RelativeLayout) holder.findViewById(R.id.maincon);
        middle.getLayoutParams().height = 200;

2 个答案:

答案 0 :(得分:0)

这是因为尚未填充RecyclerView。我建议你在适配器的onBindViewHolder内进行预期的布局操作:

 @Override
public void onBindViewHolder(ConciergeAdapter.MyViewHolder holder, int position) {
    if (position == 0) { 
        RelativeLayout middle = (RelativeLayout) holder.container(R.id.maincon);
        middle.getLayoutParams().height = 200;
    }

    Random rnd = new Random();
    int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
    holder.maincon.setBackgroundColor(color);
}

您的MyViewHolder

public class MyViewHolder extends RecyclerView.ViewHolder {
    public View container;
    public TextView text;
    public RelativeLayout maincon;

    public MyViewHolder(View view) {
        super(view);
        container = view;
        this.text = (TextView) view.findViewById(R.id.text);
        this.maincon = (RelativeLayout) view.findViewById(R.id.maincon);
    }
}

答案 1 :(得分:-2)

我的想法是将ViewHolders存储在数组中,因为它们出现在适配器中。

尝试像这样扩展RecyclerView.Adapter:

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<...> {

 ...
 // array of ViewHolders at specific positions
 private ViewHolder[] viewHolders;

 // constructor
 public MyRecyclerViewAdapter(List items) {
     ...
     viewHolders = new ViewHolder[items.size()];
     ...
 }

 @Override
 public void onBindViewHolder(final ViewHolder holder, int position) {
     ...
     viewHolders[position] = holder;
     ...
 }

 // call this function from outside of the adapter
 public ViewHolder getViewHolder(int position) {
    return viewHolders[position];
 }
}