Gridview 未正确显示为 Listview 的 header

Gridview not showing properly as a header of Listview

我想实现图 1 但我 卡在图 2 中,无法看到完整的网格视图 header 的列表视图。

如图 2 所示,gridview 没有完全显示并隐藏在 Listview 后面

网格视图 xml :

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

<GridView
    android:id="@+id/gridview"
    android:numColumns="2"
    android:stretchMode="columnWidth"
    android:cacheColorHint="#ffffffff"
    android:gravity="center"
    android:verticalSpacing="5dp"
    android:horizontalSpacing="5dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</RelativeLayout>

列表视图 xml :

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >

<ListView
    android:id="@+id/listview"
    android:scrollbars="vertical"
    android:background="#fff"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

</ListView>

</RelativeLayout>

片段代码:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


    view=inflater.inflate(R.layout.fragmentpage_layout, null);

    listView=(ListView)view.findViewById(R.id.listview);

    header=inflater.inflate(R.layout.gridview_layout,null);

    gridView=(GridView)header.findViewById(R.id.gridview);
    listView.addHeaderView(header);
    gridViewAdapter=new CustomGridViewAdapter(getActivity(),images,toptexts, bottomtexts);
    listViewAdapter=new CustomListViewAdapter(getActivity(),images,toptexts,bottomtexts);

    gridView.setAdapter(gridViewAdapter);

    listView.setAdapter(listViewAdapter);


   return view;
}

提前致谢。

尝试这种方式,将您的网格和列表置于单一布局中并使用android:weightSum进行区分,以便它适用于所有设备,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="3dp"
    android:weightSum="2">

    <GridView
        android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:cacheColorHint="#ffffffff"
        android:gravity="center"
        android:numColumns="2"
        android:stretchMode="columnWidth" />

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#fff"
        android:scrollbars="vertical" />

</LinearLayout>

片段代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    view=inflater.inflate(R.layout.fragmentpage_layout, null);

    listView=(ListView)view.findViewById(R.id.listview);
    gridView=(GridView)header.findViewById(R.id.gridview);

    //your code

    gridView.setAdapter(gridViewAdapter);
    listView.setAdapter(listViewAdapter);


   return view;

}

如果你想让整个 gridview 首先完全显示,下面应该是 listview,当你滚动时,gridview 应该向上,listview 应该跟在它后面,如图 1

检查这个 Awesome-MaterialDesign 库,简单 explaination.As 如下图所示

大家好,我使用 Gridview 作为 Listview 的页眉得到了答案。

我只是使用下面的方法计算了 Gridview 的分隔线高度,它完全符合我的要求。

   public static void setDynamicHeightGridView(GridView mListView,String oddeven) {
        ListAdapter mListAdapter = mListView.getAdapter();
        if (mListAdapter == null) {
            return;
        }
        int height = 0;
        int desiredWidth = View.MeasureSpec.makeMeasureSpec(mListView.getWidth(), View.MeasureSpec.UNSPECIFIED);


        for(int i = 0; i < mListAdapter.getCount(); i++){
            View listItem = mListAdapter.getView(i, null, mListView);
            listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
            height += listItem.getMeasuredHeight();
            itemHeight=listItem.getMeasuredHeight()/3;
        }

        ViewGroup.LayoutParams params = mListView.getLayoutParams();

        if(oddeven.equals("odd")){
            if(mListAdapter.getCount()>=5){
                int count=((mListAdapter.getCount()-5)/2) + 1;
                params.height = ((height - (height / 3)) - (itemHeight * count)) + 20 + (count * 5);

            }else{
                params.height = height - (height / 3) + 20;
            }

        }else if(oddeven.equals("even")) {
            params.height = height/2 + 20;
        }


        mListView.setLayoutParams(params);
        mListView.requestLayout();
    }

已编辑:

最后准确答案:

偶数观看次数设置:

params.height = height/2 + 20;

奇数观看次数设置:

params.height = ((height - (height / 3)) - (itemHeight * count)) + 20 + (count * 5);

其中:

  • 我用5来比较 bcoz 之后 Adapters Count 这个值在 space 之间的变化 gridview 和 listview 以固定值增加。

  • 对于5之前的space在else部分处理

  • itemHeight是space增加的增量值

  • 20 是 gridview 和 listview 之间的边距 space

  • (count x 5) 是随着元素的增加管理边距的值。

bcoz 它给了我 Gridview 高度 space 的两倍 高于 Listview 的偶数视图

Gridview + 其高度的一半 space 奇数视图

希望对您有所帮助:)

当你想膨胀你的 GridLayout 时也引用你的 listView 像这样:

header = LayoutInflater.from(getActivity()).inflate(R.layout.your_header, mListView, false);

还有一件事:传递你的 relativeLayout 而不是你的 GrideLayout 作为 header。

尝试在 ListView

RelativeLayout 中添加 android:layout_marginTop="some_value"