android - 如何更新当前未在屏幕上聚焦的回收站 View 的特定位置的 View ?

标签 android android-recyclerview

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

如果有其他方法,请帮助并告诉我。

预期的结果应该是要刷新的最后一个项目的 View ,但对于不在屏幕当前焦点中的 View 则不会发生这种情况。

下面是我的代码 fragment 。根据您的建议进行了更新。

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.
           }
    }

最佳答案

我已经根据您更新的代码段更新了我的代码。请不要更改可见性条件,否则我添加了我在您的代码 fragment 中看到的任何不同逻辑。正如您所做的那样,它不会更新选定 View 和默认 View ,因为 RecyclerView 会重用 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){
        //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 

}

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

关于android - 如何更新当前未在屏幕上聚焦的回收站 View 的特定位置的 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56575190/

相关文章:

java - 如何将 BaseAdapter 转换为 RecyclerAdapter

android - Gridview 项目使用 RecyclerView 更改顺序

android - ArrayAdapter 和 ListView - ArrayIndexOutOfBoundsException

android - 在设备上的 Google 搜索中显示我的应用程序内容

android - Recyclerview 叠加工具栏

android - 无法使用 Realm 实时更新recyclerView

Android RecyclerView 项目序列问题

java - 为什么我收到 "The constructor Intent(Class<LivrmStartActivity>) is undefined"消息?

安卓代理设置

java - 在单个 Activity 中管理多个 fragment 的理想方法是什么?