上滑动改变头部,仿支付宝头部变化,解决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);
    }
}

效果如下图


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值