ScrollView嵌套ListView

方法一:自定义ListView

MainActivity.class

package com.bwie.test;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.MeasureSpec;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

    private ListView listView;
    private List<String> list;
    private boolean b;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.lv);
        // 创建数据
        init();
        // 设置适配器
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                MainActivity.this, android.R.layout.simple_list_item_1, list);
        listView.setAdapter(adapter);
        setListViewHeightBasedOnChildren(listView);
        //给listview设置滚动监听
        setScroll();
    }

    private void setScroll() {
        listView.setOnScrollListener(new OnScrollListener() {
            
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                // TODO Auto-generated method stub
                if(scrollState==SCROLL_STATE_TOUCH_SCROLL&&b){
                    listView.getParent().getParent().requestDisallowInterceptTouchEvent(false);
                }
            }
            
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
                // TODO Auto-generated method stub
                b=((firstVisibleItem+visibleItemCount)==totalItemCount);
            }
        });
    }
    

    private void init() {
        list = new ArrayList<String>();
        for (int i = 0; i < 50; i++) {
            list.add("data" + i);
        }
    }

    // 根据listview的数据来计算他的应得的高度,设置高度
    public void setListViewHeightBasedOnChildren(ListView listView) {
        ArrayAdapter listAdapter = (ArrayAdapter) listView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;

        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            if (listItem != null) {
                listItem.setLayoutParams(new LayoutParams(
                        LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
                listItem.measure(MeasureSpec.UNSPECIFIED,
                        MeasureSpec.UNSPECIFIED);
                totalHeight += listItem.getMeasuredHeight();
            }
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        // params.height = totalHeight
        // + (listView.getDividerHeight() * (listAdapter.getCount() - 1))
        // + listView.getPaddingTop() + listView.getPaddingBottom();
        // int h = getWindowManager().getDefaultDisplay().getHeight();
        // if (params.height > h / 2) {
        // params.height = h / 2;
        // }
        params.height = 500;
        listView.setLayoutParams(params);
    }
    

}

自定义的listview类:MyListView.class

package com.bwie.test;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ListView;

public class MyListview extends ListView {

    public MyListview(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        // TODO Auto-generated method stub
        //请求父亲(Scrollview不截断touch事件,因为Scrollview也有滚动效果)
        getParent().requestDisallowInterceptTouchEvent(true);
        return super.dispatchTouchEvent(ev);
    }

}

xml布局(两种方法调用一个布局)

<ScrollView 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"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <com.bwie.test.MyListview
            android:id="@+id/lv"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </com.bwie.test.MyListview>

        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:text="按钮1"/>
        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:text="按钮2"/>
        <Button
            android:id="@+id/button3"
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:text="按钮3"/>
        <Button
            android:id="@+id/button4"
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:text="按钮4"/>
        <Button
            android:id="@+id/button5"
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:text="按钮5"/>
        <Button
            android:id="@+id/button6"
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:text="按钮6"/>
        <Button
            android:id="@+id/button7"
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:text="按钮7"/>
        <Button
            android:id="@+id/button8"
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:text="按钮8"/>
        <Button
            android:id="@+id/button9"
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:text="按钮9"/>
        <Button
            android:id="@+id/button5"
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:text="按钮5"/>
        <Button
            android:id="@+id/button6"
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:text="按钮6"/>
        <Button
            android:id="@+id/button7"
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:text="按钮7"/>
        <Button
            android:id="@+id/button8"
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:text="按钮8"/>
        <Button
            android:id="@+id/button9"
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:text="按钮9"/>
        <Button
            android:id="@+id/button6"
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:text="按钮6"/>
        <Button
            android:id="@+id/button7"
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:text="按钮7"/>
        <Button
            android:id="@+id/button8"
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:text="按钮8"/>
        <Button
            android:id="@+id/button9"
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:text="按钮9"/>
        <Button
            android:id="@+id/button5"
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:text="按钮5"/>
        <Button
            android:id="@+id/button6"
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:text="按钮6"/>
        <Button
            android:id="@+id/button7"
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:text="按钮7"/>
        <Button
            android:id="@+id/button8"
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:text="按钮8"/>
        <Button
            android:id="@+id/button9"
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:text="按钮9"/>
        
    </LinearLayout>

</ScrollView>

 

 

第二种方法:对listview设置触摸监听

MainActivity.class

 

package com.bwie.test;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.View.MeasureSpec;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ScrollView;

public class MainActivity extends Activity {

    private ListView listView;
    private List<String> list;
    private boolean b;
    private ScrollView sc;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        listView = (ListView) findViewById(R.id.lv);
        sc = (ScrollView) findViewById(R.id.sc);
        // 创建数据
        init();
        // 设置适配器
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                MainActivity.this, android.R.layout.simple_list_item_1, list);
        listView.setAdapter(adapter);
        setListViewHeightBasedOnChildren(listView);
        //给listview设置滚动监听
        setScroll();
        //对listview设置触摸监听
        setTouch();
    }

    private void setTouch() {
        // TODO Auto-generated method stub
        listView.setOnTouchListener(new OnTouchListener() {
            
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                if(event.getAction()== 2){
                    sc.requestDisallowInterceptTouchEvent(true);
                }
                else{
                    sc.requestDisallowInterceptTouchEvent(false);
                }
                return false;
            }
        });
    }

    private void setScroll() {
        listView.setOnScrollListener(new OnScrollListener() {
            
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                // TODO Auto-generated method stub
                if(scrollState==SCROLL_STATE_TOUCH_SCROLL&&b){
                    listView.getParent().getParent().requestDisallowInterceptTouchEvent(false);
                }
            }
            
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
                // TODO Auto-generated method stub
                b=((firstVisibleItem+visibleItemCount)==totalItemCount);
            }
        });
    }
    

    private void init() {
        list = new ArrayList<String>();
        for (int i = 0; i < 50; i++) {
            list.add("data" + i);
        }
    }

    // 根据listview的数据来计算他的应得的高度,设置高度
    public void setListViewHeightBasedOnChildren(ListView listView) {
        ArrayAdapter listAdapter = (ArrayAdapter) listView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;

        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            if (listItem != null) {
                listItem.setLayoutParams(new LayoutParams(
                        LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
                listItem.measure(MeasureSpec.UNSPECIFIED,
                        MeasureSpec.UNSPECIFIED);
                totalHeight += listItem.getMeasuredHeight();
            }
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        // params.height = totalHeight
        // + (listView.getDividerHeight() * (listAdapter.getCount() - 1))
        // + listView.getPaddingTop() + listView.getPaddingBottom();
        // int h = getWindowManager().getDefaultDisplay().getHeight();
        // if (params.height > h / 2) {
        // params.height = h / 2;
        // }
        params.height = 500;
        listView.setLayoutParams(params);
    }


}

 

转载于:https://www.cnblogs.com/ldou/p/5391796.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值