-->

分類

2016年5月30日 星期一

Android pull down for list

Activity

package com.example.james_lue;

import android.app.Activity;
import android.content.Context;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.AbsListView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

public class MorePullDowTest extends Activity {
    private static final String LOG_TAG = MorePullDowTest.class.getSimpleName();
    MyListAdapter mListAdapter;
    MyScrollView myScrollView;
    ListView mListView;
    TextView mUpTextView;
    private int mUpHeight;
    private boolean isListTop = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_more_pull_dow_test);
        mListAdapter = new MyListAdapter(this);

        myScrollView = (MyScrollView) findViewById(R.id.main_scroll_view);
        myScrollView.setMorePullTest(this);
        mListView = (ListView)findViewById(R.id.main_list_view);

        mUpTextView = (TextView)findViewById(R.id.text_up);

        mListView.setAdapter(mListAdapter);
        mListView.setOnScrollListener(new AbsListView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                switch (scrollState) {
                    case SCROLL_STATE_IDLE:
                        View c = mListView.getChildAt(0);
                        int scrollY = -c.getTop() + mListView.getFirstVisiblePosition() * c.getHeight();
                        if (scrollY == 0) {
                            isListTop = true;
                        }

                        Log.d(LOG_TAG, "scroll: " + scrollState + ", top: " + scrollY);
                        break;
                    case SCROLL_STATE_TOUCH_SCROLL:
                    case SCROLL_STATE_FLING:
                        isListTop = false;
                        break;
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });

        myScrollView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                // Ensure you call it only once :
                myScrollView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                // Here you can get the size :)
                ViewGroup.LayoutParams params = mListView.getLayoutParams();
                params.height = myScrollView.getHeight();
                params.width = myScrollView.getWidth();
                mUpHeight = mUpTextView.getHeight();
                mListView.setLayoutParams(params);
                myScrollView.requestLayout();
            }
        });

    }

    private static class MyListAdapter implements ListAdapter {

        private Context mContext;
        private MyListAdapter(Context context) {
            mContext = context;
        }
        @Override
        public boolean areAllItemsEnabled() {
            return true;
        }

        @Override
        public boolean isEnabled(int position) {
            return true;
        }

        @Override
        public void registerDataSetObserver(DataSetObserver observer) {

        }

        @Override
        public void unregisterDataSetObserver(DataSetObserver observer) {

        }

        @Override
        public int getCount() {
            Log.d(LOG_TAG, "getCount");
            return 50;
        }

        @Override
        public Object getItem(int position) {
            return position + ": item";
        }

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

        @Override
        public boolean hasStableIds() {
            return false;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView textView;
            if(convertView == null) {
                textView = new TextView(mContext);
                textView.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
                textView.setHeight(100);
                textView.setTextSize(25.0f);

            } else {
                textView = (TextView)convertView;
            }
            textView.setText(position + ": item");
            //Log.d(LOG_TAG, "po: " + position);
            return textView;
        }

        @Override
        public int getItemViewType(int position) {
            return 0;
        }

        @Override
        public int getViewTypeCount() {
            return 1;
        }

        @Override
        public boolean isEmpty() {
            return false;
        }
    }

    public static class MyScrollView extends ScrollView{
        private float mInitialY;
        private MorePullDowTest mActivity;
        public MyScrollView(Context context) {
            super(context, null);
            setFadingEdgeLength(0);
        }

        public MyScrollView(Context context, AttributeSet attrs) {
            super(context, attrs);
            setFadingEdgeLength(0);
        }

        public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            setFadingEdgeLength(0);
        }

        public void setMorePullTest(MorePullDowTest activity) {
            mActivity = activity;
        }

        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            switch (ev.getActionMasked()) {
                case MotionEvent.ACTION_DOWN:
                    mInitialY = ev.getY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    final float y = ev.getY();
                    float yD = y - mInitialY;

                    boolean shouldScrollBack = (yD > 0 && mActivity.isListTop);
                    Log.d(LOG_TAG, "yD: " + yD + "shouldScrollBack: " + shouldScrollBack + ", isTop: " + mActivity.isListTop);
                    if(shouldScrollBack) {
                        smoothScrollBy(0, 100 * (int) -yD);
                        mInitialY = y;
                        return true;
                    }
                    break;
            }
            Log.d(LOG_TAG, "intercept: " + ev.getActionMasked());
            return false;
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            switch (event.getActionMasked()) {
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:

                    if (this.getScrollY() <= mActivity.mUpHeight) {
                        smoothScrollTo(0, mActivity.mUpHeight);
                        //mActivity.canScrollChild = true;
                    }

            }
            Log.d(LOG_TAG, "touch: " + event.getActionMasked());
            return super.onTouchEvent(event);
        }
    }
}


layout


    

        
            
            

沒有留言:

張貼留言