gpt4 book ai didi

java - 如何在 RecyclerView.Adapter 中使用 onActivityResult

转载 作者:行者123 更新时间:2023-12-01 12:04:58 24 4
gpt4 key购买 nike

我想在 RecyclerView.Adapter 中使用 onActivityResult。但是我不明白这个版本在我的情况下是如何发布的。我有一个动态的 RecyclerView.Adapter

RecyclerView.Adapter:

public class SettingsRecyclerAdapter extends RecyclerView.Adapter {

private ArrayList<SettingsTypeModel> dataSet;
Context mContext;
int total_types;


TextInputLayout textInputLayoutIncome;
TextInputLayout textInputLayoutCosts;
EditText editIncome;
EditText editCosts;

public MoneyTypeViewHolder(View itemView) {
super(itemView);

textInputLayoutIncome = (TextInputLayout) itemView.findViewById(R.id.textInputLayoutIncome);
editIncome = (EditText) textInputLayoutIncome.findViewById(R.id.editIncome);

textInputLayoutCosts = (TextInputLayout) itemView.findViewById(R.id.textInputLayoutCosts);
editCosts = (EditText) textInputLayoutCosts.findViewById(R.id.editCosts);
}
}
public static class SendToMailTypeViewHolder extends RecyclerView.ViewHolder {

Switch switchSendMailReport;

public SendToMailTypeViewHolder(View itemView) {
super(itemView);
switchSendMailReport = (Switch) itemView.findViewById(R.id.switchSendMailReport);
}
}

public static class SendToDriveTypeViewHolder extends RecyclerView.ViewHolder {

Switch switchDriveReport;

public SendToDriveTypeViewHolder(View itemView) {
super(itemView);

switchDriveReport = (Switch) itemView.findViewById(R.id.switchDriveReport);
}
}

public SettingsRecyclerAdapter(ArrayList<SettingsTypeModel> data, Context context) {
this.dataSet = data;
this.mContext = context;
total_types = dataSet.size();
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
switch (viewType) {
case SettingsTypeModel.TYPE_SEND_TO_MAIL:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.type_send_to_mail, parent, false);
return new SendToMailTypeViewHolder(view);
case SettingsTypeModel.TYPE_SEND_TO_DRIVE:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.type_send_to_drive, parent, false);
return new SendToDriveTypeViewHolder(view);
}
return null;
}

@Override
public int getItemViewType(int position) {
switch (dataSet.get(position).type) {
case 4:
return SettingsTypeModel.TYPE_SEND_TO_MAIL;
case 5:
return SettingsTypeModel.TYPE_SEND_TO_DRIVE;
default:
return -1;
}
}

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int listPosition) {

SettingsTypeModel object = dataSet.get(listPosition);
if (object != null) {
switch (object.type) {
case SettingsTypeModel.TYPE_SEND_TO_MAIL:

((SendToMailTypeViewHolder) holder).switchSendMailReport.setChecked(false);
((SendToMailTypeViewHolder) holder).switchSendMailReport.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {

} else {

}
}
});
// switchSendMailReport.setChecked(getSwitchSendReportMail());

break;

case SettingsTypeModel.TYPE_SEND_TO_DRIVE:

((SendToDriveTypeViewHolder) holder).switchDriveReport.setChecked(false);
((SendToDriveTypeViewHolder) holder).switchDriveReport.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
((SendToDriveTypeViewHolder) holder).switchDriveReport.setChecked(false);

} else {

}
}
});

break;
}
}
}

@Override
public int getItemCount() {
return dataSet.size();
}
}

Activity :

public class SettingsActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_settings);

ArrayList<SettingsTypeModel> list= new ArrayList<>();
list.add(new SettingsTypeModel(SettingsTypeModel.TYPE_SEND_TO_MAIL));
list.add(new SettingsTypeModel(SettingsTypeModel.TYPE_SEND_TO_DRIVE));

SettingsRecyclerAdapter adapter = new SettingsRecyclerAdapter(list,this);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, OrientationHelper.VERTICAL, false);

RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recyclerVieww);
mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
mRecyclerView.setLayoutManager(linearLayoutManager);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setClickable(true);
mRecyclerView.setAdapter(adapter);
}

public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);

}

@Override
protected void onDestroy() {
super.onDestroy();
}

@Override
public void onBackPressed() {
super.onBackPressed();
}
}

点击 switchSendMailReport 或 switchDriveReport 后,我​​需要调用函数 onActivityResult

如何在 RecyclerView.Adapter 中使用onActivityResult

非常感谢!

最佳答案

在您的 SettingsRecyclerAdapter 中创建一个接口(interface):

  public interface OnAdapterResult {
public void onAdapterResult(boolean isChecked);
}

并将接口(interface)附加到它的构造函数中:

 public SettingsRecyclerAdapter (Context context){
mContext = context;
// .. Attach the interface
try{
onAdapterResult = (OnAdapterResult) context;
}catch(ClassCastException ex){

Log.e("MyAdapter","error"+ ex,);
}
}

然后初始化你的接口(interface)方法:

((SendToMailTypeViewHolder) holder).switchSendMailReport.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
onAdapterResult.onAdapterResult(isChecked); \\ here
} else {

}
}
});

然后实现你的SettingsActivity来自adepter.OnAdapterResult接口(interface):

public class SettingsActivity extends AppCompatActivity implements adepter.OnAdapterResult {
.
.
.


@Override
public void onAdapterResult(boolean isChecked) {
//
}

}

关于java - 如何在 RecyclerView.Adapter 中使用 onActivityResult,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58364659/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com