zoukankan      html  css  js  c++  java
  • scrollview嵌套listiview(解决高度问题以及两者滚动冲突问题)

    1、scrollview由于只能有一个子布局,因此scrollview嵌套listview,必须得用LinearLayout来包裹listview,然后scrollview再包含线性布局。

    xml布局文件

    <RelativeLayout 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" >
    
        <ScrollView
            android:id="@+id/sc"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
    
            <LinearLayout
                android:id="@+id/line"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >
    
                <com.bwie.view.MyListView
                    android:id="@+id/lv"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                </com.bwie.view.MyListView>
    
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
    
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
    
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
    
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
    
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
    
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
    
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
    
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
    
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
    
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
            </LinearLayout>
        </ScrollView>
    
    </RelativeLayout>

    2、mainactivity代码

    package com.bwie.scrollview_listview;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AbsListView;
    import android.widget.AbsListView.OnScrollListener;
    import android.widget.ArrayAdapter;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    import android.widget.ScrollView;
    
    public class MainActivity extends Activity implements OnScrollListener {
    
        private ListView lv;
        private String[] name = new String[] { "A", "S", "D", "F", "G", "H", "J",
                "H", "J", "K"};
        private boolean isLoad=false;
        private ScrollView sc;
         
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // 找到该布局文件下的控件
            lv = (ListView) findViewById(R.id.lv);
            sc = (ScrollView) findViewById(R.id.sc);
            
    
            // 为lv设置适配器
            lv.setAdapter(new ArrayAdapter<String>(MainActivity.this,
                    android.R.layout.simple_expandable_list_item_1, name));
    
            // 重新定义listiview的高度(自己计算listview的高度)
            setListViewHeightBasedOnChildren(lv);
            
            //为lv设置滚动监听事件,如果listview滚动到最后一条时,就请求父控件拦截touch事件,父控件进行滚动
            lv.setOnScrollListener(this);
    
        }
    
        private void setListViewHeightBasedOnChildren(ListView listView) {
            ListAdapter listAdapter = listView.getAdapter();
            if (listAdapter == null) {
                // pre-condition
                return;
            }
    
            int totalHeight = 0;
            for (int i = 0; i < listAdapter.getCount(); i++) {
                View listItem = listAdapter.getView(i, null, listView);
                listItem.measure(0, 0);
                totalHeight += listItem.getMeasuredHeight();
            }
    
            ViewGroup.LayoutParams params = listView.getLayoutParams();
            params.height = totalHeight
                    + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
            listView.setLayoutParams(params);
        }
    
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            // TODO Auto-generated method stub
            if(scrollState  == SCROLL_STATE_TOUCH_SCROLL && isLoad)
            {
                //请求父亲拦截touch事件
                sc.requestDisallowInterceptTouchEvent(false);
            }
            
        }
    
        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {
            
            //判断是否加载到最底部
            isLoad = (firstVisibleItem + visibleItemCount) == totalItemCount;
            
        }
        
     
    }

    3、重写listview控件,请求父控件(scrollview控件)不要拦截touch事件

    package com.bwie.view;
    
    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) {
    
            // 请求父亲不要拦截touch事件
            getParent().requestDisallowInterceptTouchEvent(true);
    
            return super.dispatchTouchEvent(ev);
        }
    
    }
  • 相关阅读:
    SAP CRM销售订单UI上的字段对应的数据库表存储字段:requested start date和end date
    SAP Fiori Elements里Drop down list的实现原理
    使用Fiori Elements创建的SAP UI5应用,如何支持编辑功能
    #开工新姿势#开启一年新征程,云社区叫你来充电啦!
    云小课 | 守护网络安全不是问题,iptables的四表五链为你开启“八卦阵”
    所见即搜,3分钟教你搭建一个服装搜索系统!
    AI辅助宫颈癌筛查技术全球居首,守护者的力量来源是?
    干货分享丨从MPG 线程模型,探讨Go语言的并发程序
    网络知识一箩筐:IP地址划分的那些知识点
    MindSpore:基于本地差分隐私的 Bandit 算法
  • 原文地址:https://www.cnblogs.com/8023-itxinde/p/5402054.html
Copyright © 2011-2022 走看看