android - ScrollView嵌套ListView,ListView自动聚焦到第一个,和只显示一个
来源:互联网 发布:centos telnet 安装 编辑:程序博客网 时间:2024/05/14 21:45
说明:
实际开发中ViewPager和GridView和LIstView三个布局在同一个界面中,为了整体滑动,所以在外面嵌套了一个ScrollView,图解:
问题:
实际开发中碰到了两个问题:
1.listView只显示一项
解决办法:
重写listView,禁止其上下滑动方法,来适应ScrollView,代码:
/** * 禁止ListView上下滑动 * 重写改方法让ListView适应ScrollView * * @param widthMeasureSpec * @param heightMeasureSpec */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { heightMeasureSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, heightMeasureSpec); }
2.自动聚焦到listview第一个
解决办法:
取消ListView的焦点,代码:
lvRecomm.setFocusable(false);
如果取消焦点还是不行,则在给list设置好adapter后,把scrollView.smoothScrollTo(0, 0); 就可以了
整体代码如下:
布局xml文件:最外边是自定义的下拉刷新,中间是ScrollView里面包含ViewPager、GridView、ListVIew:
<?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:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/home_bg"> <com.baofoo.mobile.wallet.common.view.PullToRefreshView android:id="@+id/rv_pull" android:layout_width="match_parent" android:layout_height="wrap_content"> <ScrollView android:id="@+id/sv_scroll" android:layout_width="fill_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <RelativeLayout android:id="@+id/rl_banners_border1" android:layout_width="match_parent" android:layout_height="wrap_content"> <com.baofoo.mobile.wallet.common.view.AutoScrollViewPager android:id="@+id/as_banners" android:layout_width="match_parent" android:layout_height="match_parent"/> <com.viewpagerindicator.CirclePageIndicator android:id="@+id/indicator_banners1" android:padding="10dip" android:layout_alignParentBottom="true" android:layout_marginBottom="5dp" android:layout_height="wrap_content" android:layout_width="fill_parent"/> </RelativeLayout> <LinearLayout android:id="@+id/ll_scroll" android:layout_width="match_parent" android:layout_height="26dp" android:gravity="center" android:orientation="horizontal" android:paddingBottom="2dp" android:paddingLeft="10dp" android:paddingRight="10dp" android:paddingTop="2dp"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/home_scroll"/> <TextView android:id="@+id/tv_scroll_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:lines="1" android:textColor="#2e8fd3" android:layout_marginLeft="10dp" android:paddingLeft="2dp" android:textSize="12dp"/> </LinearLayout> <RelativeLayout android:id="@+id/rl_banners_border2" android:layout_width="match_parent" android:layout_height="160dp"> <android.support.v4.view.ViewPager android:id="@+id/vp_banners" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ffffff"/> <com.viewpagerindicator.CirclePageIndicator android:id="@+id/indicator_banners2" android:padding="10dip" android:layout_alignParentBottom="true" android:layout_marginBottom="10dp" android:layout_height="wrap_content" android:layout_width="fill_parent" android:visibility="invisible"/> </RelativeLayout> <com.baofoo.mobile.wallet.home.RecomListView android:id="@+id/lv_recomm" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:background="@color/white" android:divider="@color/finance_divider" android:dividerHeight="6dp"/> </LinearLayout> </ScrollView> </com.baofoo.mobile.wallet.common.view.PullToRefreshView></LinearLayout>
重写的ListView方法:
package com.baofoo.mobile.wallet.home;import android.content.Context;import android.util.AttributeSet;import android.widget.ListView;/** * 首页产品推荐自定义listView * Created by zst on 2016/3/22. */public class RecomListView extends ListView{ public RecomListView(Context context) { super(context); } public RecomListView(Context context, AttributeSet attrs) { super(context, attrs); } public RecomListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } /** * 禁止ListView上下滑动 * 重写改方法让ListView适应ScrollView * * @param widthMeasureSpec * @param heightMeasureSpec */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { heightMeasureSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, heightMeasureSpec); }}
ListVIew的java代码:
//产品区 - list lvRecomm.setAdapter(new ProductsAdapter(getActivity(), productsList)); lvRecomm.setFocusable(false);
其它代码就是给ViewPager、GridView、ListView设置adapter了,省略不传了。
1 0
- android - ScrollView嵌套ListView,ListView自动聚焦到第一个,和只显示一个
- ScrollView嵌套ListView自动聚焦到ListView的问题
- ScrollView内嵌套ListView,ListView只显示一个item
- scrollview中嵌套多个listview解决每个listview只显示一个item
- ScrollView嵌套ListView,listView只显示一条
- ScrollView嵌套ListView,listView只显示一条
- Android中ScrollView嵌套ListView只显示一行的解决方案
- Android ScrollView嵌套ListView只显示一行数据
- android 控件 scrollview嵌套listview只显示一行
- android中ScrollView嵌套ListView只显示一条item问题
- Android ScrollView 里面嵌套ListView只显示一项的解决方案
- Android之将ListView嵌套到ScrollView中只显示一行的问题
- 日积月累:ScrollView嵌套ListView只显示一行
- ScrollView嵌套ListView解决只显示一行
- ScrollView嵌套ListView只显示一行
- 穷年累月:ScrollView嵌套ListView只显示一行
- 日积月累:ScrollView嵌套ListView只显示一行
- ScrollView嵌套ListView只显示一行
- Spring动态创建bean切换数据源
- ETL系列:一种针对oracle分区表的统计信息收集策略
- 调查数据的加权处理技术
- word2vec代码注释
- Spring学习笔记(十九):关于tomcat运行一直出错的问题
- android - ScrollView嵌套ListView,ListView自动聚焦到第一个,和只显示一个
- Spring学习8-Spring事务管理(AOP/声明式式事务管理)
- JAVA8 十大新特性详解
- Date工具类
- sso package or sign error解决方案
- 【IOS开发】企业版IDP分发(In-house模式)新规提示
- Shell技巧
- 约瑟夫问题
- ApplePay高调入华,教你在app里上线ApplePay