0

上記のリストビューのアイテムがクリックされたときに、リストビューを持つフラグメントを別のフラグメントに置き換えたい。

ここに私の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>

これが「タブ 1」のコードです。「タブ 2」と「タブ 3」は気にしないでください。2つはテキストビューのみを示しています

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

}   

 }

これが「タブ 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;

}

 }

助けてください。アップロードするコードをお気軽にお尋ねください。よろしくお願いします。

4

2 に答える 2

1

アクティビティで「ItemSelected」などのメソッドを作成する必要があります。次に、選択した要素の ID を FragmentList から Activity に送信し、Activity でフラグメントを置き換えます。これが最良の例です。サンプルをダウンロードして、フラグメントがどのように機能するかを確認することをお勧めします

于 2013-12-14T11:05:39.963 に答える
0

以下を置き換えることで問題を解決しました。

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

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

于 2013-12-16T17:49:37.087 に答える