我有一个recyclerview.adapter类的活动。RecyclerView包含按钮。单击该按钮时,我想更改在主活动中定义的值,并将其显示在同样在主活动中定义的textview中,否则我想刷新该活动。
我已经试过一些代码,比如:
Android, How to call onCreate() explicitly from other method?
How to restart the onCreate function

最佳答案

YourActivity extends Activity implements YourAdapter.ClickCallback{

@override
oncreate(Bundle bundle){
 // initialize views
 YourAdapter adapter = new YourAdapter(this);
 // do something with your adapter
}

@Override
public void updateView(){
//update your views here
}

}

//In Recycler Adapter pass ClickCallBack As parameter
class YourAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

 private ClickCallBack clickCallBack;

 public YourAdapter(ClickCallBack clickCallBack){
 this.clickCallBack=clickCallBack;
 }

 //ClickCallback Interface
interface ClickCallBack {
 void updateView();
}

 //In your ButtonClick
  clickCallBack.updateView();

}

您还可以在updateview方法中添加参数。

关于android - 我有一个与回收者 View 适配器类有关的 Activity ,当在适配器类中单击按钮时,我想刷新主要 Activity ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45273319/

10-09 12:49