当前位置: 首页 > 知识库问答 >
问题:

RecyclerView父级中的RecyclerView。notifyDataSetChanged子回收器视图的松动位置

陆卓
2023-03-14

我在垂直回收视图中使用水平回收视图开发一个片段(像谷歌应用商店)。

当我从父循环视图notifyDataSetChanged时,子循环视图将失去位置,因为我认为setAdapter是在onBindViewHolder中调用的。此外,当我在第一个水平循环视图中滚动到第5个位置时,如果我向下滚动并返回,我会松开第5个位置。

我尝试使用RecyCL View.scrollToPosition(),但那不起作用。

所以我想我有两个解决方案:

>

一种手动设置回收视图位置到刷新前位置的方法。(其他解决方案)

这里我的父母适配器:

public class ProfilesCardViewListAdapter extends RecyclerView.Adapter<ProfilesCardViewListAdapter.ItemRowHolder> {
    private ArrayList<AidodysProfile> sectionsList;
    private Context context;
    private boolean[] isShown;
    private ProfileCardViewItemAdapter itemAdapters[];

    public ProfilesCardViewListAdapter(ArrayList<AidodysProfile> sectionsList, Context context) {

        this.sectionsList = sectionsList;
        this.context = context;
        this.isShown = new boolean[sectionsList.size()];
        this.itemAdapters = new ProfileCardViewItemAdapter[sectionsList.size()];
        for (int i = 0; i < sectionsList.size(); i++) {
            this.isShown[i] = true;
            this.itemAdapters[i] = new ProfileCardViewItemAdapter(sectionsList.get(i).getProfiles(), context, this);
        }
    }

    @Override
    public ItemRowHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_card_view_horizontal, null);
        final ItemRowHolder rowHolder = new ItemRowHolder(view);

        return rowHolder;
    }

    @Override
    public void onBindViewHolder(final ItemRowHolder holder, final int position) {
        String sectionName = sectionsList.get(position).getName();

        AidodysProfile[] sectionItems = sectionsList.get(position).getProfiles();

        holder.sectionTitle.setTextSize(context.getResources().getDimension(R.dimen.text_size_profileslist_section_title));
        holder.sectionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showHideSection(holder, position);
            }
        });

        if (isShown[position]) {
            holder.itemRecyclerView.setVisibility(View.VISIBLE);
            holder.sectionButton.setText(context.getString(R.string.action_profiles_section_hide));
        } else {
            holder.itemRecyclerView.setVisibility(View.GONE);
            holder.sectionButton.setText(context.getText(R.string.action_profiles_section_show));
        }

        if (!sectionsList.get(position).isLeaf()) { // FOLDER
            if (sectionName.equals("")) {
                holder.sectionTitle.setVisibility(View.GONE);
                holder.sectionButton.setVisibility(View.GONE);
            } else {
                holder.sectionTitle.setVisibility(View.VISIBLE);
                holder.sectionButton.setVisibility(View.VISIBLE);
            }
            holder.sectionTitle.setText(sectionName);
        } else { // PROFILE
            return;
        }

        holder.itemRecyclerView.setHasFixedSize(true);
        holder.itemRecyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
        holder.itemRecyclerView.setAdapter(itemAdapters[position]);

    }

    private void showHideSection(ItemRowHolder holder, int position) {
        if (holder.itemRecyclerView.getVisibility() == View.VISIBLE) {
            isShown[position] = false;
            holder.itemRecyclerView.setVisibility(View.GONE);
            holder.sectionButton.setText(context.getText(R.string.action_profiles_section_show));
        } else {
            isShown[position] = true;
            holder.itemRecyclerView.setVisibility(View.VISIBLE);
            holder.sectionButton.setText(context.getString(R.string.action_profiles_section_hide));
        }
    }

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

    public class ItemRowHolder extends RecyclerView.ViewHolder {

        protected TextView sectionTitle;
        protected RecyclerView itemRecyclerView;
        protected Button sectionButton;

        public ItemRowHolder(View view) {
            super(view);
            this.sectionTitle = (TextView) view.findViewById(R.id.section_title);
            this.itemRecyclerView = (RecyclerView) view.findViewById(R.id.item_recycler_view);
            this.sectionButton = (Button) view.findViewById(R.id.section_button);
        }

    }
}

子适配器:

class ProfileCardViewItemAdapter extends RecyclerView.Adapter<ProfileCardViewItemAdapter.SingleItemRowHolder> {
    private AidodysProfile[] itemsList;
    private CurrentUser currentUser;
    private Context context;
    private int selectedPos = -1;
    private ProfilesCardViewListAdapter parent;

    public ProfileCardViewItemAdapter(AidodysProfile[] itemsList, Context context, ProfilesCardViewListAdapter parent) {
        this.itemsList = itemsList;
        this.context = context;
        this.parent = parent;
        this.currentUser = CurrentUser.getInstance(context);
    }

    @Override
    public SingleItemRowHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_card_view_horizontal, null);
        SingleItemRowHolder rowHolder = new SingleItemRowHolder(view);
        return (rowHolder);
    }

    @Override
    public void onBindViewHolder(final SingleItemRowHolder holder, final int position) {
        AidodysProfile profile = itemsList[position];

        holder.itemCardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectProfile(holder, position);
            }
        });

        if (profile.equals(currentUser.getProfile())) {
            selectedPos = position;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                holder.itemCardView.setCardBackgroundColor(context.getColor(R.color.aidodysRed));
            } else {
                holder.itemCardView.setCardBackgroundColor(context.getResources().getColor(R.color.aidodysRed));
            }
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                holder.itemCardView.setCardBackgroundColor(context.getColor(R.color.white));
            } else {
                holder.itemCardView.setCardBackgroundColor(context.getResources().getColor(R.color.white));
            }
        }

        holder.itemTitle.setText(profile.getName());

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            holder.itemPicture.setImageDrawable(context.getDrawable(R.drawable.ic_sheet_smile_black_48dp));
            holder.button1.setImageDrawable(context.getDrawable(R.drawable.ic_edit_black_24dp));
            holder.button2.setImageDrawable(context.getDrawable(R.drawable.ic_look_profile));
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                holder.itemTitle.setTextAppearance(R.style.Aidodys_Text_ProfilesList_Item);
                holder.itemPicture.setColorFilter(context.getColor(R.color.white));
                holder.topParts.setBackgroundColor(context.getColor(R.color.aidodysRed));
            } else  {
                holder.itemTitle.setTextColor(context.getResources().getColor(R.color.white));
                holder.itemTitle.setTextSize(context.getResources().getDimension(R.dimen.text_size_profileslist_item));
                holder.itemPicture.setColorFilter(context.getResources().getColor(R.color.white));
                holder.topParts.setBackgroundColor(context.getResources().getColor(R.color.aidodysRed));
            }
        } else {
            holder.button1.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_edit_black_24dp));
            holder.button2.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_look_profile));
            holder.itemTitle.setTextColor(context.getResources().getColor(R.color.white));
            holder.itemTitle.setTextSize(context.getResources().getDimension(R.dimen.text_size_profileslist_item));
            holder.itemPicture.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_sheet_smile_black_48dp));
            holder.itemPicture.setColorFilter(context.getResources().getColor(R.color.white));
            holder.topParts.setBackgroundColor(context.getResources().getColor(R.color.aidodysRed));
        }
    }

    private void selectProfile(SingleItemRowHolder holder, int position) {
        SharedPreferences.Editor editor = context.getSharedPreferences("Aidodys", 0).edit();
        editor.putString("profile", new Gson().toJson(itemsList[position]));
        editor.apply();
        currentUser.setProfile(itemsList[position]);
        parent.notifyDataSetChanged();
        notifyDataSetChanged();
        ((RecyclerView)holder.itemCardView.getParent()).scrollToPosition(position);
        selectedPos = position;
    }

    @Override
    public int getItemCount() {
        return (itemsList != null ? itemsList.length : 0);
    }

    public class SingleItemRowHolder extends RecyclerView.ViewHolder {

        protected TextView itemTitle;
        protected ImageView itemPicture;
        protected CardView itemCardView;
        protected ImageView button1;
        protected ImageView button2;
        protected LinearLayout topParts;

        public SingleItemRowHolder(View view) {
            super(view);

            this.itemTitle = (TextView)view.findViewById(R.id.item_title);
            this.itemPicture = (ImageView)view.findViewById(R.id.item_picture);
            this.itemCardView = (CardView)view.findViewById(R.id.card_view_list_item);
            this.topParts = (LinearLayout)view.findViewById(R.id.card_view_list_item_top_part);
            this.button1 = (ImageView)view.findViewById(R.id.item_button_1);
            this.button2 = (ImageView)view.findViewById(R.id.item_button_2);
        }
    }
}

如果有人能帮我解决问题

非常感谢。

共有2个答案

逑翰翮
2023-03-14

我找到了一个解决方案,我使用onTouchListener在子回收视图中设置一个滚动位置(我不使用onScrollListener,因为这种方法不建议)我实现了一个getter到这个字段,从父回收视图中获取滚动位置,所以在onBindViewHolder的末尾在父回收站视图i调用(子)回收站View.scrollToPoplace(scrollPoplace)。

这就是我的工作

父RecycleServiceAdapter:

public class ProfilesCardViewListAdapter extends RecyclerView.Adapter<ProfilesCardViewListAdapter.ItemRowHolder> {
    private ArrayList<AidodysProfile> sectionsList;
    private Context context;
    private boolean[] isShown;
    private ProfileCardViewItemAdapter itemAdapters[];

    public ProfilesCardViewListAdapter(ArrayList<AidodysProfile> sectionsList, Context context) {

        this.sectionsList = sectionsList;
        this.context = context;
        this.isShown = new boolean[sectionsList.size()];
        this.itemAdapters = new ProfileCardViewItemAdapter[sectionsList.size()];
        for (int i = 0; i < sectionsList.size(); i++) {
            this.isShown[i] = true;
            this.itemAdapters[i] = new ProfileCardViewItemAdapter(sectionsList.get(i).getProfiles(), context, this);
        }
    }

    @Override
    public ItemRowHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_card_view_horizontal, null);
        final ItemRowHolder rowHolder = new ItemRowHolder(view);
        rowHolder.itemRecyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
        return rowHolder;
    }

    @Override
    public void onBindViewHolder(final ItemRowHolder holder, final int position) {
        String sectionName = sectionsList.get(position).getName();

        AidodysProfile[] sectionItems = sectionsList.get(position).getProfiles();


        holder.sectionTitle.setTextSize(context.getResources().getDimension(R.dimen.text_size_profileslist_section_title));
        holder.sectionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showHideSection(holder, position);
            }
        });

        if (isShown[position]) {
            holder.itemRecyclerView.setVisibility(View.VISIBLE);
            holder.sectionButton.setText(context.getString(R.string.action_profiles_section_hide));
        } else {
            holder.itemRecyclerView.setVisibility(View.GONE);
            holder.sectionButton.setText(context.getText(R.string.action_profiles_section_show));
        }

        if (!sectionsList.get(position).isLeaf()) { // FOLDER
            if (sectionName.equals("")) {
                holder.sectionTitle.setVisibility(View.GONE);
                holder.sectionButton.setVisibility(View.GONE);
            } else {
                holder.sectionTitle.setVisibility(View.VISIBLE);
                holder.sectionButton.setVisibility(View.VISIBLE);
            }
            holder.sectionTitle.setText(sectionName);
        } else { // PROFILE
            return;
        }

        holder.itemRecyclerView.setHasFixedSize(true);
        holder.itemRecyclerView.setAdapter(itemAdapters[position]);
        holder.itemRecyclerView.scrollToPosition(itemAdapters[position].getScrollPos());
    }

    private void showHideSection(ItemRowHolder holder, int position) {
        if (holder.itemRecyclerView.getVisibility() == View.VISIBLE) {
            isShown[position] = false;
            holder.itemRecyclerView.setVisibility(View.GONE);
            holder.sectionButton.setText(context.getText(R.string.action_profiles_section_show));
        } else {
            isShown[position] = true;
            holder.itemRecyclerView.setVisibility(View.VISIBLE);
            holder.sectionButton.setText(context.getString(R.string.action_profiles_section_hide));
        }
    }

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

    public class ItemRowHolder extends RecyclerView.ViewHolder {

        protected TextView sectionTitle;
        protected RecyclerView itemRecyclerView;
        protected Button sectionButton;

        public ItemRowHolder(View view) {
            super(view);
            this.sectionTitle = (TextView) view.findViewById(R.id.section_title);
            this.itemRecyclerView = (RecyclerView) view.findViewById(R.id.item_recycler_view);
            this.sectionButton = (Button) view.findViewById(R.id.section_button);
        }

    }
}

儿童回收ViewAdapter:

class ProfileCardViewItemAdapter extends RecyclerView.Adapter<ProfileCardViewItemAdapter.SingleItemRowHolder> {
    private AidodysProfile[] itemsList;
    private CurrentUser currentUser;
    private Context context;
    private int scrollPos = 0;
    private ProfilesCardViewListAdapter parent;

    public int getScrollPos() {
        return scrollPos;
    }

    public ProfileCardViewItemAdapter(AidodysProfile[] itemsList, Context context, ProfilesCardViewListAdapter parent) {
        this.itemsList = itemsList;
        this.context = context;
        this.parent = parent;
        this.currentUser = CurrentUser.getInstance(context);
    }

    @Override
    public SingleItemRowHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_card_view_horizontal, null);
        SingleItemRowHolder rowHolder = new SingleItemRowHolder(view);
        return (rowHolder);
    }

    @Override
    public void onBindViewHolder(final SingleItemRowHolder holder, final int position) {
        AidodysProfile profile = itemsList[position];

        holder.itemCardView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                scrollPos = position;
                return false;
            }
        });
        holder.itemCardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectProfile(holder, position);
            }
        });

        if (profile.getId() == currentUser.getProfile().getId()) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                holder.itemCardView.setCardBackgroundColor(context.getColor(R.color.aidodysRed));
            } else {
                holder.itemCardView.setCardBackgroundColor(context.getResources().getColor(R.color.aidodysRed));
            }
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                holder.itemCardView.setCardBackgroundColor(context.getColor(R.color.white));
            } else {
                holder.itemCardView.setCardBackgroundColor(context.getResources().getColor(R.color.white));
            }
        }

        holder.itemTitle.setText(profile.getName());

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            holder.itemPicture.setImageDrawable(context.getDrawable(R.drawable.ic_sheet_smile_black_48dp));
            holder.button1.setImageDrawable(context.getDrawable(R.drawable.ic_edit_black_24dp));
            holder.button2.setImageDrawable(context.getDrawable(R.drawable.ic_look_profile));
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                holder.itemTitle.setTextAppearance(R.style.Aidodys_Text_ProfilesList_Item);
                holder.itemPicture.setColorFilter(context.getColor(R.color.white));
                holder.topParts.setBackgroundColor(context.getColor(R.color.aidodysRed));
            } else  {
                holder.itemTitle.setTextColor(context.getResources().getColor(R.color.white));
                holder.itemTitle.setTextSize(context.getResources().getDimension(R.dimen.text_size_profileslist_item));
                holder.itemPicture.setColorFilter(context.getResources().getColor(R.color.white));
                holder.topParts.setBackgroundColor(context.getResources().getColor(R.color.aidodysRed));
            }
        } else {
            holder.button1.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_edit_black_24dp));
            holder.button2.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_look_profile));
            holder.itemTitle.setTextColor(context.getResources().getColor(R.color.white));
            holder.itemTitle.setTextSize(context.getResources().getDimension(R.dimen.text_size_profileslist_item));
            holder.itemPicture.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_sheet_smile_black_48dp));
            holder.itemPicture.setColorFilter(context.getResources().getColor(R.color.white));
            holder.topParts.setBackgroundColor(context.getResources().getColor(R.color.aidodysRed));
        }
    }

    private void selectProfile(SingleItemRowHolder holder, int position) {
        SharedPreferences.Editor editor = context.getSharedPreferences("Aidodys", 0).edit();
        editor.putString("profile", new Gson().toJson(itemsList[position]));
        editor.apply();
        currentUser.setProfile(itemsList[position]);
        parent.notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return (itemsList != null ? itemsList.length : 0);
    }

    public class SingleItemRowHolder extends RecyclerView.ViewHolder {

        protected TextView itemTitle;
        protected ImageView itemPicture;
        protected CardView itemCardView;
        protected ImageView button1;
        protected ImageView button2;
        protected LinearLayout topParts;

        public SingleItemRowHolder(View view) {
            super(view);

            this.itemTitle = (TextView)view.findViewById(R.id.item_title);
            this.itemPicture = (ImageView)view.findViewById(R.id.item_picture);
            this.itemCardView = (CardView)view.findViewById(R.id.card_view_list_item);
            this.topParts = (LinearLayout)view.findViewById(R.id.card_view_list_item_top_part);
            this.button1 = (ImageView)view.findViewById(R.id.item_button_1);
            this.button2 = (ImageView)view.findViewById(R.id.item_button_2);
        }
    }
}
益麻雀
2023-03-14

在SparseArray中存储x滚动偏移,以便在绑定视图保持架时定位和恢复。

public class ProfilesCardViewListAdapter extends RecyclerView.Adapter<ProfilesCardViewListAdapter.ItemRowHolder> {

    private SparseIntArray sparseArray = new SparseIntArray();

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

        // Use srollBy for animate scrolling
        holder.itemRecyclerView.srollBy(sparseArray.get(position, 0), 0);
        // Or scrollTo for restore previous x offset
        //holder.itemRecyclerView.srollTo(sparseArray.get(position, 0), 0);

        holder.itemRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {

           @Override
           public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
               sparseArray.put(position, dx);
           }
        }
    }
}
 类似资料:
  • 我有一个带有和的activity。我正在使用水平显示图像列表。当我单击中的图像时,activity中的将显示该图像的更大图片。到目前为止一切正常。 现在activity中又多了两个:和。当我单击时,中的图像应该向左转,并且中的缩略图应该反映这个变化。的情况也类似。 我可以旋转。但是,如何在中旋转缩略图呢?如何获取的? 代码: activity XML: 我的activity代码: 我得Recycl

  • 有人能帮我吗?我不知道为什么我会犯这个错误。

  • 我正在使用Recycler视图显示元素列表。当我们单击每一行时,每一行上都有一个按钮,状态改变,背景颜色改变。状态更新后,我将调用notifyDataSetChanged(),但recyclerView不会刷新。

  • 问题内容: 下面的代码是在第一个位置上查找视图。如何找到最后一个位置的视图? 这是我的xml 问题答案: 试试这个 试试这个 或尝试这个 只需执行以下操作即可:

  • 我已经做了一些研究,我还没有找到一个示例或实现,允许您在滑动时将一个视图(下面的示例图像)放在RecyclerView下面。是否有人有任何例子或想法,如何实现这将不使用库。 下面是我如何实现扫瞄以解除职务。

  • 我延长了 当我打电话的时候: 什么也没发生。 刷新视图的唯一方法是再次设置适配器(请参见此答案): 我对此解决方案有两个问题: 当我再次设置适配器时,我可以看到屏幕上有一个闪烁 listview返回第一个位置。 有什么想法吗?