上滑動改變頭部,仿支付寶頭部變化,解決ScrollView與listView衝突問題

爲了快速實現,請看圖,看代碼,關鍵點在實現ScrollView 的onScrollChanged() 方法

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lxs
 * @date :2017/4/13
 * @Description:
 */

public class MainActivity extends AppCompatActivity {

    private List<String> list = new ArrayList<String>();
    private ListviewDataAdapter listviewDataAdapter;
    private PayScrollView payScrollView;
    private ListViewForScrollView listView;
    private RelativeLayout header_rl;
    private LinearLayout header_il, header_sheliang_ll;

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

        listView = (ListViewForScrollView) findViewById(R.id.list_v);
        listView.setFocusable(false);
        payScrollView = (PayScrollView) findViewById(R.id.scroll_v);
        header_rl = (RelativeLayout) findViewById(R.id.header_rl);
        header_il = (LinearLayout) findViewById(R.id.header_il);
        header_sheliang_ll = (LinearLayout) findViewById(R.id.header_sheliang_ll);

        payScrollView.setOnScrollChangedListeners(new PayScrollView.OnScrollChangedListener() {
            @Override
            public void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt) {
                if (t > header_sheliang_ll.getHeight() / 2 * 3) {
                    header_il.setVisibility(View.VISIBLE);
                    header_rl.setVisibility(View.GONE);
                } else if (t < header_sheliang_ll.getHeight() / 2) {
                    header_il.setVisibility(View.GONE);
                    header_rl.setVisibility(View.VISIBLE);
                }
            }
        });
        getData(true);
    }

    private void getData(boolean falge) {
        if (falge) {
            for (int i = 0; i < 50; i++) {
                list.add("張三" + i);
            }
            listviewDataAdapter = new ListviewDataAdapter(list, this);
            listView.setAdapter(listviewDataAdapter);
        }
    }

}

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;

/**
 * @author lxs
 * @date 創建時間 : 2017/4/10
 * @Description:
 */

public class PayScrollView extends ScrollView{

    public interface OnScrollChangedListener {
        void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt);
    }

    private OnScrollChangedListener mOnScrollChangedListener;

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

    public PayScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PayScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (mOnScrollChangedListener != null) {
            mOnScrollChangedListener.onScrollChanged(this, l, t, oldl, oldt);
        }
    }

    public void setOnScrollChangedListeners(OnScrollChangedListener listener) {
        mOnScrollChangedListener = listener;
    }

}

下面是實現listview 防止衝突

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

/**
 * @author lxs
 * @date 創建時間 : 2017/4/10
 * @Description:
 */

public class ListViewForScrollView extends ListView {


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


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


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

    /**
     * 重寫該方法,達到使ListView適應ScrollView的效果
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
}

效果如下圖


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章