簡體   English   中英

Android-將值從ListFragment傳遞到另一個ListFragment

[英]Android - Passing value from ListFragment to another 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添加什么以轉到“項目列表”? 我需要使用另一個ListFragment還是僅使用常規ListActivity?

如何從解析的JSON檢索類別ID並將其傳遞給列表項?

編輯:我使用Volley解析JSON。 我正在考慮在布局中為類別ID創建一個不可見的TextView,以便可以從那里拉出它。 那可能嗎?

您應該使用Interface ,這是將值傳遞給Fragment的“官方”方式。 在這里查看此文檔:

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

因此,您需要做的是:

FragmentA->活動-> 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);

        }

    });

讓您的FragmentActivity實現一個具有categorySelected(int categoryId)方法的接口。

在CategoryOverviewFragment內,當您選擇一個類別時,您將其稱為:

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

然后,在活動中實現categorySelected並將總覽片段替換為CategoryFragment

創建CategoryFragment片段時,將CategoryFragment ID設置為參數。 最好使用newInstance模式設置setArguments()

要將類別概述列表片段替換為類別詳細信息片段,請使用FragmentManagerbeginTransaction() ,然后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通過Activity完成兩個Fragment之間的通信

Fragment A --------------------------> Activity -------------------- > Fragment B

(定義Interface )(實現interface )(傳遞給Fragment B)

文檔參考在這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM