9

I have a RecyclerView. It has a custom layout and inside the custom layout is another RecyclerView. When I notify the recycler view that an item has been deleted, my main recycler view is updated but my custom view recycle view is not getting notified.

 SwipeDismissRecyclerViewTouchListener listener = new SwipeDismissRecyclerViewTouchListener.Builder(
            recyclerView,
            new SwipeDismissRecyclerViewTouchListener.DismissCallbacks() {
                @Override
                public boolean canDismiss(int position) {

                    return true;
                }

                @Override
                public void onDismiss(View view) {
                    // Do what you want when dismiss
                    int id = recyclerView.getChildAdapterPosition(view);
                    databaseHelper.deleteSingleItem(cartAdapter.cartItemName.get(id));

                    Log.e(TAG, "onDismiss: " + subdata.get(id) + " " + data.get(id));
                    subdataprice.remove(id);
                    subdata.remove(id);
                    addonlist.remove(id);
                    price.remove(id);
                    data.remove(id);
                    cartAdapter.notifyItemRemoved(id);

                    Log.e(TAG, data.size() + " onDismiss: size " + subdata.size());
                    totalPriceTextView.setText(String.valueOf(getTotal()));
                    cartAdapter.updateRecycler();
                }
            })
            .setIsVertical(false)
            .setItemClickCallback(new SwipeDismissRecyclerViewTouchListener.OnItemClickCallBack() {
                @Override
                public void onClick(int i) {
                    // Toast.makeText(getBaseContext(), String.format("Delete item"), Toast.LENGTH_LONG).show();

                }
            })
            .create();
    recyclerView.setOnTouchListener(listener);

This is the code for RecyclerView swipe to remove. In my cart Adapter I've take another recycle view adapted. Any Idea how to notify adapted If any data is removed from recycle view.???

My onBindViewHolder Class

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

    try {
        holder.itemName.setText(String.format("%s", cartItemName.get(position)));
        holder.itemPrice.setText(String.format("£  %s", cartItemPrice.get(position)));

        AddonRecycleviewAdapter recycleViewAdapter = new AddonRecycleviewAdapter(context, listsubdata.get(position), listsubprice.get(position), listsubAddon.get(position));
        holder.addon_recycleview.setHasFixedSize(true);
        holder.addon_recycleview.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        holder.addon_recycleview.setAdapter(recycleViewAdapter);


    } catch (Exception e) {
        e.printStackTrace();
    }
}

Parent Recycleview

   <android.support.v7.widget.RecyclerView
                android:id="@+id/cartRecyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="2dp"
                android:clipToPadding="false"
                android:padding="@dimen/item_gallery_padding"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />

Child recycleview

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">


<ImageButton
    android:visibility="invisible"
    android:id="@+id/btnremove"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_margin="@dimen/standard_padding_small"
    android:background="@null"
    android:src="@drawable/ic_action_action_search"
    android:text="Remove" />


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/standard_padding_small"
    android:gravity="center_vertical"
    android:padding="8dp"
    android:text="+" />


<TextView
    android:id="@+id/addonNameTextView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/standard_padding_small"
    android:layout_weight="1"
    android:ellipsize="end"
    android:gravity="center_vertical"
    android:maxLines="1"
    android:padding="8dp"
    android:text="9 inch Pizza"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="@color/secondary_text" />

<TextView
    android:id="@+id/addOnPriceTextView"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_margin="@dimen/standard_padding_small"
    android:gravity="center_vertical"
    android:maxLines="1"
    android:text="£3.15"
    android:padding="8dp"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="@color/primary_text" />
</LinearLayout>
AesSedai101
  • 1,502
  • 2
  • 23
  • 37
AMAN SINGH
  • 3,491
  • 6
  • 28
  • 44
  • 1
    you can check for http://android-pratap.blogspot.in/2015/12/horizontal-recyclerview-in-vertical.html it will give you; nice example what are you asking for. – Pramod mishra Aug 02 '16 at 08:50
  • yeah this is some what similar but there no any code for how to notify if any changes in SectionListDataAdapter – AMAN SINGH Aug 02 '16 at 09:06
  • Show your parent Recycle view adapter code..it need to set ChildRecycleView in onBindViewHolder instead of in ViewHolder class... – Burhanuddin Rashid Aug 02 '16 at 09:07
  • I updated my onBindViewHolder class please check it @BurhanuddinRashid – AMAN SINGH Aug 02 '16 at 09:23
  • `setHasFixedSized(true)` is not necessary..remove it and check – Burhanuddin Rashid Aug 02 '16 at 10:11
  • already checked that one also – AMAN SINGH Aug 02 '16 at 10:12
  • post your xml file for both parent and child recycleview – Burhanuddin Rashid Aug 02 '16 at 11:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/118900/discussion-between-aman-singh-and-burhanuddin-rashid). – AMAN SINGH Aug 02 '16 at 11:41
  • so all you want to do is "how to call `notifyDataSetChanged` on the Adapter on the RecyclerView that is inside the main RecyclerView? If that's it, the answer is to have the adapter register for data changes. Let me know and I'll be happy to write an example – Budius Aug 05 '16 at 07:18
  • yeah @Budius you're ryt. I want to call `notifyDataSetChanged` from main RecycleView to inside custom RecycleView – AMAN SINGH Aug 05 '16 at 08:09
  • You've set `addon_recycleview` inside your parent recyclerview's holder. Can you set an id/ tag to `addon_recycleview`, retrieve it from `onDismiss` view and call `notifyDataChanged` for it? – random Aug 09 '16 at 06:40

3 Answers3

1

Well first of all why do you even need one recycler view in the other? It is usually solved through expandable recycler view. Here is a nice tutorial.

Second, I see this line cartAdapter.notifyItemRemoved(id); where is notification for the second recycler? Have you got a link on it? You should call notifyDataSetChanged() for it!

And OH MY GOD THERE ARE SO MUCH ANONYMOUS CLASSES! I cannot even understand what is happening in your code! It is just incomprehensible. Get rid of that stuff, seriously.

RexSplode
  • 1,475
  • 1
  • 16
  • 24
0

Try Like this:

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

                try {
                    holder.itemName.setText(String.format("%s", cartItemName.get(position)));
                    holder.itemPrice.setText(String.format("£  %s", cartItemPrice.get(position)));

                    AddonRecycleviewAdapter recycleViewAdapter = new AddonRecycleviewAdapter(context);
                    holder.addon_recycleview.setHasFixedSize(true);
                    holder.addon_recycleview.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));

                    //Create a setter method in AddonRecycleviewAdapter  adapter
                    holder.addon_recycleview.setLists(listsubdata.get(position), listsubprice.get(position), listsubAddon.get(position));
                    holder.addon_recycleview.setAdapter(recycleViewAdapter);



                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

And in your setter do like this:

//set list of items from ParentAdapter in your AddonRecycleviewAdapter adapter
    public void setLists(List<T> l1,List<T> l2,List<T> l3) {
        //Set You list as field variable here        
        this.l1 = l1;
        this.l2 = l2;
        this.l3 = l3;
        this.notifyDataSetChanged();
    }
Burhanuddin Rashid
  • 5,260
  • 6
  • 34
  • 51
0

finally I got my answer today , from another way simply i call adapter in synchronized

synchronized(recyclerview){
     recycleViewAdapter = new AddonRecycleviewAdapter(context, listsubdata.get(position), listsubprice.get(position), listsubAddon.get(position));
    holder.addon_recycleview.setHasFixedSize(true);
    holder.addon_recycleview.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
    holder.addon_recycleview.setAdapter(recycleViewAdapter);
}
AMAN SINGH
  • 3,491
  • 6
  • 28
  • 44