Ash*_*kla 5 android listview android-gridview
我想要实现Fig-1,但我被图2所困,并且无法将完整的gridview视为Listview的标题.
正如您所看到的,gridview未完全显示并隐藏在图2中的Listview后面
Gridview 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>
Run Code Online (Sandbox Code Playgroud)
Listview 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>
Run Code Online (Sandbox Code Playgroud)
片段代码:
@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;
}
Run Code Online (Sandbox Code Playgroud)
提前致谢.
大家好,我使用 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();
}
Run Code Online (Sandbox Code Playgroud)
编辑:
最后给出准确答案:
对于偶数视图集:
params.height = height/2 + 20;
Run Code Online (Sandbox Code Playgroud)
对于奇数个视图集:
params.height = ((height - (height / 3)) - (itemHeight * count)) + 20 + (count * 5);
Run Code Online (Sandbox Code Playgroud)
在哪里 :
我使用 5 作为比较的数字 bcoz 在 Adapters Count 的这个值之后,gridview 和 listview 之间的空间变化随着固定值的增加而增加。
5之前的空格在else部分处理。
itemHeight 是空间增加的增量值
20是gridview和listview之间的边距空间
(count x 5) 是随着元素增加而管理边距的值。
bcoz 它为我提供了双倍的 Gridview 高度空间,高于 Listview 的偶数视图
和Gridview + 其高度空间的一半用于奇数视图
希望能帮助到你 :)
| 归档时间: |
|
| 查看次数: |
658 次 |
| 最近记录: |