华域联盟 Andriod Android解决ScrollView下嵌套ListView和GridView中内容显示不全的问题

Android解决ScrollView下嵌套ListView和GridView中内容显示不全的问题

最近为公司做的一个Demo里面用到了ScrollView嵌套了GridView和ListView,然而在嵌套的时候我发现GridView和ListView都是不能完全显示,显示的基本上都是单行的数据,最后查找资料和翻阅文档看到原因是ListView和GridView的绘制过程中在ScrollView中无法准确的测量自身的高度,而且listVIew和GridView抢占了焦点,使得ListView和GrideView具有自身的显示的效果,这样就测量出显示一行条目即可的距离,其他的条目根据自身的滑动显示。

我的XMl的部分代码如下:

<ScrollView 
  android:layout_height="match_parent"
  android:layout_width="fill_parent"
  android:scrollbars="none"
  >
  <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#23000000"
    android:orientation="vertical"
    android:paddingTop="-1dp" >
    <android.support.v4.view.ViewPager
      android:id="@+id/home_pager"
      android:layout_width="fill_parent"
      android:layout_height="100dp" >
    </android.support.v4.view.ViewPager>
    <GridView
      android:id="@+id/gv_home"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:numColumns="5"
      android:paddingBottom="10dp"
      android:paddingTop="13dp" >
    </GridView>
    <LinearLayout
      android:id="@+id/ll_clickin"
      android:layout_width="fill_parent"
      android:layout_height="30dp"
      android:layout_marginBottom="10dp"
      android:layout_marginLeft="10dp"
      android:layout_marginRight="10dp"
      android:background="@drawable/shape_home"
      android:orientation="horizontal" >
      <ImageView
        android:layout_width="85dp"
        android:layout_height="wrap_content"
        android:src="@drawable/click_in" />
      <TextView
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:singleLine="true"
        android:text="限时抢购 ,快点进入吧!"
        android:textSize="18sp" />
    </LinearLayout>
    <ListView
      android:id="@+id/list_home"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:background="#ffffff" >
    </ListView>
  </LinearLayout>
</ScrollView>

显示的效果是这样的其中的Listview和GridView是可以滑动的就是显示不全

用自己写的方法之后才显示出来了所有的条目 

那就不再废话了 把我个人研究的代码呈上

首先是关于ListView的 (注意此方法必须方到SetAdapter()方法之后执行)

这是控件的查找

list_home = (ListView) view.findViewById(R.id.list_home);
list_home.setAdapter(new MyListViewAdapter(grideview_List));
 list_home.setFocusable(false);//词句加不加像也没有什么影响,我是加的
 //setAdapter之后调用
 getListViewSelfHeight(list_home);

这是getListViewSelfHeight(ListView youListview)

public void getListViewSelfHeight(ListView listView) {  
      // 获取ListView对应的Adapter  
    ListAdapter listAdapter = listView.getAdapter();  
    //健壮性的判断
    if (listAdapter == null) {  
      return;  
    } 
    // 统计所有子项的总高度  
    int totalHeight = 0;  
    for (int i = 0, len = listAdapter.getCount(); i < len; i++) {  
      // listAdapter.getCount()返回数据项的数目  
      View listItem = listAdapter.getView(i, null, listView);  
      // 调用measure方法 传0是测量默认的大小
      listItem.measure(0, 0);  
      totalHeight += listItem.getMeasuredHeight();  
    }  
    //通过父控件进行高度的申请
    ViewGroup.LayoutParams params = listView.getLayoutParams();  
    //listAdapter.getCount() - 1 从零开始 listView.getDividerHeight()获取子项间分隔符占用的高度 
    params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));  
    listView.setLayoutParams(params);  
  }

下面是GridView的方法和ListView的测量的方法基本一样  但是listView是单行条目的不用在担心列的问题问GridView则是需要进行自己分行和自己分列的 所以要注意一下

        gv_home = (GridView) view.findViewById(R.id.gv_home);
  gv_home.setSelector(new ColorDrawable(Color.TRANSPARENT));
 home_pager.setFocusable(false);
 gv_home.setAdapter(new MyGrigeViewAdapter(grideview_List));
 getGridViewSelfHeight(gv_home);

下面是getGridViewSelfHeight(GridView youGrideView)(这个方法能解决问题但是感觉不是很好灵活性太差 我用的获取的列数始终获取不到,有看神看到了 给我回复)

public void getGridViewSelfhetght(GridView gridView) {  
    // 获取GridView对应的Adapter  
    ListAdapter adapter = gridView.getAdapter();  
    if (adapter == null) {  
      return;  
    }  
    int totalHeight = 0;  
    for (int i = 0, len = adapter.getCount(); i < len; i++) {  
      // gridView.getCount()返回数据项的数目  
      View listItem = adapter.getView(i, null, gridView);  
      // 计算子项View 的宽高  
      listItem.measure(0, 0);  
      //此处方法并不好
      //5其中5是我们在Xml中的android:numColumns="5"
      //FontDisplayUtil.dip2px(MyGlobalApplication.getContext(),3)设置下边据
      totalHeight += listItem.getMeasuredHeight()/5+FontDisplayUtil.dip2px(MyGlobalApplication.getContext(),3);  
    }  
    ViewGroup.LayoutParams params = gridView.getLayoutParams(); 
    params.height = totalHeight;  
    gridView.setLayoutParams(params);  
  }

下面是FontDisplayUtil类

import android.content.Context;
/**
 * dp、sp 、 px之间的相互转化的工具类
 */
public class FontDisplayUtil {
 /**
 * 将px值转换为dip或dp值,保证尺寸大小不变
 */
 public static int px2dip(Context context, float pxValue) {
 final float scale = context.getResources().getDisplayMetrics().density;
 return (int) (pxValue / scale + 0.5f);
 }
 /**
 * 将dip或dp值转换为px值,保证尺寸大小不变
 */
 public static int dip2px(Context context, float dipValue) {
 final float scale = context.getResources().getDisplayMetrics().density;
 return (int) (dipValue * scale + 0.5f);
 }
 /**
 * 将px值转换为sp值,保证文字大小不变
 */
 public static int px2sp(Context context, float pxValue) {
 final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
 return (int) (pxValue / fontScale + 0.5f);
 }
 /**
 * 将sp值转换为px值,保证文字大小不变
 */
 public static int sp2px(Context context, float spValue) {
 final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
 return (int) (spValue * fontScale + 0.5f);
 }
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对华域联盟的支持。如果你想了解更多相关内容请查看下面相关链接

本文由 华域联盟 原创撰写:华域联盟 » Android解决ScrollView下嵌套ListView和GridView中内容显示不全的问题

转载请保留出处和原文链接:https://www.cnhackhy.com/106267.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

Android自定义View实现简单文字描边功能

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们