是的,有很多方法可以解决这个问题...
您可以通过以下方式创建它,
首先,
-
如果您通过 XML 创建片段,
所以你只需要先通过like来获取你的片段
FragmentClassName object=getSupportFragmentManager().findFragmentById(R.id.id_frag);
-
如果您以编程方式按视图组添加片段
FragmentClassName object=getSupportFragmentManager().beginTransaction().add().commit();
使用该对象调用 Fragment 类中的任何方法来更新视图或数据
喜欢这里,
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();
}
}