[Android] Android最简单ScrollView和ListView滚动冲突解决方案
[Question]问题描述:
单独的ListView列表能自动垂直滚动,但当将ListView嵌套在ScrollView后,会和ScrollView的滚动滑块冲突,造成ListView滑块显示不完整。
activity_main.xml表现:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
****此处省略ScrollView的修饰代码***
> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:text="test"/> <ListView
android:id="@+id/list_class"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none"/>
</LinearLayout> </ScrollView>
[Solution]解决方法:
第一步:自定义ListViewForScrollView类: ListViewForScrollView.java
package com.jack.helloen.ui; import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView; /**
* 自定义一个类继承自ListView,通过重写其onMeasure方法,达到对ScrollView适配的效果。
*/
public class ListViewForScrollView extends ListView {
public ListViewForScrollView(Context context) {
super(context);
} public ListViewForScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
} public ListViewForScrollView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
} @Override
/**
* 重写该方法,达到使ListView适应ScrollView的效果
*/
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
第二步: MainActivity.java里增加兼容patch
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
//ListView兼容ScrollView
sv = (ScrollView) findViewById(R.id.main_scroll);
sv.smoothScrollTo(0, 0);
}
第三步:布局文件xml 里 引用自定义 组件
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_scroll"
*** >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:text="test"/>
<com.jack.helloen.ui.ListViewForScrollView
android:id="@+id/list_class"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none"/>
</LinearLayout>
</ScrollView>
本博客地址: wukong1688
本文原文地址:https://www.cnblogs.com/wukong1688/p/10657559.html
转载请著名出处!谢谢~~
[Android] Android最简单ScrollView和ListView滚动冲突解决方案的更多相关文章
- 【Android - 问题解决】之ScrollView嵌套ListView时总是自动滑动到ListView顶部的问题
最近做了一个项目,里面有一个ScrollView嵌套ListView的布局. 做出来之后发现,进入这个界面之后,总是自动滑动到ListView的顶部,而ScrollView中位于ListView上面的 ...
- Android——MeasureSpec学习 - 解决ScrollView嵌套ListView和GridView冲突的方法
原文地址:http://blog.csdn.net/yuhailong626/article/details/20639217 在自定义View和ViewGroup的时候,我们经常会遇到int ...
- 【朝花夕拾】Android自定义View篇之(七)Android事件分发机制(下)滑动冲突解决方案总结
前言 转载请声明,转自[https://www.cnblogs.com/andy-songwei/p/11072989.html],谢谢! 前面两篇文章,花了很大篇幅讲解了Android的事件分发机制 ...
- ScrollView与ListView的冲突
众所周知ListView与ScrollView都具有滚动能力,对于这样的View控件,当ScrollView与ListView相互嵌套会成为一种问题: 问题一:ScrollView与ListView嵌 ...
- 解决ScrollView与ListView事件冲突
1,在最近做项目的时候使用ScrollView嵌套ListView的时候发现ListView的滑动效果失效,简单的网上搜索了一下,也就有了下面的解决方法,在ListView中设置事件的监听listvi ...
- Android布局中ScrollView与ListView的冲突的最简单方法
看到网上流行的一种使用方法是: public class Utility { public static void setListViewHeightBasedOnChildren(ListView ...
- Android ScrollView与ListView的冲突解决办法汇总
1. public void setListViewHeight(){ ListAdapter listadapter = lv.getAdapter(); if (listadapter == n ...
- Android ScrollView和ListView滑动冲突解决记录
private int mLastX; private int mLastY; public View.OnTouchListener onTouchListener = new View.OnTou ...
- Android View的事件分发机制和滑动冲突解决方案
这篇文章会先讲Android中View的事件分发机制,然后再介绍Android滑动冲突的形成原因并给出解决方案.因水平有限,讲的不会太过深入,只希望各位看了之后对事件分发机制的流程有个大概的概念,并且 ...
随机推荐
- LG P2473 [SCOI2008]奖励关
题目链接:P2473 [SCOI2008]奖励关 题意:有n个宝物 每次等概率抛出其中之一一共抛出k次每个宝物有一个价值 和一个前提集合只有集齐了集合中的所有宝物 才可以领取这个宝物 范围:1 < ...
- Hdoj 1176.免费馅饼 题解
Problem Description 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy的人品实在是太好了,这馅饼别处都不掉,就掉落在他身旁 ...
- https搭建实例
:(用的)https://www.coderecord.cn/lets-encrypt-wildcard-certificates.html :acme.shvim .acme.sh/account. ...
- 8、16、32-BIT系列单片机区别与特点
一.8位单片机 8031/8051/8751是Intel公司早期的产品 1.8031的特点 8031片内不带程序存储器ROM,使用时用户需外接程序存储器和一片逻辑电路373,外接的程序存储器多为EPR ...
- 用Python3、NetCore、Shell分别开发一个Ubuntu版的定时提醒(附NetCore跨平台两种发布方式)
汇总系列:https://www.cnblogs.com/dunitian/p/4822808.html#ai Python3 与 C# 基础语法对比:https://www.cnblogs.com/ ...
- Java 破解谷歌翻译api,可以实现程序自动化翻译文章
1 原理:查看谷歌翻译网站,输入需要翻译的文字,选择语言得到翻译后的文字,发送异步请求参数返回结果.java使用httpclient发送请求,实现使用代码翻译文章的功能. 2 下载代码后,测试入口 ...
- Spring 依赖注入优化
Spring 依赖注入优化 原创: carl.zhao SpringForAll社区 今天 Spring 最大的好处就是依赖注入,关于什么是依赖注入,在Stack Overflow上面有一个问题,如何 ...
- C# Winfrom常用的几个公共控件
ComboBox控件的使用方法: //首先写好查询方法,实例化对象, NationData nd = new NationData(); List<Nation> NN = new Lis ...
- 洛谷P5111 zhtobu3232的线段树
题意:给定线段树,上面若干个节点坏了,求能表示出多少区间. 区间能被表示出当且仅当拆出来的log个节点都是好的. 解:每个区间在最浅的节点处计算答案. 对于每个节点维护从左边过来能有多少区间,从右边过 ...
- bzoj3829 POI2014 FAR-FarmCraft
题目链接 思路 用\(f[i]\)表示完成第\(i\)棵子树所需要得时间. 考虑如果有两个子树\(a\)和\(b\),如果先去完成子树\(a\),那么对于花费得时间就是\(f[b] + siz[a] ...