ScrollerView嵌套listview.实现,listview滑动


ScrollerView嵌套listview.实现,listview滑动_第1张图片

原文:

http://bbs.anzhuo.cn/thread-982250-1-1.html

http://www.cnblogs.com/shortboy/archive/2013/04/08/3008710.html


按常理,ScrollView套ListView会存在两个问题:


1.里面的listView高度无法算出来,通常只能显示listview的其中一行
2.listview不能滚动


在解决问题一的时候,我在网上找了一大堆资料,怎么怎么让listview显示完整,终于被我找到一个帖子,能解决这个问题
http://blog.csdn.net/hitlion2008/article/details/6737459 
(同时我不小心搜到了我的"ScrollView嵌套ScrollView"贴子满天飞...在这里我拜托大家,转贴注明出处啊亲。。。。。)。
按照这个贴子我试了一下,虽然listview的高度出来了,但是存在两个问题:
a.还是只支持ScrollView滚动,listView不会滚动,因为listView的高度已经达到最大,它不需要滚动。
b.listview的优点就是能够复用listItem,如果把listView撑到最大,等于是没有复用listItem,这跟没用listView一样,省不了ui资源,那我还不如直接用linearlayout呢


解决方式:把listview的长度,设置为固定.对listview设置监听

public class MyListview extends ListView {
    private static final String TAG = "MyListview";

    public MyListview(Context context) {
        super(context);
    }

    public MyListview(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    //方式一:把listview的高度设置为最大值.这样listview全部加载了
/*    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }*/


    public
    boolean onInterceptTouchEvent(MotionEvent ev) {
        switch
                (ev.getAction()) {
            case
                    MotionEvent.ACTION_DOWN:

                setParentScrollAble(false);//当手指触到listview的时候,让父ScrollView交出ontouch权限,也就是让父scrollview

//                setParentScrollAble(false);//当手指触到listview的时候,让父ScrollView交出ontouch权限,也就是让父scrollview
//                LogManager.d("onInterceptTouchEvent
//                        down");
            case MotionEvent.ACTION_MOVE:

//                LogManager.d("onInterceptTouchEvent move");
                break;

            case MotionEvent.ACTION_UP:

//                LogManager.d("onInterceptTouchEvent up");
            case
                    MotionEvent.ACTION_CANCEL:
                setParentScrollAble(true);//当手指松开时,让父ScrollView重新拿到onTouch权限
//                LogManager.d("onInterceptTouchEvent cancel");

//                requestDisallowInterceptTouchEvent(true);//当手指松开时,让父ScrollView重新拿到onTouch权限

                break;
            default:
                break;

        }
        return super.onInterceptTouchEvent(ev);

    }
    private void setParentScrollAble(boolean flag) {

        requestDisallowInterceptTouchEvent(!flag);//这里的parentScrollView就是listview外面的那个scrollview


    }

}
public class MainActivity extends AppCompatActivity {

    private ListView mListView;
    private ArrayList mData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mListView = (ListView) findViewById(R.id.listview);
//        mListView.setFocusable(false);
        iniData();
    }



    private void iniData() {
        mData = new ArrayList<>();
        for (int i = 0; i < 200; i++) {
            mData.add(i + "");
        }


        mListView.setAdapter(new MyAdapter());

//        setListViewHeightBasedOnChildren(mListView);

    }


    //测量listv的长度,也是全部加载起来.没有实现listview的优化
    public static void setListViewHeightBasedOnChildren(ListView listView) {
        if(listView == null) return;
        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);
    }

    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mData == null ? 0 : mData.size();
        }

        @Override
        public Object getItem(int position) {
            if (mData != null) {
                return mData.get(position);
            }
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView textView;
            if (convertView == null) {
                textView = new TextView(MainActivity.this);
            } else {
                textView = (TextView) convertView;
            }
            textView.setText(mData.get(position));
            textView.setTextSize(20);
            System.out.println("viwe:"+position);
            return textView;
        }
    }


}



    

        
        
        
        
        
        
        
        
        
    






你可能感兴趣的:(Ui)