swiperefreshlayout嵌套scrollview冲突解决方法
if (scll != null) { scll.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() { @Override public void onScrollChanged() { if (swipeRefreshLayout != null) { swipeRefreshLayout.setEnabled(scll.getScrollY() == 0); //CommonLog.infoLog("init scrollView"); } } }); }
package com.example.administrator.xialashuaxin; import java.util.ArrayList; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.os.SystemClock; import android.widget.ArrayAdapter; public class MainActivity extends Activity { private SwipeRefreshLayout swipeRefreshLayout; private int count = 0; private ArrayList<String> data = new ArrayList<String>(); private ArrayAdapter<String> adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout); swipeRefreshLayout.setSize(SwipeRefreshLayout.LARGE); // 设置下拉多少距离之后开始刷新数据 swipeRefreshLayout.setDistanceToTriggerSync(100); // 设置进度条背景颜色 swipeRefreshLayout.setProgressBackgroundColorSchemeColor(this.getResources().getColor(android.R.color.holo_blue_light)); // 设置刷新动画的颜色,可以设置1或者更多. swipeRefreshLayout.setColorSchemeColors(getResources().getColor(android.R.color.holo_blue_bright), getResources().getColor(R.color.colorAccent), getResources().getColor(R.color.colorPrimary)); swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() { @Override public void onRefresh() { /** * 注意:AsyncTask是单线程顺序执行的线程任务。 * 如果涉及到大量、耗时的线程加载任务,可以考虑自己利用Thread(ExecutorService线程池)+Handler+Message实现。 * */ LongTimeOperationTask task = new LongTimeOperationTask(); task.execute(); } }); } private class LongTimeOperationTask extends AsyncTask<String, Integer, String> { @Override protected void onPreExecute() { swipeRefreshLayout.setRefreshing(true); } @Override protected String doInBackground(String... params) { SystemClock.sleep(2000); //刷新后的执行方法 return null; } @Override protected void onPostExecute(String result) { swipeRefreshLayout.setRefreshing(false); } } }
//布局
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/swipeRefreshLayout" android:layout_width="match_parent" android:layout_height="match_parent" > <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/scll"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> </LinearLayout> </ScrollView> </android.support.v4.widget.SwipeRefreshLayout>
本文介绍了一种解决SwipeRefreshLayout与ScrollView嵌套时产生的滑动冲突的方法,并提供了一个示例项目,展示了如何配置SwipeRefreshLayout的各种属性,以及如何通过监听ScrollView的滚动变化来动态启用或禁用SwipeRefreshLayout的刷新功能。
4939

被折叠的 条评论
为什么被折叠?



