OnLongClickListener na recyclerview nieoczekiwanie zmienia wygląd layoutu

0

Dzień dobry, mam problem, z którym nie mogę uporać się już parę dni. Mam recyclerview, który składa się obecnie z 7 elementów, ale nas interesuje dalsze 5. Dodałem do recyclerview expandableLayout i kiedy rozwinę ten dodatkowy layout poprzez OnLongClickListener na elemencie 2,3 lub 5 i potem rozwinę na elemencie 1 lub 4 to znika nagłówek (sectionName) elementu odpowiednio 1 lub 4 co jest oczywiście zjawiskiem nieporządanym. Jednak kiedy nadal będe klikać na 1 lub 4, nagłówki wrócą na swoje miejsce, więc problem jest tylko podczas 1 kliknięcia po wcześniejszych kliknięciach na 2,3, lub 5. Bardzo proszę o rozwiązanie tego problemu. Jeśli jeszcze jakiś kod byłby potrzebny proszę pisać. Oto kluczowy kod:

public class Gra extends Activity {

    private List<RightDrawerMenu> rightMenu = new ArrayList<>();
    private RecyclerView rvRight;
    private RightDrawerAdapter2 adapter2;

    ...

    void configureRightDrawer() {

        rightMenu.addAll(Arrays.asList(
                null,
                new RightDrawerMenu.Builder("Health potion", onlineGame ? 0 : pref.getInt("hpPotionCount", 0)).icon(R.mipmap.hpotion1).sectionName("Items").build(),
                new RightDrawerMenu.Builder("Mana potion", onlineGame ? 0 : pref.getInt("mpPotionCount", 0)).icon(R.mipmap.mpotion1).build(),
                new RightDrawerMenu.Builder("user1", -1).sectionName("Followed").build(),
                new RightDrawerMenu.Builder("user2", -1).build(),
                new RightDrawerMenu.Builder("user3", -1).build(),
                new RightDrawerMenu.Builder("user4", -1).sectionName("Cooperation").build(),
                new RightDrawerMenu.Builder("user5", -1).build()));

        adapter2 = new RightDrawerAdapter2(this, rightMenu, dLayout, this, onlineGame);
        rvRight.setAdapter(adapter2);
    }
public class RightDrawerMenu {

    String sectionName, title, subitle, subtitle2;
    int amount, icon;
    boolean expanded;

    public void setSectionName(String sectionName) {
        this.sectionName = sectionName;
    }

    public boolean isExpanded() {
        return expanded;
    }

    public void setExpanded(boolean expanded) {
        this.expanded = expanded;
    }

    public String getSubitle() {
        return subitle;
    }

    public void setSubitle(String subitle) {
        this.subitle = subitle;
    }

    public String getSubtitle2() {
        return subtitle2;
    }

    public void setSubtitle2(String subtitle2) {
        this.subtitle2 = subtitle2;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

    public String getSectionName() {
        return sectionName;
    }

    public String getTitle() {
        return title;
    }

    public int getIcon() {
        return icon;
    }

    public int getAmount() {
        return amount;
    }


    public static class Builder {

        private final String title;
        private int amount;
        boolean expanded;
        private int icon;
        private String sectionName;

        public Builder(String title, int amount) {
            this.title = title;
            this.amount = amount;
            this.expanded = false;
        }

        public Builder icon(int icon) {
            this.icon = icon;
            return this;
        }

        public Builder sectionName(String sectionName) {
            this.sectionName = sectionName;
            return this;
        }

        public RightDrawerMenu build() {
            return new RightDrawerMenu(this);
        }
    }

    public RightDrawerMenu(Builder builder) {
        this.title = builder.title;
        this.icon = builder.icon;
        this.amount = builder.amount;
        this.sectionName = builder.sectionName;
    }

    @Override
    public String toString() {
        return "Builder{" +
                "title='" + title + '\'' +
                ", amount=" + amount +
                ", expanded=" + expanded +
                ", icon=" + icon +
                ", sectionName='" + sectionName + '\'' +
                '}';
    }
}


public class RightDrawerAdapter2 extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_HEADER = 0;
    private static final int TYPE_ITEM = 1;

    private final SharedPreferences pref;
    private final SharedPreferences.Editor editor;
    private final DBHelper dbHelper;
    Context context;
    List<RightDrawerMenu> menuList;
    DrawerLayout drawerLayout;
    public TextView tvMode, tvHpMax, tvHpAkt, tvMpMax, tvMpAkt, tvName, tvNameValue, tvLvl, tvExp, tvExpRequired;
    public RightDrawerVHHeader header;
    public RightRecyclerViewAadapterListener recyclerViewAadapterListener;
    private boolean onlineGame;

    public RightDrawerAdapter2(Context context, List<RightDrawerMenu> menuList, DrawerLayout drawerLayout, RightRecyclerViewAadapterListener recyclerViewAadapterListener, boolean onlineGame) {
        this.context = context;
        this.menuList = menuList;
        this.drawerLayout = drawerLayout;
        this.recyclerViewAadapterListener = recyclerViewAadapterListener;
        this.onlineGame = onlineGame;
        dbHelper = new DBHelper(context);
        dbHelper.getReadableDatabase();
        pref = PreferenceManager.getDefaultSharedPreferences(context);
        editor = pref.edit();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_HEADER) {

            ...

        } else if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_right_item, parent, false);
            return new RightDrawerVHItems(view);
        }
        throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
    }


    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof RightDrawerVHItems) {
            boolean isExpanded = menuList.get(position).isExpanded();
            ((RightDrawerVHItems) holder).expandableLayout.setVisibility(isExpanded ? View.VISIBLE : View.GONE);
            if (menuList.get(position).getIcon() != 0)
                ((RightDrawerVHItems) holder).ivIcon.setImageResource(menuList.get(position).getIcon());
            else ((RightDrawerVHItems) holder).ivIcon.setVisibility(View.GONE);
            if (menuList.get(position).getAmount() == -1)
                ((RightDrawerVHItems) holder).tvAmount.setVisibility(View.GONE);
            if (menuList.get(position).sectionName != null)
                ((RightDrawerVHItems) holder).tvSectionName.setText(menuList.get(position).sectionName);
            else {
                ((RightDrawerVHItems) holder).tvSectionName.setVisibility(View.GONE);
                Toast.makeText(context, "chuj", Toast.LENGTH_SHORT).show();
            }

            ((RightDrawerVHItems) holder).tvTitle.setText(String.valueOf(menuList.get(position).getTitle()));
            ((RightDrawerVHItems) holder).linearLayout.setOnLongClickListener(v -> {

                if (menuList.get(position).getTitle().equals("Health potion"))
                    Toast.makeText(context, "Health potion heals 50HP", Toast.LENGTH_LONG).show();
                if (menuList.get(position).getTitle().equals("Mana potion"))
                    Toast.makeText(context, "Mana potion heals 5MP", Toast.LENGTH_LONG).show();

                if (!menuList.get(position).getTitle().equals("Health potion") && !menuList.get(position).getTitle().equals("Mana potion")) {     
                    menuList.get(position).setExpanded(!menuList.get(position).isExpanded());
                    if (menuList.get(position).sectionName != null)
                        ((RightDrawerVHItems) holder).tvSectionName.setVisibility(View.VISIBLE);
                    notifyItemChanged(position);
                }
                return true;
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_HEADER;
        }
        return TYPE_ITEM;
    }

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


    public static class RightDrawerVHHeader extends RecyclerView.ViewHolder {

        ...
    }

    public static class RightDrawerVHItems extends RecyclerView.ViewHolder {

        LinearLayout linearLayout;
        RelativeLayout expandableLayout;
        ImageView ivIcon;
        TextView tvSectionName, tvTitle, tvAmount, tvSubtitle, tvSubtitle2;

        public RightDrawerVHItems(@NonNull View itemView) {
            super(itemView);

            linearLayout = itemView.findViewById(R.id.ll);
            expandableLayout = itemView.findViewById(R.id.expandableLayout);
            ivIcon = itemView.findViewById(R.id.ivIcon);
            tvSectionName = itemView.findViewById(R.id.tvSectionName);
            tvTitle = itemView.findViewById(R.id.tvTitle);
            tvAmount = itemView.findViewById(R.id.tvAmount);
            tvSubtitle = itemView.findViewById(R.id.tvSubtitle);
            tvSubtitle2 = itemView.findViewById(R.id.tvSubtitle2);


        }
    }
}
0

W RightDrawerAdapter2 musiałem zmienić ten fragment kodu i działa prawie dobrze. Do clean codu chyba mu brakuje ale serio próbowałem uprościć kod to program się buntuje ;) :

                if (!menuList.get(position).getTitle().equals("Health potion") && !menuList.get(position).getTitle().equals("Mana potion")) {
                    if (menuList.get(position).sectionName != null)
                        ((RightDrawerVHItems) holder).tvSectionName.setVisibility(View.VISIBLE);

                   if(!menuList.get(position).isExpanded())
                       ((RightDrawerVHItems) holder).tvSectionName.setVisibility(View.VISIBLE);
                    if(menuList.get(position).isExpanded())
                        ((RightDrawerVHItems) holder).tvSectionName.setVisibility(View.VISIBLE);
                    menuList.get(position).setExpanded(!menuList.get(position).isExpanded());

                    notifyItemChanged(position);
                }
0

@Kubaz: :) Dupa debugging at finest.

Po co Ci w RightDrawerAdapter2 pola onlineGame, drawerLayout oraz dbHelper?

//Edit: ach dobra Ty masz tam jeszcze header gdzie może być to używane. I tak nie powinno.

1 użytkowników online, w tym zalogowanych: 0, gości: 1