Listview作为主/详细视图中的详细信息片段

时间:2013-10-24 07:35:56

标签: android android-listview android-fragments

this链接创建主/明细片段。这工作正常,现在在这里而不是textview详细片段类我想实现listviews,(即)我想显示masterview的细节作为子列表视图,因此我试过 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>

和我的detailFragment类

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);

     }

我的问题是子列表视图在初始阶段显示但是当我点击母版页的第一行时我想显示这个列表视图。这是片段的新内容,所以帮助我实现这一点。提前谢谢..

2 个答案:

答案 0 :(得分:1)

创建一个CallBack界面:

public interface Callbacks {

        public void onItemSelected(long id);
}

让片段补充它:

public class DetailFragment1 extends ListFragment implements CallBacks {

在ListFragment OnListitemClick中:

@Override
public void onListItemClick(ListView listView, View view, int position, long id) {     
    mCallbacks.onItemSelected(id);
}

在活动中:

Public class MyActivity extends FragmentActivity implements Callbacks {

@Override
public void onItemSelected(long id) {
    // Replace the detail fragment with the corresponding item selected in the list
}

答案 1 :(得分:0)

您可以设计一个包含ListView作为master和frameLayout容器的布局,而不是这样做。在frameLayout容器中,您可以动态地扩展布局,布局的设计使您可以拥有子列表视图和详细信息视图。

<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>

在onclick listerner中输入此代码,

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();

试试这个。它会起作用

相关问题