如何在RecyclerView.Adapter中使用onActivityResult

我想在 RecyclerView.Adapter 中使用onActivityResult。但我不明白这个释放在我的情况下是如何进行的。我有一个动态 RecyclerView.Adapter

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?


三国纷争
浏览 107回答 2
2回答

慕运维8079593

在interface您的SettingsRecyclerAdapter:&nbsp; public interface OnAdapterResult {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onAdapterResult(boolean isChecked);&nbsp; &nbsp; &nbsp; &nbsp; }并将接口附加到其构造函数中:&nbsp;public SettingsRecyclerAdapter (Context context){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mContext = context;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // .. Attach the interface&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onAdapterResult = (OnAdapterResult) context;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }catch(ClassCastException ex){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.e("MyAdapter","error"+ ex,);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }然后初始化你的接口方法:((SendToMailTypeViewHolder) holder).switchSendMailReport.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onCheckedChanged(CompoundButton buttonView,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;boolean isChecked) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (isChecked) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onAdapterResult.onAdapterResult(isChecked); \\ here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });那么implements您的SettingsActivity来自adepter.OnAdapterResult interface:public class SettingsActivity extends AppCompatActivity implements adepter.OnAdapterResult {...&nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onAdapterResult(boolean isChecked) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }}

慕仙森

您不能在适配器类中使用活动的 onActivityResult() 。但您可以在适配器类中创建自己的相同方法public&nbsp;void&nbsp;onActivityResult(final&nbsp;int&nbsp;requestCode,&nbsp;final&nbsp;int&nbsp;resultCode,&nbsp;final&nbsp;Intent&nbsp;data)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;do&nbsp;you&nbsp;stuff &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}现在,当您在 onActivityResult() 中回调时,从活动中调用您的适配器方法if(adapter&nbsp;!=null){ &nbsp;&nbsp;&nbsp;adapter.onActivityResult(requestCode,resultCode,data) }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java