Android – 将值从ListFragment传递到另一个ListFragment

我有一个包含项目类别的列表视图。 当我按下列表中的类别标题时,它将显示包含所选类别中的项目的另一个列表视图。

我是通过使用ListFragment来做到这一点的。 我是否必须开始一项新活动并在意图中将类别ID一起传递?

这是我的ListFragment类别:

 public class CategoryFragment extends ListFragment implements OnItemClickListener { @Override public void onActivityCreated(Bundle savedInstanceState) { getListView().setOnItemClickListener(this); } @Override public void onItemClick(AdapterView adapterView, View view, int i, long l) { // what to do here } } 

我在onItemClick添加什么来转到Items List? 我是否需要使用另一个ListFragment或只使用常规ListActivity?

如何从解析的JSON中检索类别ID并将其传递给List项?

编辑:我使用Volley解析了JSON。 我正在考虑在类别ID的布局中创建一个不可见的TextView,所以我可以从那里拉它。 那可能吗?

FragmentActivity实现一个具有categorySelected(int categoryId)方法的接口。

在CategoryOverviewFragment中,您在选择类别时调用此方法:

 @Override public void onItemClick(AdapterView adapterView, View view, int i, long l) { ((CategorySelectedListener)getActivity()).categorySelected(i); } 

然后在活动中实现categorySelected并使用CategoryFragment替换概述片段。

创建CategoryFragment请将cateogry ID设置为参数。 最好使用newInstance模式来设置setArguments()

要使用类别详细信息片段替换类别概述列表片段,请使用FragmentManager来开始beginTransaction()然后replace()

假设类别概述片段是动态添加的而不是XML,请使用以下代码:

 CategoryFragment newFragment = CategoryFragment.newInstance(categoryIdSelected); FragmentTransaction transaction = getFragmentManager().beginTransaction(); transaction.replace(R.id.fragment_container, newFragment); transaction.addToBackStack(null); transaction.commit(); 

如果在XML中添加类别列表片段,则需要将其删除,将其更改为FrameLayout并在代码中动态添加片段。

你应该使用Interface ,它是将值传递给Fragment的“官方”方式。 请在此处查看此文档:

http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity

那你需要做什么:

FragmentA – > Activity – > FragmentB

一个看不见的TextView绝对不是正确的解决方案……

编辑更详细的解决方案:

您定义了一个接口(在单独的文件中或在FragmentA类中):

 public interface MyFragmentListener { public void onFragmentItemSelected(String url); } 

在你的FragmentA类中:

 MyFragmentListener myListener; public void setMyFragmentListener(MyFragmentListener listener) { myListener = listener; } // here I used the onClick method but you can call the listener whereever you like. public void onClick(View view) { if (myListener != null) { myListener.onFragmentItemSelected(url); } } 

声明你的FragmentB类:

 public class FragmentB extends Fragment implements MyFragmentListener { public void onFragmentItemSelected(String url) { // do what you want to do } } 

在您的活动中:

 // you tell your fragmentA that his listener is the FragmentB. And because you implemented the listener in FragmentB, everything is allright; fragmentA.setMyFragmentListener(fragmentB)); 

用这个 ……..

  list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { // getting values from selected ListItem int new_position = position; String disp = dataList.get(new_position).get("displaytext"); String msg = dataList.get(new_position).get("message"); String pht = dataList.get(new_position).get("photo"); String mt = dataList.get(new_position).get("messagetime"); String fpht = dataList.get(new_position).get("feedphoto"); String prdctprice = dataList.get(new_position).get( "productprice"); String pid = dataList.get(new_position).get("productid"); String cmntcount = dataList.get(new_position).get( "commentcount"); String storeid = dataList.get(new_position).get("storeid"); addfragment(new FeedChat(c, disp, msg, pht, mt, fpht, prdctprice, pid, storeid, cmntcount), true, FragmentTransaction.TRANSIT_FRAGMENT_OPEN); } }); 

两个Fragment之间的通信使用Interface通过Activity完成

Fragment A ————————–> Activity ——————– > Fragment B

(定义Interface )(实现interface )(传递给Fragment B)

Doc参考在这里