hanggrian / recyclerview-expandable

RecyclerView implementation of traex's ExpandableLayout
Apache License 2.0
70 stars 13 forks source link

onClickListener only calls once #2

Closed DavidBarbaran closed 4 years ago

DavidBarbaran commented 7 years ago

I wanted to implement a listener in your code to perform an action when the content is displayed, and making use of the log, I realized that the onclicklistener in ExpandableRecyclerView is only called once, it can not do an action when the content is hidden and only when it shows Is there any way to do this? That is to say perform a certain action when it is displayed and hidden, in my case I am trying to make a notifyDataSetChanged () making use of this listener that implements.

Your code and my listener implementation

`

    public void onBindViewHolder(final VH holder, int position) {

      ExpandableItem expandableItem = (ExpandableItem) holder.itemView.findViewWithTag(ExpandableItem.TAG);
        if (expandableItem == null) {
            throw new RuntimeException("Item of this adapter must contain ExpandableItem!");
        }
        expandableItem.onItemExpand = onItemExpand;
        expandableItem.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("element ","click");
                index = holder.getLayoutPosition();
                currentPosition = holder.getLayoutPosition();
                for (int index = 0; index < lm.getChildCount(); ++index) {
                    if (index != (currentPosition - lm.findFirstVisibleItemPosition())) {
                        ExpandableItem currentExpandableItem = (ExpandableItem) lm.getChildAt(index).findViewWithTag(ExpandableItem.TAG);
                        currentExpandableItem.hide();
                    }
                }

                ExpandableItem expandableItem = (ExpandableItem) lm.getChildAt(currentPosition - lm.findFirstVisibleItemPosition()).findViewWithTag(ExpandableItem.TAG);

                if (expandableItem.isOpened()) {
                    expandableItem.hide();

                } else {
                    expandableItem.show();

                }
                // interface
                onItemExpand.onClick(holder.getLayoutPosition());
            }
        });
        if (currentPosition != position && expandableItem.isOpened()) {
            expandableItem.hideNow();
        } else if (currentPosition == position && !expandableItem.isOpened() && !expandableItem.isClosedByUser()) {
            expandableItem.showNow();
        }
    }

`

My code onBindViewHolder in Adapter

`

public void onBindViewHolder(final UniversityViewHolder holder, int i) {
   // Log.e("p. ","" + i);

    super.onBindViewHolder(holder, i);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        holder.titleTextView.setTransitionName("trans_text" + i);
    }

    holder.titleTextView.setText(universityList.get(i).getName());
    // holder.placeRecycler.setHasFixedSize(true);
    holder.placeRecycler.setLayoutManager(new LinearLayoutManager(mContext));
    PlaceAdapter adapter = new PlaceAdapter(mContext, universityList.get(i).getPlace(), holder.titleTextView);
    adapter.setOnPlaceClickListener(onPlaceClickListener);
    holder.placeRecycler.setAdapter(adapter);
    if(statusColor != 0) {
        if (universityList.get(i).isStatus()) {
            holder.titleTextView.setTextColor(ContextCompat.getColor(mContext, R.color.lead));
        }
        else{
            holder.titleTextView.setTextColor(ContextCompat.getColor(mContext, R.color.gray));

        }
    }
    setOnItemExpand(new OnItemExpand() {
        @Override
        public void onClick(int position) {
            Log.e("click: ","" + position);
            if (!universityList.get(position).isStatus()) {
                statusColor = 1;
                universityList.get(position).setStatus(true);
                for (University u:universityList){
                    if(u!=universityList.get(position)){
                        u.setStatus(false);
                    }
                }
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(251);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        Log.e("h","h");
                        ((HomeActivity)mContext).runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Log.e("hp","hp");
                                notifyDataSetChanged();
                            }
                        });
                    }
                }).start();
            }
        }
    });

`

NoxLab commented 4 years ago

@DavidBarbaran, Its because ExpandableItem intercept touch. You need block touch listener in ExpandableItem headerLayout, like so

    val item: ExpandableItem = v.findViewById(R.id.row) as ExpandableItem
    holder.item.headerLayout.setOnTouchListener(null)

and move expand logic from library in your code

    holder.item.setOnClickListener {
                val currentPosition = holder.layoutPosition
                val tg = ExpandableItem::class.java.canonicalName
                for (index in 0 until layout.childCount) {
                    if (index != currentPosition - layout.findFirstVisibleItemPosition()) {
                        val currentExpandableItem: ExpandableItem = layout.getChildAt(index)!!.findViewWithTag(tg)
                        currentExpandableItem.hide()
                    }
                }
                if (holder.item.isOpened) {
                    holder.item.hide()
                } else {
                    holder.item.show()
                }
            }
DavidBarbaran commented 4 years ago

Wow it's been so long 😮

Thanks for taking the time to respond, I'm sure it will help someone who has the same problem 🤝