GridView作为ListView android的头文件

时间:2014-12-30 10:31:35

标签: android android-listview android-gridview

我们可以使用gridView作为listView的标头吗? gridView仅显示一行并跳过其他行。 listView工作正常的地方。 这是标题xml

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="fill_parent">

    <TextView
        android:id="@+id/section_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <GridView
        android:id="@+id/gridView1"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_below="@+id/section_label" 
        android:columnWidth="50dp"
        android:numColumns="auto_fit" />
</RelativeLayout>

这是fragment_main xml

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

 <ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:layout_below="@+id/gridView1" />
</RelativeLayout>

这是我用于setAdapters的代码

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
       super.onCreateView(inflater, container, savedInstanceState);
    View rootView = inflater.inflate(R.layout.fragment_main, container,
            false);
   mListView = (ListView) rootView.findViewById(R.id.list); 

        //header on each fragment
        View headerView = inflater.inflate(
                R.layout.fragment_header, null);

        mTextView = (TextView) headerView.findViewById(R.id.section_label);
        mTextView.setText("Title");

        mGridView = (GridView) headerView
                .findViewById(R.id.gridView1);
        mGridView.setAdapter(new ImageAdapter( mThumbIds , getActivity().getApplicationContext() ));

        mListView.addHeaderView(headerView); 
        //header on each fragment

        mTaskAdapter = new TaskAdapter(getActivity(),
                R.layout.listview_row, data);
        mListView.setAdapter(mAdapter);
       }

0 个答案:

没有答案