自RecyclerAdapter和startActivityForResult [英] Custom RecyclerAdapter and startActivityForResult

查看:355
本文介绍了自RecyclerAdapter和startActivityForResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含片段 A RecyclerView 它使用一个自定义的 RecyclerAdapter 。我有一个 onClickListener 我的自定义里面 RecyclerAdapter - 被点击的位置时,我想它来启动 startActivityForResult 。到目前为止,这部作品在这样的时候被点击,并根据需要启动该活动的。然而,当我preSS后退按钮转到片段包含 RecyclerView 的onActivityResult 永远不会被调用。我有一个背景下自定义 RecyclerAdapter 通过。这东西是可能的吗?抑或是活动/片段开始 startActivityForResult 是拦截它的人?如果不是我最终会处理片段中的onClick用手势检测器或类似的东西,但在此之前,我想这给出一个公正的裂缝!注:我已经包括的onActivityResult MainActivity 里面有片段容器,以便在片段并收到的onActivityResult 如果 startActivityForResult 片段启动。我的code:

RecyclerAdapter onClickListener:

  @覆盖
        公共无效的onClick(视图v){            串titleId = titlesListDataArrayList.get(getLayoutPosition())getTitle_id();            意向意图=新意图(背景下,CreateItemsActivity.class);
            intent.putExtra(title_id的titleId);
            ((活动)上下文).startActivityForResult(意向,Constants.NEW_ITEMS_REQUEST_ code);
        }

CreateItemsActivity.class - onBack pressed()

  @覆盖
        公共无效onBack pressed(){            意向意图=新的Intent();
            的setResult(Constants.NEW_ITEMS_REQUEST_ code,意向);
            完();
        }

MyListsFragment.class(含RecyclerView)

  @覆盖
    公共无效的onActivityResult(INT申请code,INT结果code,意图数据){
        super.onActivityResult(要求code,结果code,数据);        Log.e(叫,OnActivity结果);        //如果请求code从CreateItemsActivity匹配
        如果(要求code == Constants.NEW_ITEMS_REQUEST_ code){            Log.e(REQUEST code,将String.valueOf(要求code));            populateRecyclerView();
        }
    }

我也有这个在MainActivity:

  @覆盖
    保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
        super.onActivityResult(要求code,结果code,数据);
        //包括允许片段接收的onActivityResult    }


解决方案

好了,万一别人在这里有这个问题是我的

解决方案。我说这个code到在MainActivity的onActivityResult(注意我有​​一个框架容器,是所有片段充气):

  @覆盖
    保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
        super.onActivityResult(要求code,结果code,数据);        //获取集装箱当前片段
        片段片段= getSupportFragmentManager()findFragmentById(R.id.frameContainer)。
        fragment.onActivityResult(要求code,结果code,数据);
    }

我相信这可以作为在MainActivity是顶部的层次和拦截的onActivityResult,所以基本上我只是点吧,我希望它被使用。

I have a Fragment that contains a RecyclerView which uses a custom RecyclerAdapter. I have an onClickListener inside my custom RecyclerAdapter - when a position is clicked I want it to start startActivityForResult. So far this works in as such as when it is clicked it starts the Activity as desired. However when I press the back button to go to the Fragment containing the RecyclerView onActivityResult is never called. I have passed in a context to the custom RecyclerAdapter. Is this something that is possible? Or does the Activity/Fragment initiating startActivityForResult be the one that intercepts it? If not I will end up handling the onClick in the Fragment with a gesture detector or something similar, but before that I wanted to give this a fair crack! Note: I have included onActivityResult in the MainActivity which has the Fragment container so the Fragment does receive onActivityResult if startActivityForResult is initiated from the Fragment. My code:

RecyclerAdapter onClickListener:

@Override
        public void onClick(View v) {

            String titleId = titlesListDataArrayList.get(getLayoutPosition()).getTitle_id();

            Intent intent = new Intent(context, CreateItemsActivity.class);
            intent.putExtra("TITLE_ID", titleId);
            ((Activity) context).startActivityForResult(intent, Constants.NEW_ITEMS_REQUEST_CODE);
        }

CreateItemsActivity.class - onBackPressed()

    @Override
        public void onBackPressed() {

            Intent intent = new Intent();
            setResult(Constants.NEW_ITEMS_REQUEST_CODE, intent);
            finish();
        }

MyListsFragment.class (contains RecyclerView)

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

        Log.e("CALLED", "OnActivity Result");

        // if requestCode matches from CreateItemsActivity
        if (requestCode == Constants.NEW_ITEMS_REQUEST_CODE) {

            Log.e("REQUEST CODE", String.valueOf(requestCode));

            populateRecyclerView();
        }
    }

Also I have this in the MainActivity:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // included to allow fragment to receive onActivityResult

    }

解决方案

OK, so IF by any chance somebody else has this problem here is my

solution. I added this code into the MainActivity onActivityResult (note I have a frame container which is where all fragments are inflated):

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // get current fragment in container
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.frameContainer);
        fragment.onActivityResult(requestCode, resultCode, data);
    }

I believe this works as the MainActivity is top in the hierarchy and intercepts the onActivityResult, so basically I just point it where I want it to be used.

这篇关于自RecyclerAdapter和startActivityForResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆