您的位置:首页 > 其它

swipRefreshLayout嵌套listview的冲突解决

2016-12-13 14:30 155 查看
在使用5.0的design包下的下拉刷新的时候,当嵌套listview的产生冲突;

导致问题,listView的向上滑动,不受影响,但是向下滑动的时候出现问题,会直接触发下拉刷新!

xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f3f4f8"
android:orientation="vertical">

<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/srl_refresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--TITLE-->
<LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="55dp"
android:background="#0066ff"
android:gravity="center_vertical"
android:orientation="horizontal">
<!--广播-->
<ImageView
an
d4f6
droid:id="@+id/boss_alma_iv1"
android:layout_width="23dp"
android:layout_height="23dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="60dp"
android:background="@mipmap/message"/>

<LinearLayout
android:id="@+id/boss_alma_ll"
android:layout_width="182dp"
android:layout_height="35dp"
android:background="@mipmap/boss_alma_one"
android:orientation="horizontal">

<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="60dp"
android:text="需求推荐"
android:textColor="#203048"
android:textSize="16sp"/>

<ImageView
android:layout_width="6dp"
android:layout_height="6dp"
android:layout_gravity="center"
android:layout_marginLeft="30dp"
android:src="@mipmap/boss_alma_right2"/>
</LinearLayout>
</LinearLayout>
<!--公司展示-->
<ListView
android:id="@+id/boss_alma_lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/ll"
android:cacheColorHint="@android:color/white"
android:divider="@null"
android:fadingEdge="none"
android:scrollbars="none"/>
</LinearLayout>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>


下面的代码是对下拉刷新设置监听,这里就直接将代码考过来


//下拉刷新设置监听
mSrl_refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
NeedFragmentBean.getInstance().getShowData();       //获取listview刷新
NeedFragmentBean.getInstance().getAlmaTitle();      //标题
//耗时处理,做一个异步的操作
new Thread(new Runnable() {
@Override
public void run() {
SystemClock.sleep(3000);
getActivity().runOnUiThread(new Runnable() {     //主线程更新UI
@Override
public void run() {
mSrl_refresh.setRefreshing(false);
}
});
}
}).start();
}
});



//上面的都是基本的处理,下面的是对问题的解决--

//解决下拉刷新和listview的问题
mBoss_alma_lv.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView absListView, int i) {

}

@Override
public void onScroll(AbsListView absListView, int firstVisibleItem, int i1, int i2) {
if (firstVisibleItem == 0){
mSrl_refresh.setEnabled(true);
} else{
mSrl_refresh.setEnabled(false);
}
}
});


思路总结:

在listview向下各种滚动的过程中,可以加上一个OnScrollListener,监听listview是否滑到了最顶端的一个item,如果在最顶端,就将swiperefreshlayout设置成setEnabled(true),如果不再最顶端,就设置成setEnabled(false),这样直接就OK,其它的也可以进行效仿
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: