簡體   English   中英

Android:導航到另一個片段

[英]Android : Navigate to another fragment

我有一個帶有4個片段的ViewPagerTabLayout設置,就像任何現代社交應用程序一樣。 我已經嘗試,嘗試,嘗試過,但是找不到解決我問題的答案。 在一個選項卡中,我想從片段導航到另一個片段,但是除了導航之外,它只是將其放在頂部,我仍然可以與上一個片段進行交互。 它並不能替代,而只是將其分層。

碼:

// Chat fragment : Inside the onCreateView fun
this.loadConvos({
            chats ->
            this.chatsArray = chats
            this.chatsArray.sortBy { it.timestamp}
            this.chatsArray.reverse()

            listView.adapter = ChatBaseAdapter(this.chatsArray, context)
            listView.setOnItemClickListener {
                parent, view, position, id ->
                this.chatID = this.chatsArray[position].chatID!!
                Toast.makeText(context, "Position Clicked: " + position, Toast.LENGTH_SHORT).show()

                childFragmentManager
                        .beginTransaction()
                        .replace(R.id.chatFragmentLayout, MessagesFragment())
                        .addToBackStack(null)
                        .commit()
            }
        }, {
            error ->
            print(error)
        })

該函數只是將帶有用戶可以使用的聊天詳細信息的listView加載,我可以點擊,Toast將為我提供單元格的位置,但是當它commit()時,它只是將MessagesFragment()放在ChatsFragment()之上。 我還想知道如何將信息傳遞到下一個片段。 與我所知道的常規Bundle/Intent方法不同,我沒有這種方法來傳遞數據。

在viewpager上的XML聊天:

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/chatFragmentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"

tools:context="io.jedi.jedi.fragments.ChatFragment">

<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</FrameLayout>

XML消息

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/messagesFragmentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"

tools:context="io.jedi.jedi.fragments.MessagesFragment">

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Messages" />

</FrameLayout>

如果您要切換到的Fragment在ViewPager中是一個,則不應通過FragmentTransaction手動對其進行更改。 您應該改為在ViewPager上調用setCurrentItem() (請參閱在線文檔中的此鏈接 )。 如果此調用是通過這些Fragments之一進行的,則還需要具有某種形式的接口以在父Activity和該Fragment之間進行通信(請參見此處 )。

至於第二個問題,如果您打算在ViewPager中有一個不斷變化的Fragment ,則您ViewPager一下。 否則,您應該查看New Instance類型的模式,如本文所討論

片段具有void setArguments(Bundle)Bundle getArguments()方法,您可以使用與對Activity使用意圖相同的方法。

例如,來自官方文檔

public class CountingFragment extends Fragment {

    private int mNum;

    public static CountingFragment newInstance(int num) {
        CountingFragment f = new CountingFragment();

        Bundle args = new Bundle();
        args.putInt("num", num);
        f.setArguments(args);

        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mNum = getArguments() != null ? getArguments().getInt("num") : 1;
    }
}

或在科特林:

class CountingFragment : Fragment() {

    companion object {
        fun newInstance(number: Int): CountingFragment {
            return CountingFragment().apply {
                arguments = Bundle().apply {
                    putInt("number", number)
                }
            }
        }
    }

    private var number: Int = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        number = arguments?.getInt("number") ?: 0
    }

}

暫無
暫無

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

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