SwipeRefreshLayout和ListView冲突

下拉刷新的代码就不贴了,本文主要讲的是SwipeRefreshLayout里面添加了其他View跟随listview一起向下滑动切换

回归正解以前用的下拉刷新基本都是继承ScrollView做的,但是SwipeRefreshLayout是继承ViewGroup。(之前一直傻傻以为SwipeRefreshLayout是继承ScrollView一直在错误方向找)

这里附带下添加listView头部的方法,可以自己选择使用哪种方法适合

View view = getActivity().getLayoutInflater().inflate(R.layout.ind_layout,null,false);
listview.addHeaderView(view);

 

 

这是红色View添加方法

多余的话不多说上代码

这是xml文件

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#e1e112"
        android:orientation="vertical">
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
                <View
                    android:layout_width="match_parent"
                    android:layout_height="400dp"
                    android:background="@color/colorAccent"/>
                <com.example.my.swiperefresh.ListViewForScrollView
                    android:id="@+id/listview"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:minHeight="700dp"
                    android:headerDividersEnabled="true">

                </com.example.my.swiperefresh.ListViewForScrollView>
            </LinearLayout>
        </ScrollView>
    </android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>

这是自定义listView

public class ListViewForScrollView extends ListView {
    public ListViewForScrollView(Context context) {
        super(context);
    }

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

    public ListViewForScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public ListViewForScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
}

这是下滑冲突的解决办法

listview.setOnScrollListener(new AbsListView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        View firstView = view.getChildAt(firstVisibleItem);
        if(firstVisibleItem ==0 && (firstView == null || firstView.getTop() == 0)) {
            /*上滑到listView的顶部时,下拉刷新组件可见*/
            swipeLayout.setEnabled(true);
        } else {
            /*不是listView的顶部时,下拉刷新组件不可见*/
            swipeLayout.setEnabled(false);
        }
    }
});

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值