当我从recyvlerview中删除一个项目时,它可以正确地反映在我的视图中,但是我还需要在与recyvlerview相关的文本视图中也反映出它。
是否可以通过从适配器的recyvlerview中删除项目来重新加载片段?
holder.layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Remove");
builder.setTitle("Do you want to remove");
builder.setNeutralButton("Remove", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int witch) {
DatabaseHelper dbHelper = new DatabaseHelper(context);
dbHelper.deleteItem(cot.getId(), context);
list.remove(position);
recyclerView.removeViewAt(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position,list.size());
notifyDataSetChanged();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int witch ) {
dialog.dismiss();
}
});
builder.create().show();
}
});
更新:
也许我没有很好地解释自己,这就是为什么回答arent的原因。当我从recyclerview删除时,项目正确刷新了,我从适配器调用了我的项目并将其放在textviews中。我使用这些texviews中的数据进行求和,减法等操作,并将结果放在另一个textviews中,但是如果我删除某项,则那些不会改变,因为它们不属于我的recyclerview。但是,如果我再次打开片段,我的文本视图中会看到不同的结果,所以我认为每当删除recylcerview的项时,我确实都需要刷新整个片段
答案 0 :(得分:0)
我对此不确定,但是我想解决这个问题。将以下代码放入“删除”按钮:-
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.detach(this).attach(this).commit();
因此,每当删除一个项目时,都会触发此代码以刷新您的片段。
答案 1 :(得分:0)
是的,有很多方法可以解决这个问题... 您可以通过创建 首先,
如果要通过XML创建片段, 因此,您只需要先像
FragmentClassName object=getSupportFragmentManager().findFragmentById(R.id.id_frag);
如果要以编程方式按视图组添加片段
FragmentClassName object=getSupportFragmentManager().beginTransaction().add().commit();
使用此对象可以调用Fragment Class中的任何方法来更新视图或数据
就像这里
holder.layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Remove");
builder.setTitle("Do you want to remove");
builder.setNeutralButton("Remove", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int witch) {
DatabaseHelper dbHelper = new DatabaseHelper(context);
dbHelper.deleteItem(cot.getId(), context);
list.remove(position);
recyclerView.removeViewAt(position);
notifyItemRemoved(position);
object.methodCall();
// Don't do that only one call is enough to delete item from view
// notifyItemRangeChanged(position,list.size());
//notifyDataSetChanged();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int witch ) {
dialog.dismiss();
}
});
builder.create().show();
}
});
其他是一些棘手的方法,这些方法使用接口调用了调用机制,
如果您知道它们,则可以通过调用静态方法来绑定接口并使用其对象进行调用来达到目的。
让我通过调用接口机制来更新我的答案以获得更多帮助
//write this on your activity or fragment to get delete notification
// Can be used to get reply on Any Class Either Activity or Fragment, no matter you recycler or adapter existence class.
Adapter.bindListener(new Adapter.NotifyMe() {
@Override
public void onItemDelete() {
// When ever your delete code run your this block run then you can
//Update your code from here if you want to update things of fragment class
}
});
您的适配器类
Class Adapter extend RecyclerView.Adapter<Adapter.ViewHolder> {
private static NotifyMe notifyMe;
public static void bindListener (NotifyMe notifyMeListener){
notifyMe = notifyMeListener;
}
@Override
public void onBindViewHolder (@NonNull ViewHolder viewHolder,int position){
holder.layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
builder.setNeutralButton("Remove", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int witch) {
//....
notifyItemRemoved(position);
// object.methodCall();
//Call this here this will take you to implementation block.
if (notifyMe != null)
notifyMe.onItemDelete();
// Don't do that only one call is enough to delete item from view
// notifyItemRangeChanged(position,list.size());
//notifyDataSetChanged();
}
});
}
});
}
public interface NotifyMe {
void onItemDelete();
}
}
或者更多,还有另一种实现方式供您学习
//您的适配器对象声明代码
Adapter adapter = new Adapter(new Adapter.NotifyMe() {
@Override
public void onItemDelete() {
// When ever your delete code run your this block run then you can
//Update your code from here if you want to update things of fragment class
}
});
适配器类代码:
Class Adapter extend RecyclerView.Adapter<Adapter.ViewHolder> {
private NotifyMe notifyMe;
public Adapter(NotifyMe notifyMe) {
this.notifyMe=notifyMe;
}
@Override
public void onBindViewHolder (@NonNull ViewHolder viewHolder,int position){
holder.layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
builder.setNeutralButton("Remove", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int witch) {
//....
notifyItemRemoved(position);
// object.methodCall();
//Call this here this will take you to implementation block.
notifyMe.onItemDelete();
// Don't do that only one call is enough to delete item from view
// notifyItemRangeChanged(position,list.size());
//notifyDataSetChanged();
}
});
}
});
}
public interface NotifyMe {
void onItemDelete();
}
}