Reputation: 6666
I was looking into changing my solution to use Fragments instead of ActivityGroups and found an example which allowed me to change the part of my ui depending on a ListView lying above it. What I want to do is have a ListView which, when an item is clicked, replaces itself so that only the new layout is visible - Is this possible?
Below you'll see the relevant code example I am using :
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>
Upvotes: 0
Views: 1706
Reputation: 28152
This is possible, what I'd do is to use a Framelayout as the container and then have two different fragments which will be switched out (the switching happens in your activity). This is pretty straight forward and you should not have troubles finding examples.
Upvotes: 2