下拉刷新的代码就不贴了,本文主要讲的是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); } } });