gpt4 book ai didi

android - 如何从回收者 View 将结果返回到 fragment

转载 作者:行者123 更新时间:2023-11-30 02:13:49 24 4
gpt4 key购买 nike

我有一个带有 fragment B 的 Activity B,我使用 startActivityForResult 从 Activity A 打开它。在我的 fragment B 中,我有一个回收器 GridView 。在 GridView 中,我有用于显示动画 gif 的 WebView 网格。

我的 fragment 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;
}
}

Giphy测试适配器

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();
}
}

目标 我想将结果返回给 Activity A。

通常我会在 fragment B 中创建一个 Intent ,使用 put extra 添加所需的结果并调用 finish。

在上面的代码中,我在 shouldOverrideUrlLoading 方法中返回了数据。如何给它 fragment B 以便它可以创建 Intent 并返回?

最佳答案

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

关于android - 如何从回收者 View 将结果返回到 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29711940/

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