如何更新当前不在屏幕上的回收者视图的特定位置的视图?

时间:2019-06-13 07:15:06

标签: android android-recyclerview

我实际上是对回收者视图中单击的项目进行一些可见性更改。但是,当用户单击一个对象,然后单击另一个对象时,前一个对象应进入其初始状态。 如果视图位于屏幕焦点上,则manager.findViewByPosition(position)工作正常,但如果元素不在当前焦点中,则无法获取视图。 例如:-用户单击第一个(位置)项目,然后单击最后一个位置,然后findViewByPosition返回null。

请帮助,让我知道是否还有其他方法可以做到。

预期结果应该是要刷新的最后一项的视图,但是对于不在屏幕当前焦点中的视图,则不会发生。

下面是我的代码段。更新了您的建议。

public class BodyPartWithMmtRecyclerView extends 
RecyclerView.Adapter<BodyPartWithMmtRecyclerView.ViewHolder>
{
   //variables defined. 
   int selectedPosition = -1;
   static class ViewHolder extends RecyclerView.ViewHolder {
   //All the view items declared here.
   ViewHolder(View view) {
        super(view);
  //All the views are defined here.
  }
} 
public BodyPartWithMmtRecyclerView(List<BodyPartWithMmtSelectionModel> bodyPartsList, Context context){
//array list initialization and shared preference variables initialization
}

public BodyPartWithMmtRecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    //Creating a new view.
}

 public void onBindViewHolder(@NonNull final BodyPartWithMmtRecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
BodyPartWithMmtSelectionModel bodyPartWithMmtSelectionModel = bodyPartsList.get(position);
    holder.iv_bodypart.setImageResource(bodyPartWithMmtSelectionModel.getIv_body_part());
    holder.tv_body_part_name.setText(bodyPartWithMmtSelectionModel.getExercise_name());

if(selectedPosition!=position && selectedPosition!=-1){
 //updated the elements view to default view. Like made the visibility and other changes here.           
    }

 //some click listeners on the sub-elements of the items. Like textviews, spinner, etc
holder.iv_bodypart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ((BodyPartSelection)context).setFabVisible();
            if(selectedPosition!=-1){
                ((BodyPartSelection)context).visibilityChanged(selectedPosition,position);
           /*here what I was doing is whenever the user clicks on an item I check weather a previous item is clicked or not then if yes then I send the position to a function that makes it to default but the issue was that if the item is not in the focus of the screen the findViewByPosition returns null.*/

            }
            selectedPosition = position;
            bodypartSelected = holder.tv_body_part_name.getText().toString();
            holder.iv_bodypart.setVisibility(View.INVISIBLE);
            holder.rl_left_right.setVisibility(View.VISIBLE);
        }
    });


   //and other listeners below 

}

 @Override
public int getItemCount() {
    return bodyPartsList==null?0:bodyPartsList.size();
}

@Override
public int getItemViewType(int position) {
    return position;
}

}

VisibilityChanged函数

public void visibilityChanged(int position, int clicked){

          View view = manager.findViewByPosition(position);
          if(view!=null) {
            Log.i("inside","visibility change");
            ImageView imageView = view.findViewById(R.id.bodypartImage);
            //other elements and changing the visibility of elemets to default.
           }
    }

2 个答案:

答案 0 :(得分:2)

我已根据您更新的代码段更新了代码。如果没有,请不要更改可见性条件,否则我添加了我在您的代码片段中看到的任何其他逻辑。就像您所做的那样,它不会同时更新选定视图和默认视图,因为RecyclerView重用了视图布局。因此,如果条件不合适,您可能会看到已选择多个项目或某些其他类型的无用行为。

    public void onBindViewHolder(@NonNull final BodyPartWithMmtRecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
    BodyPartWithMmtSelectionModel bodyPartWithMmtSelectionModel = bodyPartsList.get(position);
    holder.iv_bodypart.setImageResource(bodyPartWithMmtSelectionModel.getIv_body_part());
    holder.tv_body_part_name.setText(bodyPartWithMmtSelectionModel.getExercise_name());

    if(selectedPosition == position){
        //updated the elements view to SELECTED VIEW. Like made the visibility and other changes here.           
    } else {
        //updated the elements view to default view. Like made the visibility and other changes here.
    }

     //some click listeners on the sub-elements of the items. Like textviews, spinner, etc
    holder.iv_bodypart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ((BodyPartSelection)context).setFabVisible();


            /Comment by Hari: Don't try to change the visibility of default as it will be done automatically after calling notifyDataSetChanged(). */
            if(selectedPosition!=-1){
                ((BodyPartSelection)context).visibilityChanged(selectedPosition,position);
           /*here what I was doing is whenever the user clicks on an item I check weather a previous item is clicked or not then if yes then I send the position to a function that makes it to default but the issue was that if the item is not in the focus of the screen the findViewByPosition returns null.*/

           /*Comment by Hari: This snippet is valuable which is missing as you are getting null issue here.
           However Don't try to change the visibility of default as it will be done automatically after calling notifyDataSetChanged(). */

            }
            selectedPosition = position;
            bodypartSelected = holder.tv_body_part_name.getText().toString();
            holder.iv_bodypart.setVisibility(View.INVISIBLE);
            holder.rl_left_right.setVisibility(View.VISIBLE);

            //Keep this as last statement in onClick
            notifyDataSetChanged();
        }
    });

   //and other listeners below 

}

让我知道您的进一步答复。

答案 1 :(得分:0)

基于@Hari N Jha的答案。

更新任何内容时,请致电notifyDataSetChanged()。例如

    int selectedPosition = -1;

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        //....

        if(position == selectedPosition) {
            //Add background color change of your layout or as you want for selected item.
        } else {
            //Add background color change of your layout or as you want for default item.
        }
        notifyDataSetChanged(); //Call notifyDataSetChanged() here after done all the stufs
        //...
    }
相关问题