【问题标题】:Gridview not showing properly as a header of ListviewGridview 没有正确显示为 Listview 的标题
【发布时间】:2016-05-01 08:49:09
【问题描述】:

我想实现 Fig-1,但我卡在 Fig-2,无法将完整的 gridview 视为 Listview 的标题。

如您所见,gridview 没有完全显示并隐藏在图 2 中的 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;
}

提前致谢。

【问题讨论】:

  • 提供 xml 以便我们提供帮助。
  • 因为你提到了高度,所以只需最小化高度并检查
  • 我添加了xml文件
  • @AshishShukla 在调用 setAdater 后获取 gridview 计算 gridview 的高度并在布局参数中设置为高度
  • 其实你不需要手动计算尺寸,你可以通过覆盖网格视图的onmeasure方法来完成。请检查链接以获取更多信息。 stackoverflow.com/a/22727503/3257178

标签: android listview android-gridview


【解决方案1】:

大家好,我的答案是使用 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 的这个值之后 计算之间的空间变化 gridview 和 listview 以固定值递增。

  • 对于5之前的空格在else部分处理。

  • itemHeight 是空间增加的增量值

  • 20是gridview和listview之间的margin空间

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

因为它给了我 Gridview 高度空间的两倍 在 Listview 之上的偶数视图

Gridview + 一半的高度空间用于奇数个视图

希望对你有帮助:)

【讨论】:

  • 您必须像设置高度一样设置高度,因为网格视图是可滚动的,甚至列表视图也是可滚动的。因此,将来每当我们尝试将可滚动视图放在列表视图或任何其他可滚动视图中时,我们都需要设置内部视图的高度,而与方向无关。
【解决方案2】:

当你想膨胀你的 GridLayout 时也参考你的 listView 类似这样的东西:

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

还有一件事:将您的 relativeLayout 而不是您的 GridLayout 作为标题传递。

【讨论】:

    【解决方案3】:

    尝试在ListViewRelativeLayout 中添加android:layout_marginTop="some_value"

    【讨论】:

      【解决方案4】:

      尝试这种方式,将您的 Grid 和 List 放在单一布局中并使用 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库。如下图所示

      【讨论】:

      • 我也试过了,它不工作。如果你的代码工作,请发布完整的代码
      • 什么在你身边不起作用?你卡在哪里了? @AshishShukla
      • 感谢图书馆链接,但我不想这样做。
      • 正如您在上面的图 2 中看到的,gridview 隐藏在 listview 后面
      • 不客气。告诉我你在单一布局中使用 gird 和 list 卡在代码中的位置@AshishShukla
      猜你喜欢
      • 2019-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-09
      • 1970-01-01
      相关资源
      最近更新 更多