通过ListView将一个片段替换为另一个片段

时间:2013-12-14 11:01:10

标签: android android-listview android-fragments fragment-tab-host

我希望在单击所述列表视图上的项目时,将具有列表视图的片段替换为另一个片段。

这是我的MainActivity

public class MainActivity extends FragmentActivity {
private FragmentTabHost hostTab;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    hostTab = (FragmentTabHost)findViewById(android.R.id.tabhost);
    //hostTab = (TabHost) findViewById(R.id.tabhost);
    hostTab.setup(this,getSupportFragmentManager(), android.R.id.tabcontent);

    hostTab.addTab(
            hostTab.newTabSpec("tab1").setIndicator("Tab 1",
                    getResources().getDrawable(android.R.drawable.star_on)),
            FragmentTab.class, null);
    hostTab.addTab(
            hostTab.newTabSpec("tab2").setIndicator("Tab 2",
                    getResources().getDrawable(android.R.drawable.star_on)),
            FragmentTab1.class, null);
    hostTab.addTab(
            hostTab.newTabSpec("tab3").setIndicator("Tab 3",
                    getResources().getDrawable(android.R.drawable.star_on)),
            FragmentTab2.class, null);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

这是我的activity_main.xml

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/replace"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v4.app.FragmentTabHost 

    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

   <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:orientation="horizontal" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
   </LinearLayout>
</android.support.v4.app.FragmentTabHost>

这是“Tab 1”的代码。不要介意“Tab 2”和“Tab 3”。这两个只显示一些文本视图

public class FragmentTab extends ListFragment implements OnItemClickListener {

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    String[] values = new String[] { "Fragment1", "Fragment2", "Fragment3"};
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, values);
    setListAdapter(adapter);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {

    // Create new fragment and transaction
    Fragment newFragment = new FragmentInListView();
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    //transaction.show(newFragment);
    // Replace whatever is in the fragment_container view with this fragment,
    // and add the transaction to the back stack
    transaction.replace(R.id.replace, newFragment);
    transaction.addToBackStack(null);
    transaction.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    // Commit the transaction
    transaction.commit();

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub

}   

 }

这是“Tab 1”的布局xml

 <LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >

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

</ListView>

此处是单击列表视图中的项目时将出现的类

public class FragmentInListView extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    View view = inflater.inflate(R.layout.fragment_layout1,
            container, false);
        TextView tv= (TextView) view.findViewById(R.id.text);
        tv.setText("Fragment in List View"); 
        return view;

}

 }

请帮助,请随时索取上传的代码。谢谢。

2 个答案:

答案 0 :(得分:1)

您应该在Activity中创建示例'ItemSelected'的方法。然后将选定元素的id从FragmentList发送到Activity和Activity替换片段。 Here is best example我建议您下载示例以查看片段的工作原理

答案 1 :(得分:0)

我通过替换:

解决了这个问题

transaction.replace(R.id.replace,newFragment);

transaction.replace(R.id.tabcontent,newFragment);