As shown in figure, after set up good recycleView, I want to dynamically set the background of each itemView, but use getchildat has access to the null? What's the reason
CodePudding user response:
https://blog.csdn.net/yanxiangxue/article/details/80974416
CodePudding user response:
Add the following code in your adapter:
//preserve corresponding position in the map the viewholder
Private Map viewHolderMap=new HashMap<> (a);
Public Map getViewHolderMap () {
Return viewHolderMap;
}
@ Override
Public void onBindViewHolder (RecyclerView ViewHolder holder, int position) {
ViewHolderMap. Put (the position holder);
}
Where the need to set the background
Adapter. GetViewHolderMap (). The get (position). ItemView (controls) to set up the background
CodePudding user response:
refer to the second floor without the fucker said reply: add the following code in your adapter: //preserve corresponding position in the map the viewholder Private Map viewHolderMap=new HashMap<> (a); Public Map getViewHolderMap () { Return viewHolderMap; } @ Override Public void onBindViewHolder (RecyclerView ViewHolder holder, int position) { ViewHolderMap. Put (the position holder); } Where the need to set the background Adapter. GetViewHolderMap (). The get (position). ItemView (controls) to set up the background The method I tried before it is no use wow, through the adapter. After setadaper getxxx or null, but inside the adapter map is to have value, is this why? CodePudding user response:
Simply want to vomiting blood, RecyclerView and his adapter are reusable controls, why do you always think about getChild? You directly in the adapter onBind according to the position change background CodePudding user response:
Thinking more QingQi, using onBindView to set the background in the adapter CodePudding user response:
Thinking QingQi 666 4 l interpretation in place CodePudding user response:
refer to the second floor without the fucker said reply: add the following code in your adapter: //preserve corresponding position in the map the viewholder Private Map viewHolderMap=new HashMap<> (a); Public Map getViewHolderMap () { Return viewHolderMap; } @ Override Public void onBindViewHolder (RecyclerView ViewHolder holder, int position) { ViewHolderMap. Put (the position holder); } Where the need to set the background Adapter. GetViewHolderMap (). The get (position). ItemView (controls) to set up the background No problem, this method can get all the ViewHolder