【问题标题】:Android - Passing value from ListFragment to another ListFragmentAndroid - 将值从 ListFragment 传递到另一个 ListFragment
【发布时间】:2014-01-10 09:36:45
【问题描述】:

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

我通过使用 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 中添加什么以进入项目列表?我需要使用另一个 ListFragment 还是只使用常规 ListActivity?

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

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

【问题讨论】:

  • 使用接口作为活动的回调。然后将值传递给另一个片段。片段到片段的通信是通过活动完成的。
  • 我会看看那个。请阅读我更新的问题。
  • 你可以使用扩展列表视图
  • 展开式listview的问题是item list太长。所以这是一个反模式。我会使用普通的列表视图,所以我可以实现加载更多按钮。
  • 正如我所说,使用接口作为活动的回调,然后与其他片段通信或启动新的列表活动并使用意图传递值。

标签: java android listview android-fragments


【解决方案1】:

您应该使用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));

【讨论】:

  • 我仍然无法理解这一点。如何访问category_id 并将其传递给接口?我做了界面部分,一切都运行良好,除了,当按下一个项目时,我无法从 fragment A 检索category_id。我需要 fragment B 中的 id 作为 JSON url 的参数。嗯。
  • 你能分辨出fragmentA.setMyFragmentListener(fragmentB));中的fragmentB是什么
【解决方案2】:

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

类别viewsfragment您在选择类别时调用此:

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

然后在活动中实现categorySelected 并将概览片段替换为CategoryFragment。

当您创建 CategoryFragment 时,将类别 ID 设置为参数。最好使用newInstance pattern 到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 并在代码中动态添加片段。

【讨论】:

    【解决方案3】:

    使用这个........

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

    【讨论】:

      【解决方案4】:

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

      FragmentA-------------------------->Activity---------- ------->FragmentB

      (定义Interface)(实现interface)(传递给FragmentB)

      Doc reference is Here

      【讨论】:

        猜你喜欢
        • 2016-04-07
        • 2019-01-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多