![](/img/trans.png)
[英]Android navigate one fragment to another is working but , cannot see other fragment's background
[英]Android : Navigate to another fragment
我有一個帶有4個片段的ViewPager
和TabLayout
設置,就像任何現代社交應用程序一樣。 我已經嘗試,嘗試,嘗試過,但是找不到解決我問題的答案。 在一個選項卡中,我想從片段導航到另一個片段,但是除了導航之外,它只是將其放在頂部,我仍然可以與上一個片段進行交互。 它並不能替代,而只是將其分層。
碼:
// 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>
片段具有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.