• scrollview嵌套listiview(解决高度问题以及两者滚动冲突问题)不用重写listview


    package com.bwie.scrollview_lv_ori;
    
     
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.View.OnTouchListener;
    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 {
    
        private ListView lv;
        private ScrollView sc;
        private String[] name = new String[] { "A", "S", "D", "F", "G", "H", "J","A1", "S1", "D1", "F1", "G1", "H1", "J1","A2", "S2", "D2", "F3", "G4", "H5", "J6",
                "H", "J", "K" };
        private boolean isLoad=false;
    
        @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的高度
            setListViewHeightBasedOnChildren(lv);
            
            //为listview设置触摸事件
            lv.setOnTouchListener(new OnTouchListener() {
                
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                     
                    //请求父亲不要拦截
                    if(event.getAction() == 2)
                    {
                        sc.requestDisallowInterceptTouchEvent(true);
                    }else{
                        sc.requestDisallowInterceptTouchEvent(false);
                    }
                    
                    
                    return false;
                }
            });
            
            lv.setOnScrollListener(new OnScrollListener() {
                
                @Override
                public void onScrollStateChanged(AbsListView view, int scrollState) {
                     
                    if((scrollState == SCROLL_STATE_TOUCH_SCROLL) && isLoad)
                    {
                        //滑到最低端时,就请求父亲可以拦截
                        sc.requestDisallowInterceptTouchEvent(false);
                    }
                    
                }
                
                @Override
                public void onScroll(AbsListView view, int firstVisibleItem,
                        int visibleItemCount, int totalItemCount) {
                 
                    //判断lv是否加载到最低端
                    isLoad = (firstVisibleItem + visibleItemCount) == totalItemCount;
                    
                }
            });
    
        }
        
        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 =500 /*totalHeight
                    + (listView.getDividerHeight() * (listAdapter.getCount() - 1))*/;
            listView.setLayoutParams(params);
        }
        
         
    
    
    }
  • 相关阅读:
    error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/
    E: 无法获得锁 /var/lib/apt/lists/lock
    卸载nginx
    服务器文件传输
    一些重要的sql命令
    设置某个字段的值是唯一的
    mysql注释的方法
    认证 (authentication) 和授权 (authorization) 的区别
    neutron_lib: 一个进程内的发布与订阅
    人工智能导论作业
  • 原文地址:https://www.cnblogs.com/8023-itxinde/p/5402198.html
Copyright © 2020-2023  润新知