2012-02-27 16 views
0

ActivityGroupsの代わりにFragmentsを使用するように私のソリューションを変更してみました。その上にあるListViewに応じて私のUIの一部を変更できる例が見つかりました。私がしたいのは、アイテムがクリックされたときに新しいレイアウトのみが表示されるようにアイテムをクリックすると、それ自体を置き換えるListViewを持つことです。これは可能ですか?フラグメントをビューに置き換える

あなたは、私が使用しています関連するコードの例を参照してくださいよ下:

public class FragmentTestActivity extends FragmentActivity implements OnItemClickListener 
{ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    ListView l = (ListView) findViewById(R.id.number_list); 
    ArrayAdapter<String> magzTitles = new ArrayAdapter<String>(getApplicationContext(), 
       android.R.layout.simple_list_item_1, 
        new String[]{"Cupcake", 
          "TaberTHULE", 
          "Lite"}); 
    l.setAdapter(magzTitles); 
    l.setOnItemClickListener(this); 
} 

public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
{ 
    Fragment testFragment = new TestFragment(position++); 
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
    ft.replace(R.id.the_frag, testFragment); 
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 
    ft.addToBackStack(null); 
    ft.commit(); 
} 
} 

main.xml

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

<ListView 
    android:id="@+id/number_list" 
    android:layout_width="150dp" 
    android:layout_height="match_parent" 
    /> 

<fragment class="com.android.saket.fragments.TestFragment" 
    android:id="@+id/the_frag" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    /> 

</LinearLayout> 

答えて

2

は、これは私がやるだろう何、可能であるようでframeLayoutを使用することです2つの異なるフラグメントがスイッチアウトされます(切り替えはあなたのアクティビティで行われます)。これはかなり簡単ですし、例を見つけるのに苦労するべきではありません。

+0

リストビューもフラグメントでなければなりませんか? – CodePrimate

+0

私は単に私がしようとしていることをしている例を見つけることができませんでした。あなたが私にリンクを提供する可能性がありますか? – CodePrimate

+0

リストビューはフラグメントである可能性がありますが、それはリストフラグメント内にあるため、そうである必要はありません。以下は、正しいと思われるものへのリンクです:http://developer.android.com/resources/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentHideShowSupport.html – Warpzit

関連する問題