从 recyclerview 行中的项目调用新的 activity
Calling new activity from an item inside a recyclerview row
我需要调用一个新的 activity,当我的一个 recyclerview 行元素中的一个按钮被调用时。列表中的每一行项目包含 4 个按钮,其中一个需要打开一个新的 activity,它将用于编辑该行中的数据。
到目前为止,这是我的按钮代码:
public void onBindViewHolder(CounterLayoutAdapter.ViewHolder holder, final
int position) {
final Counter counter = counterList.get(position);
//counter is a class which holds the data that will be displayed on one
//row
String comment = counter.getComment();
String name = counter.getCounterName();
int number = counter.getCurrentValue();
//LocalDate modifyDate = counter.getLastModifyDate();
Button up = holder.itemView.findViewById(R.id.buttonUp);
Button down = holder.itemView.findViewById(R.id.buttonDown);
Button reset = holder.itemView.findViewById(R.id.buttonReset);
Button edit = holder.itemView.findViewById(R.id.buttonEdit);
Button delete = holder.itemView.findViewById(R.id.buttonDelete);
// code for 4 other buttons goes here
//
edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
因为我需要打开 return 用户输入数据的 activity,所以我正在使用 startActivityForResult。但是,据我所知,这只会在实际的 activity class 中起作用。
然后我尝试将 mainactivity 上下文传递到我的 CounterLayoutAdapter class,我所有的按钮代码都在这里。但是,OnBindViewHolder 方法仍然无法在那里访问它。所以我尝试将上下文传递给 OnBindViewHolder,但这也不起作用,因为如果我这样做它不会覆盖抽象 class..
那么,我到底怎么才能在这里调用一个新的 activity?
或者,如果有一些其他方法可以让用户输入到 4 个字段并且 return 将输入返回给适配器,而不调用 activity,那也可以。
编辑:viewholder 和布局 inflation
public static class ViewHolder extends RecyclerView.ViewHolder implements
View.OnClickListener {
private TextView name;
private TextView comment;
private TextView number;
//private TextView date;
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
comment = itemView.findViewById(R.id.textComment);
name = itemView.findViewById(R.id.textName);
number = itemView.findViewById(R.id.editTextNum);
//date = itemView.findViewById(R.id.textDate);
}
@Override
public void onClick(View view) {}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View inflatedView =
LayoutInflater.from(parent.getContext()).inflate(R.layout
.row_layout, parent, false);
return new ViewHolder(inflatedView);
}
如您所见,您不必在 onBindViewHolder
中 findViewById
。
public void onBindViewHolder(CounterLayoutAdapter.ViewHolder holder, final
int position) {
final Counter counter = counterList.get(position);
//counter is a class which holds the data that will be displayed on one
//row
String comment = counter.getComment();
String name = counter.getCounterName();
int number = counter.getCurrentValue();
//LocalDate modifyDate = counter.getLastModifyDate();
holder.edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
那么你应该在 ViewHolder
构造函数中初始化 edit
。
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
comment = itemView.findViewById(R.id.textComment);
name = itemView.findViewById(R.id.textName);
number = itemView.findViewById(R.id.editTextNum);
//date = itemView.findViewById(R.id.textDate);
// init four button
edit = itemView.findViewById(R.id.buttonEdit)
}
您可以在适配器 class 中调用 startActivityForResult()
。
- 在适配器中获取上下文,如
Context context=holder.up.getContext();
然后在您按钮的 OnClickListener 中执行此操作。
edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(context,ActivityYouWantToStart.class);
//Pass any extras if you want to.
((Activity)context).startActivityForResult(intent,REQUEST_CODE);
}
});
然后在你的activity(其中包含这个recyclerView)覆盖onActivityResult
像这样
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE) {//same REQUEST_CODE you used in adapter
if (resultCode == RESULT_OK) {
//Do your thing and get the data you want.
adapter.onDataReady(Data data);//where adapter is your recycler adapter,
//and data is whatever data you want to pass to adapter
//(Data you got from the activityResult, do not confuse it with onActivityResult's parameter 'Intent data')
}
}
}
最后在你的 Recycler Adapter class 中,定义 onDataReady()
函数,如
public void onDataReady(Data data){
//Update RecyclerView with new data
}
希望这对您有所帮助。我曾经这样做过,它对我有用。如果您有任何问题,请告诉我。
我需要调用一个新的 activity,当我的一个 recyclerview 行元素中的一个按钮被调用时。列表中的每一行项目包含 4 个按钮,其中一个需要打开一个新的 activity,它将用于编辑该行中的数据。
到目前为止,这是我的按钮代码:
public void onBindViewHolder(CounterLayoutAdapter.ViewHolder holder, final
int position) {
final Counter counter = counterList.get(position);
//counter is a class which holds the data that will be displayed on one
//row
String comment = counter.getComment();
String name = counter.getCounterName();
int number = counter.getCurrentValue();
//LocalDate modifyDate = counter.getLastModifyDate();
Button up = holder.itemView.findViewById(R.id.buttonUp);
Button down = holder.itemView.findViewById(R.id.buttonDown);
Button reset = holder.itemView.findViewById(R.id.buttonReset);
Button edit = holder.itemView.findViewById(R.id.buttonEdit);
Button delete = holder.itemView.findViewById(R.id.buttonDelete);
// code for 4 other buttons goes here
//
edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
因为我需要打开 return 用户输入数据的 activity,所以我正在使用 startActivityForResult。但是,据我所知,这只会在实际的 activity class 中起作用。
然后我尝试将 mainactivity 上下文传递到我的 CounterLayoutAdapter class,我所有的按钮代码都在这里。但是,OnBindViewHolder 方法仍然无法在那里访问它。所以我尝试将上下文传递给 OnBindViewHolder,但这也不起作用,因为如果我这样做它不会覆盖抽象 class..
那么,我到底怎么才能在这里调用一个新的 activity?
或者,如果有一些其他方法可以让用户输入到 4 个字段并且 return 将输入返回给适配器,而不调用 activity,那也可以。
编辑:viewholder 和布局 inflation
public static class ViewHolder extends RecyclerView.ViewHolder implements
View.OnClickListener {
private TextView name;
private TextView comment;
private TextView number;
//private TextView date;
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
comment = itemView.findViewById(R.id.textComment);
name = itemView.findViewById(R.id.textName);
number = itemView.findViewById(R.id.editTextNum);
//date = itemView.findViewById(R.id.textDate);
}
@Override
public void onClick(View view) {}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View inflatedView =
LayoutInflater.from(parent.getContext()).inflate(R.layout
.row_layout, parent, false);
return new ViewHolder(inflatedView);
}
如您所见,您不必在 onBindViewHolder
中 findViewById
。
public void onBindViewHolder(CounterLayoutAdapter.ViewHolder holder, final
int position) {
final Counter counter = counterList.get(position);
//counter is a class which holds the data that will be displayed on one
//row
String comment = counter.getComment();
String name = counter.getCounterName();
int number = counter.getCurrentValue();
//LocalDate modifyDate = counter.getLastModifyDate();
holder.edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
那么你应该在 ViewHolder
构造函数中初始化 edit
。
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
comment = itemView.findViewById(R.id.textComment);
name = itemView.findViewById(R.id.textName);
number = itemView.findViewById(R.id.editTextNum);
//date = itemView.findViewById(R.id.textDate);
// init four button
edit = itemView.findViewById(R.id.buttonEdit)
}
您可以在适配器 class 中调用 startActivityForResult()
。
- 在适配器中获取上下文,如
Context context=holder.up.getContext();
然后在您按钮的 OnClickListener 中执行此操作。
edit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(context,ActivityYouWantToStart.class); //Pass any extras if you want to. ((Activity)context).startActivityForResult(intent,REQUEST_CODE); } });
然后在你的activity(其中包含这个recyclerView)覆盖
onActivityResult
像这样@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE) {//same REQUEST_CODE you used in adapter if (resultCode == RESULT_OK) { //Do your thing and get the data you want. adapter.onDataReady(Data data);//where adapter is your recycler adapter, //and data is whatever data you want to pass to adapter //(Data you got from the activityResult, do not confuse it with onActivityResult's parameter 'Intent data') } } }
最后在你的 Recycler Adapter class 中,定义
onDataReady()
函数,如public void onDataReady(Data data){ //Update RecyclerView with new data }
希望这对您有所帮助。我曾经这样做过,它对我有用。如果您有任何问题,请告诉我。