0
如果用戶單擊其中一個列表項目,我一直試圖用另一片段替換片段。現在的問題是其他片段正在顯示,但它並沒有取代以前的幀,但它只是顯示在前一個片段的頂部。這是我如何打電話給我的列表項點擊新片段:此片段相關用另一個片段替換一個片段時重疊
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String sender = ((TextView) view.findViewById(R.id.sender)).getText() .toString();
String subject = ((TextView) view.findViewById(R.id.subject)).getText() .toString();
String fname = ((TextView) view.findViewById(R.id.file_name)).getText() .toString();
Fragment frag =new DisplayFragment();
Bundle bundle = new Bundle();
bundle.putString("file_name", fname);
frag.setArguments(bundle);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_frame, frag);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
和活動是:
<FrameLayout
android:id="@+id/fragment_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
現在新片段的代碼是:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.activity_displayfile, container, false);
return rootView;
}
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
TextView tv=(TextView)getView().findViewById(R.id.textView1);
Bundle bundle = this.getArguments();
String myfile = (String)bundle.get("file_name");
tv.setText(myfile);
}
及其相關活動是:
<FrameLayout
android:id="@+id/display_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
所需的輸出應該是新片段中的textview應該替換之前片段的全部內容。但點擊我得到的項目後的輸出是: 這個「嗨」應該單獨顯示不與列表。看來我的代碼是正確的。提前致謝。
沒有放置框架佈局內的列表視圖沒有幫助 –
請參閱更新 – Sanjeev
一個選項是替換我理解的框架佈局,但什麼是「在片段替換中使用列表視圖ID」你怎麼做? –