zoukankan      html  css  js  c++  java
  • ScrollView 嵌套listview 正常显示

    //自定义listview

    package com.ce.scrollview_listview;

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

    public class MyListView extends ListView {
        int mLastMotionY;
        boolean bottomFlag;

        public MyListView(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
        }

        public MyListView(Context context, AttributeSet attrs) {
            super(context, attrs);
            // TODO Auto-generated constructor stub
        }

        public MyListView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            // TODO Auto-generated constructor stub
        }

        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            // TODO Auto-generated method stub
            // 闂冪粯顒涢悥鍓佽�閹凤附鍩呮禍瀣╂�
            if (bottomFlag) {
                getParent().requestDisallowInterceptTouchEvent(true);
            }
            return super.onInterceptTouchEvent(ev);
        }

        @Override
        public boolean onTouchEvent(MotionEvent ev) {
            // TODO Auto-generated method stub
            int y = (int) ev.getRawY();
            switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // 妫f牕鍘涢幏锔藉焻down娴滃�娆?,鐠佹澘缍峺閸ф劖鐖?
                mLastMotionY = y;
                break;
            case MotionEvent.ACTION_MOVE:
                // deltaY > 0 閺勵垰鎮滄稉瀣�箥閸旓拷,< 0閺勵垰鎮滄稉濠呯箥閸旓拷
                int deltaY = y - mLastMotionY;
                if (deltaY < 0) {
                    bottomFlag = true;
                    getParent().requestDisallowInterceptTouchEvent(true);
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                break;
            }
            return super.onTouchEvent(ev);
        }

    }

    //MainActivity

    package com.ce.scrollview_listview;

    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.ListAdapter;
    import android.widget.ListView;

    public class MainActivity extends Activity {
        // 制造数据
        String[] strs = { "item 1", "item 2", "item 3", "item 4", "item 5",
                "item 6", "item 7", "item 8", "item 9", "item 10", "item 11",
                "item 12", "item 13", "item 14", "item 15" };
        private ListView listview;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            listview = (ListView) findViewById(R.id.listView1);
            listview.setAdapter(new ArrayAdapter<String>(MainActivity.this,
                    android.R.layout.simple_list_item_1, strs));
            setListViewHeightBasedOnChildren(listview);
        }

        @Override
        public void onWindowFocusChanged(boolean hasFocus) {
            // TODO Auto-generated method stub
            super.onWindowFocusChanged(hasFocus);

        }

        private void setListViewHeightBasedOnChildren(ListView listView) {
            ListAdapter listAdapter = listView.getAdapter();
            if (listAdapter == null) {
                return;
            }

            int totalHeight = 0;
            for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
                // listAdapter.getCount()返回数据项的数目
                View listItem = listAdapter.getView(i, null, listView);
                // 计算子项View 的宽高
                listItem.measure(0, 0);
                // 统计所有子项的总高度
                totalHeight += listItem.getMeasuredHeight();
            }

            ViewGroup.LayoutParams params = listView.getLayoutParams();
            // params.height = totalHeight
            // + (listView.getDividerHeight() * (listAdapter.getCount() - 1));

            params.height = 400;
            // listView.getDividerHeight()获取子项间分隔符占用的高度
            // params.height最后得到整个ListView完整显示需要的高度
            listView.setLayoutParams(params);
        }
    }

    //mian.xml

    <LinearLayout 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"
        android:background="#FFE1FF"
        android:orientation="vertical" >

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

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

                <com.ce.scrollview_listview.MyListView
                    android:id="@+id/listView1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:fadingEdge="vertical"
                    android:fadingEdgeLength="5dp" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="button1111111111" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="button1111111111" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="button1111111111" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="button1111111111" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="button1111111111" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="button1111111111" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="button1111111111" />
            </LinearLayout>
        </ScrollView>

    </LinearLayout>

  • 相关阅读:
    ApiDoc 一键生成注释
    质量报告之我见
    一些 ssh 小技巧
    virtualenv简介以及一个比较折腾的scrapy安装方法
    用scrapy数据抓取实践
    linux rootfs制作
    ubuntu环境下android开发环境安装
    弱符号 与 强符号, 强引用 与 弱引用
    链接器和加载器
    wine的使用
  • 原文地址:https://www.cnblogs.com/weiyangge/p/5388859.html
Copyright © 2011-2022 走看看