AndroidOptimist AndroidOptimist - 2 months ago 31
Android Question

Listview as a detail fragment in master/detail view

Am creating a master/detail fragment from this link. This works fine, now here instead of textview in detail fragment class i would like to implement listviews, (ie) i would like to display the details of the masterview as a child listview, hence i tried
detail.xml:

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

<ListView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:id="@+id/listview1"
/>"
<ListView
android:layout_width="120dp"
android:layout_height="wrap_content"
android:id="@+id/listview2"
/>"

</LinearLayout>


and in my detailFragment class

public class DetailFragment1 extends ListFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

String[] values = new String[] { "1", "2", "3",
"4" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);

}


My problem is child listview is displaying at initial stage but i want to display this listview when i click the first row of master page.Am new to fragments, so help me in achieving this. Thanks in advance..

Answer

Instead of doing like this, you can design a layout which contains ListView as master and frameLayout container. In frameLayout container, you can dynamically inflate layout, which is designed in such a way that you can have child listview and details view.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".NavigationActivity" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@color/strip_background"
        android:layout_weight="1"
        android:orientation="vertical" >

        <ListView
            android:id="@+id/nav_lv_transactions"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="none" >

        </ListView>

    </LinearLayout>

    <FrameLayout
        android:id="@+id/nav_fl_frag_container"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="14"
        android:background="@android:color/darker_gray" >

    </FrameLayout>

</LinearLayout>

In onclick listerner put this code,

Bundle arguments = new Bundle();
arguments.putString(ARG_ITEM_ID,
"Some Name");
MyClassFragment newFragment = new MyClassFragment();
newFragment.setArguments(arguments);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.nav_fl_frag_container, newFragment);
transaction.commit();

Try this. It will work