2012-02-27 2 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이를 사용하는 것입니다 컨테이너로 이동 한 다음 전환 할 두 개의 다른 조각이 있습니다 (전환이 활동에서 발생 함). 이것은 꽤 솔직하며 예제를 찾는 데 어려움을 겪지 않아야합니다.

+0

그래서 listview도 조각이어야합니까? – CodePrimate

+0

나는 내가하려는 일을하는 예제를 찾을 수 없었습니다. 나에게 링크를 줄 수 있니? – CodePrimate

+0

listview는 조각 일 수 있지만 listfragment에있을 수도 있고 그렇지 않아도됩니다. 다음은 올바른 소리로 연결되는 링크입니다. http://developer.android.com/resources/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentHideShowSupport.html – Warpzit

관련 문제