android - ScrollView嵌套ListView,ListView自动聚焦到第一个,和只显示一个

说明:

实际开发中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
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Beluga_白鲸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值