如何在另一个recyclerview适配器下使用recyclerview适配器?
How to recycleview adapter under another recycleview adapter?
我有一个RecyclerView
。它有一个自定义布局,自定义布局内部是另一个 RecyclerView
。当我通知回收站视图某个项目已被删除时,我的主回收站视图已更新,但我的自定义视图回收视图未收到通知。
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);
这是RecyclerView
滑动删除的代码。在我的购物车适配器中,我采用了另一个回收视图。任何想法如何通知适应如果从回收视图中删除任何数据。???
我的 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();
}
}
父 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" />
子回收视图
<?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>
首先 为什么你甚至需要一个回收器视图在另一个? 它通常通过可扩展的回收器视图来解决。 Here is a nice tutorial.
其次,我看到这一行 cartAdapter.notifyItemRemoved(id);
第二个回收商的通知在哪里?你上面有 link 吗?你应该打电话给notifyDataSetChanged()
!
而且 天哪,有这么多匿名者 类! 我什至无法理解您的代码中发生了什么!简直无法理解。认真地摆脱那些东西。
像这样尝试:
@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();
}
}
在你的 setter 中这样做:
//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();
}
今天我终于从另一个方面得到了答案
我只是在 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);
}
我有一个RecyclerView
。它有一个自定义布局,自定义布局内部是另一个 RecyclerView
。当我通知回收站视图某个项目已被删除时,我的主回收站视图已更新,但我的自定义视图回收视图未收到通知。
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);
这是RecyclerView
滑动删除的代码。在我的购物车适配器中,我采用了另一个回收视图。任何想法如何通知适应如果从回收视图中删除任何数据。???
我的 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();
}
}
父 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" />
子回收视图
<?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>
首先 为什么你甚至需要一个回收器视图在另一个? 它通常通过可扩展的回收器视图来解决。 Here is a nice tutorial.
其次,我看到这一行 cartAdapter.notifyItemRemoved(id);
第二个回收商的通知在哪里?你上面有 link 吗?你应该打电话给notifyDataSetChanged()
!
而且 天哪,有这么多匿名者 类! 我什至无法理解您的代码中发生了什么!简直无法理解。认真地摆脱那些东西。
像这样尝试:
@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();
}
}
在你的 setter 中这样做:
//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();
}
今天我终于从另一个方面得到了答案
我只是在 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);
}