我在一个片段中有Recyclerview类别和Recyclerview产品(抽屉活动)。 当我单击类别中的某项时,我想重新加载/刷新recyclerview产品。
Recyclerview类别
...
@Override
public transaksibaru_kategori_viewholder onCreateViewHolder(final ViewGroup parent, int viewType) {
View view;
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_item_transaksibaru_kategori, parent, false);
final transaksibaru_kategori_viewholder vHolder = new transaksibaru_kategori_viewholder(view);
sharedPrefManager = new SharedPrefManager(parent.getContext());
Activity_transaksibaru.KEY_ACT = "run";
vHolder.linearlayout_kategori.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String id_kategori = mItem_kategori.get(vHolder.getAdapterPosition()).getId();
sharedPrefManager.savetransaksibarustring(SharedPrefManager.sp_idkategori,id_kategori);
Toast.makeText(parent.getContext(), "ID KATEGORI = " + id_kategori,Toast.LENGTH_LONG).show();
}
});
return vHolder;
}
...
片段
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
...
statusview = sharedPrefManager.getSp_viewmode();
statushitung = Integer.valueOf(statusview);
id_kategori = sharedPrefManager.getSp_idkategori();
//id_kategori = "10";
if (statushitung%2 == 0){
call_itembarang_gridview();
i=1;
}else {
call_itembarang();
i=2;
}
...
我只需要从RV_category(RV2)运行call_itembarang(),它必须很简单,但我不知道该怎么做
答案 0 :(得分:0)
解决您问题的简单方法是:
将loadCategory(int categoryId)方法添加到SharedPrefManager。
public void loadCategory(int categoryId) {
// you need to create getProductsOfCategory method in SharedPrefManager.
List<Product> products = getProductsOfCategory(categoryId);
// you need to create setProductList method in productsAdapter
productsAdapter.setProductList(products);
productsAdapter.notifyDatasetChanged();
}
在类别的RecyclerView中,在onCreateViewHolder方法中更改onClickListener,以便它使用loadCategory方法。
答案 1 :(得分:0)
因此,在这种情况下,适配器必须与片段进行通信。
我们将为此使用接口:
1)在新文件中创建界面
public interface onCategoryClickedListener{
void onCategoryClicked(String categoryId);
}
2)适配器
public class YourAdapter extends ......{
private onCategoryClickedListener listener;
//method to set listener
public void setOnCategoryClickedListener(onCategoryClickedListener listener){
this.listener=listener;
}
//oncreateviewholder
@Override
public transaksibaru_kategori_viewholder onCreateViewHolder(final ViewGroup parent, int viewType) {
......
vHolder.linearlayout_kategori.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//your category id
final String id_kategori = mItem_kategori.get(vHolder.getAdapterPosition()).getId();
//send the category id back to your fragment
listner.onCategoryClicked(id_kategori);
}
});
}
3)现在让您的片段实现此接口
public class YourFragment extends Fragment implements onCategoryClickedListener{
private RecyclerView mRecyclerView;
private YourAdapter adapter;
.....
.....
.....
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
.......
//here you pass the listener to your adapter
adapter.setOnCategoryClickedListener(this);
........
return(view);
}
@Override
public void onCategoryClicked(String categoryId){
//this will be called every time an item in your category is clicked
//call the method from here and whatever if statements you want
}
}