码迷,mamicode.com
首页 > 其他好文 > 详细

ScrollView嵌套ListView

时间:2016-04-14 17:50:22      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:

方法一:自定义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);
    }


}

 

ScrollView嵌套ListView

标签:

原文地址:http://www.cnblogs.com/ldou/p/5391796.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!