我们大家都知道ListView,GridView加载数据项,如果数据项过多时,就会显示滚动条。ScrollView组件里面只能包含一个组件,当ScrollView里面嵌套listView,GridView时,由于ScrollView,ListView,GridView都有滚动条,系统默认ScrollView的滚动条,ListView,GridView的滚动条会被忽略,就会出现数据加载不全的问题。解决这种问题,要利用自定义布局的知识解决,具体实现如下所示:

一.ListView数据加载不全问题的解决的解决

1.MyListViewActivity.class

public class MyListViewActivity extends AppCompatActivity {

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_list_view);
}
}

  

2.MyListView.class

/**
* 自定义listView,解决scrowView嵌套listView,
* listView里面的滚动条不显示导致数据显示不全的问题
*/ public class MyListView extends ListView {
public MyListView(Context context) {
super(context);
} public MyListView(Context context, AttributeSet attrs) {
super(context, attrs);
} public MyListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
} @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int exeSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2,MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, exeSpec); }
}

3.strings.xml

  

<resources>
<string name="app_name">泰洋</string>
<string-array name="userName">
<item>马里山</item>
<item>陈渠珍</item>
<item>金日成</item>
<item>金成日</item>
<item>姜涛</item>
<item>李想</item>
<item>牛奔</item>
<item>金鑫</item>
<item>乐嘉</item>
<item>刘梅</item>
<item>樊城</item>
<item>卢决</item>
<item>邓乐</item>
<item>陈吉</item>
</string-array>
</resources>

  

4.activity_my_list_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_my_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.langdon.taiyang.androidtest.autoView.MyListViewActivity"> <!--方式一
<ListView
android:entries="@array/userName"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="button 1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="button 2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="button 3"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="button 4"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="button 5"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="button 6"
android:layout_width="match_parent"
android:layout_height="wrap_content" />-->
<!--方式二
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:entries="@array/userName"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="button 1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="button 2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="button 3"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="button 4"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="button 5"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="button 6"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>-->
<!--方式三-->
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.langdon.taiyang.androidtest.autoView.MyListView
android:entries="@array/userName"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="button 1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="button 2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="button 3"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="button 4"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="button 5"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="button 6"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
</LinearLayout>

  效果图如下:

总结:从左往右是:方法一,方法二,方法三的图片;方法二显示的就是ListView加载数据不全的情况

二.GridView数据加载不全问题的解决的解决

1.MyGridViewActivity.class

public class MyGridViewActivity extends AppCompatActivity {
private GridView gv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_grid); gv = (GridView) findViewById(R.id.gv_my_gridView);
gv.setAdapter(new MyAdapter(this));
gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MyGridViewActivity.this,"gridview"+position,Toast.LENGTH_LONG).show();
}
});
} //自定义适配器
static class MyAdapter extends BaseAdapter {
private Context context;
public MyAdapter(Context context){
this.context = context;
} private int[] imges = {R.mipmap.gradview_dog_one,R.mipmap.gradview_dog_two,R.mipmap.gradview_dog_three,
R.mipmap.gradview_dog_one,R.mipmap.gradview_dog_two,R.mipmap.gradview_dog_three,
R.mipmap.gradview_dog_one,R.mipmap.gradview_dog_two,R.mipmap.gradview_dog_three,
R.mipmap.gradview_dog_one,R.mipmap.gradview_dog_two,R.mipmap.gradview_dog_three,};
private String[] names = {"拉布拉多","泰迪","柴犬","牧羊犬",
"拉布拉多","泰迪","柴犬","牧羊犬",
"拉布拉多","泰迪","柴犬","牧羊犬"};
@Override
public int getCount() {
return names.length;
} @Override
public Object getItem(int position) {
return names[position];
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
View view = layoutInflater.inflate(R.layout.gridview_item,null); ImageView iv = (ImageView) view.findViewById(R.id.iv_gridview_img);
TextView tv = (TextView) view.findViewById(R.id.tv_gridview_name);
iv.setImageResource(imges[position]);
tv.setText(names[position]);
return view;
}
}
}

  

2.MyGridView.class

/**
* 自定义gridView,解决scrowView嵌套gridView,
* gridView里面的滚动条不显示导致数据显示不全的问题
*/ public class MyGridView extends GridView {
public MyGridView(Context context) {
super(context);
} public MyGridView(Context context, AttributeSet attrs) {
super(context, attrs);
} public MyGridView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
} @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int exeSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2,MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, exeSpec);
}
}

  

3.activity_my_grid.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.langdon.taiyang.androidtest.autoView.MyGridViewActivity"> <!--方式一
<GridView
android:id="@+id/gv_my_gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="4"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp" />
<Button
android:text="button 1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="button 2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="button 3"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="button 4"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="button 5"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="button 6"
android:layout_width="match_parent"
android:layout_height="wrap_content" />-->
<!--方式二:scrollView中只能有一个组件
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<GridView
android:id="@+id/gv_my_gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="4"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp" />
<Button
android:text="button 1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="button 2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="button 3"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="button 4"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="button 5"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="button 6"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>-->
<!--方式三-->
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.langdon.taiyang.androidtest.autoView.MyGridView
android:id="@+id/gv_my_gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="4"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp" />
<Button
android:text="button 1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="button 2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="button 3"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="button 4"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="button 5"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="button 6"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView> </LinearLayout>

  

效果图如下:

总结:从左往右依次是:方法一,方法二,方法三的图片;方法二显示的就是GridView加载数据不全的情况

												

ScrollView嵌套ListView,GridView数据加载不全问题的解决的更多相关文章

  1. Android之ListView分页数据加载

    1.效果如下: 实例如下:  上图的添加数据按钮可以换成一个进度条  因为没有数据所以我加了一个按钮添加到数据库用于测试:一般在服务器拉去数据需要一定的时间,所以可以弄个进度条来提示用户: 点击加载按 ...

  2. 关于ligerui 中 grid 表格的扩展搜索功能在远程数据加载时无法使用的解决办法

    要想使用grid里的扩展搜索功能,除了要引用ligerui主要的js文件外,还必须引入下面的JS文件: 1.Source\demos\filter\ligerGrid.showFilter.js 2. ...

  3. ScrollView嵌套ListView嵌套GridView的上下拉以及加载更多

    ScrollView 效果 ScrollView 说明 一个ScrollView 嵌套ListView 嵌套GridView的上拉加载更多,下拉刷新的demo. 主要是重写了GridView和Lsit ...

  4. android中ScrollView嵌套ListView或GridView显示位置问题

    Android中ScrollView中嵌套ListView或GridView时在开始进入界面时总是显示中间位置,开头的位置显示不出来.这种情况下只需要在ScrollView的父控件中添加以下两行代码即 ...

  5. Android: 阻止ScrollView随着数据加载自动滚动

    当ScrollView中有类似GridView的控件时,当数据加载后ScrollView会自动滚动.要阻止这种事情发生,我们需要做的是在ScrollView的下层容器中添加android:descen ...

  6. ListView用法及加载数据时的闪烁问题和加载数据过慢问题

    ListView介绍及添加数据时的闪烁问题 1.     ListView类 1.1 ListView常用的基本属性: (1)FullRowSelect:设置是否行选择模式.(默认为false) 提示 ...

  7. ScrollView嵌套ListView、GridView,进入页面显示的位置并不是在最顶部,而是在中间部分问题

    在Android项目的开发中,经常会遇到一些布局,可能需要在ScrollView中嵌套ListView或.GridView来实现, 是在使用的过程总又遇到了一个新的问题,就是如果在ScrollView ...

  8. XE7 & FMX 那些年我们一起上过的控件:ListView 之 (3) 加载数据时如何显示自定义样式

    本文介绍一下ListView下如何加载数据.及使用进度条反馈当前进度给用户. 注意: 原创作品,请尊重作者劳动成果,转载请注明出处!!!原文永久固定地址:http://www.cnblogs.com/ ...

  9. Android——MeasureSpec学习 - 解决ScrollView嵌套ListView和GridView冲突的方法

      原文地址:http://blog.csdn.net/yuhailong626/article/details/20639217   在自定义View和ViewGroup的时候,我们经常会遇到int ...

随机推荐

  1. 按需加载.js .css文件

    首先,理解按需加载当你需要用到某个js里面的函数什么鬼,或者某个css里的样式的时候你才开始加载这个文件. 然后是怎样实现的,简单来说就是在js中动态的createElem<script> ...

  2. OpenCASCADE Shape Location

    OpenCASCADE Shape Location eryar@163.com Abstract. The TopLoc package of OpenCASCADE gives resources ...

  3. spring源码分析之context

    重点类: 1.ApplicationContext是核心接口,它为一个应用提供了环境配置.当应用在运行时ApplicationContext是只读的,但你可以在该接口的实现中来支持reload功能. ...

  4. STM32F429 LCD程序移植

    STM32F429自带LCD驱动器,这一具有功能给我等纠结于屏幕驱动的程序员带来了很大的福音.有经验的读者一定有过这样的经历,用FSMC驱动带由控制器的屏幕时候,一旦驱动芯片更换,则需要重新针对此驱动 ...

  5. “RazorEngine.Templating.TemplateParsingException”类型的异常在 RazorEngine.NET4.0.dll 中发生,但未在用户代码中进行处理 其他信息: Expected model identifier.

    这个问题是由于在cshtml中 引用了model这个单词  它可能和Model在解析时有冲突. 解决方法:把model换成别的单词就可以了.

  6. 如何解决流程开发中SheetRadioButtonList页面取值问题

    分享一个常见的取值问题. 应用场景: SheetRadioButtonList控件,点击其中一项执行事件操作.如果是页面加载的情况下,值就无法取到. 具体原因如下: 我给SheetRadioButto ...

  7. Android 死锁和重入锁

    死锁的定义: 1.一般的死锁 一般的死锁是指多个线程的执行必须同时拥有多个资源,由于不同的线程需要的资源被不同的线程占用,最终导致僵持的状态,这就是一般死锁的定义. package com.cxt.t ...

  8. windows 7(32/64位)GHO安装指南(U盘引导篇)~

    上一篇我们说了怎么制作U盘启动盘,那么这一篇让我们来看看如何进行正确的U盘引导启动. 现在的个人计算机一般分为台式机和笔记本,由于各厂商的喜好不同(开玩笑的啦),所以对于主板的BIOS设置各所不同.进 ...

  9. 【流量劫持】SSLStrip 终极版 —— location 瞒天过海

    前言 之前介绍了 HTTPS 前端劫持 的方案,虽然很有趣,然而现实却并不理想.其唯一.也是最大的缺陷,就是无法阻止脚本跳转.若是没有这个缺陷,那就非常完美了 -- 当然也就没有必要写这篇文章了. 说 ...

  10. opengl 笔记(1)

    参考<opengl入门教程>.<OpenGL之坐标转换>.<OpenGL绘制管线操作细节>等资料. 复习下留个备忘:) /*- * Opengl Demo Test ...