如何将结果从回收站视图返回到片段

时间:2015-04-18 01:38:34

标签: android webview android-recyclerview

我有一个带有片段B的活动B,我使用startActivityForResult从活动A打开。在片段B中,我有一个回收网格视图。在网格视图中,我有用于显示GIF动画的网页视图网格。

我的片段B代码

public static class GiphyFragment extends Fragment {

private RecyclerView mRecyclerView;
private GiphyTestAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;

public GiphyFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_giphy_test, container, false);

    mRecyclerView = (RecyclerView) rootView.findViewById(R.id.giphy_recycler_view);
    mRecyclerView.setHasFixedSize(true);

    // use a grid layout manager
    mLayoutManager = new GridLayoutManager(getActivity(),2);
    mRecyclerView.setLayoutManager(mLayoutManager);

    // specify an adapter
    ArrayList<Giphy> giphyDataSet = new ArrayList<Giphy>();
    mAdapter = new GiphyTestAdapter(getActivity(),giphyDataSet);
    mRecyclerView.setAdapter(mAdapter);

    //run async task to fetch list of giphys
    ...
    ...

    return rootView;
}
}

GiphyTestAdapter

public class GiphyTestAdapter extends RecyclerView.Adapter<GiphyTestAdapter.ViewHolder> {

private final String LOG_TAG = getClass().getSimpleName();
private ArrayList<Giphy> mDataset;
private Context mContext;

public class ViewHolder extends RecyclerView.ViewHolder{
    public WebView giphyView;
    public TextView textView;

    public ViewHolder(View v) {
        super(v);
        giphyView = (WebView) v.findViewById(R.id.giphy_view);
        textView = (TextView) v.findViewById(R.id.giphy_url);
        this.giphyView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url){
                Toast toast = Toast.makeText(mContext, textView.getText(), Toast.LENGTH_SHORT);
                toast.show();
                //Here I want to return result to fragment so that it can give it to activity A
                return true;
            }
        });
    }
}

public void add(int position, Giphy giphyObj) {
    mDataset.add(giphyObj);
    notifyItemInserted(position);
}

public GiphyTestAdapter(Context context, ArrayList<Giphy> myDataset) {
    mContext = context;
    mDataset = myDataset;
}

// Create new views (invoked by the layout manager)
@Override
public GiphyTestAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_giphy_item_list, parent, false);
    ViewHolder vh = new ViewHolder(v);
    return vh;
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    final Giphy giphy = mDataset.get(position);
    holder.giphyView.loadUrl(giphy.getUrl());
    holder.textView.setText(giphy.getUrl());
}

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

目标我想将结果返回到活动A.

通常我会通过创建一个intent在片段B中这样做,使用put extra和调用finish添加所需的结果。

在上面的代码中,我有shouldOverrideUrlLoading方法返回的数据。如何给它片段B以便它可以创建意图并返回?

3 个答案:

答案 0 :(得分:1)

尝试使用getActivity().setResult(int responseCode, Intent data),在活动A中,覆盖Activity.onActivityResult(int requestCode, int responseCode, Intent data)

答案 1 :(得分:0)

你的Fragment B需要有一个Activity B实现的接口。一旦你有了Fragment中的数据,只需调用接口将数据传递给Activity,然后设置intent并调用finish,这样Activity A就会得到结果。

与onFragmentInteraction()接口没有区别:)

答案 2 :(得分:0)

我认为@Degreat Yartey的答案有效。

但还有其他两种方法值得尝试:

1.注册BoadcastReceiver是FragmentA并在ActivityB中发送广播;

2.使用界面来完成它。