recyclerview嵌套在NestedScrollView里,一次性加载出全部数据问题。
因为要实现向上滑动时toolbar悬停在顶部的那种效果,所以recyclerview必须嵌套在NestedScrollView里。
作者给出的两种解决方法:
1.将嵌套部分的布局当作头部和尾部添加到rv上面或下面,去除ScrollView。
2.更换第三发的嵌套的上拉下拉控件。
第一种直接不可取,第二种就是要抛弃这个开源库的意思^_^
同时这个问题可以参考已关闭的issues #1821
@chailijun 可以的
public class MainActivity extends AppCompatActivity implements BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {
private final String TAG = "MainActivity";
private RecyclerView recyclerView;
private NestedScrollView scrollView;
private SwipeRefreshLayout swipe;
private List<String> list = new ArrayList<>();
private TestAdapter testAdapter;
private boolean enableLoadMore = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUI();
onRefresh();
}
private void initUI() {
recyclerView = (RecyclerView) findViewById(R.id.recycler);
scrollView = (NestedScrollView) findViewById(R.id.scrollView);
swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
swipe.setOnRefreshListener(this);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this) {
@Override
public boolean canScrollVertically() {
return false;
}
};
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.addItemDecoration(new DividerItemDecoration(getApplicationContext(),DividerItemDecoration.VERTICAL));
recyclerView.setNestedScrollingEnabled(false);
scrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
@Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
if (scrollY > oldScrollY) {
Log.i(TAG, "Scroll DOWN");
}
if (scrollY < oldScrollY) {
Log.i(TAG, "Scroll UP");
}
if (scrollY == 0) {
Log.i(TAG, "TOP SCROLL");
}
if (scrollY == (v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight())) {
Log.i(TAG, "BOTTOM SCROLL");
if(enableLoadMore) {
onLoadMoreRequested();
}
}
}
});
testAdapter = new TestAdapter(R.layout.item,list);
// testAdapter.setOnLoadMoreListener(this,recyclerView);
recyclerView.setAdapter(testAdapter);
}
@Override
public void onLoadMoreRequested() {
Log.i(TAG, "onLoadMoreRequested");
enableLoadMore = false;
testAdapter.addFooterView(getLayoutInflater().inflate(R.layout.footer,(ViewGroup) recyclerView.getParent(),false));
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
testAdapter.addData(loadData());
testAdapter.removeAllFooterView();
enableLoadMore = true;
}
},1500);
}
private List<String> loadData() {
List<String> mList = new ArrayList<>();
for (int i = 0;i<5;i++) {
mList.add("test");
}
return mList;
}
@Override
public void onRefresh() {
list= loadData();
testAdapter.setNewData(list);
swipe.setRefreshing(false);
}
}
activity_main.xml
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
tools:context="com.test.netrec.MainActivity">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.NestedScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:gravity="center"
android:text="自定义布局1" />
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#d1d1d1" />
<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:gravity="center"
android:text="自定义布局2" />
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#d1d1d1" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
footer.xml
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#fff"
tools:context="com.test.netrec.MainActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ProgressBar
android:layout_centerInParent="true"
android:layout_width="20dp"
android:layout_height="20dp" />
<TextView
android:textSize="12sp"
android:textColor="@android:color/black"
android:layout_marginLeft="8dp"
android:text="数据加载中..."
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
item.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#fff"
android:textColor="#f00"
android:text="item"
android:textSize="20sp"
android:gravity="center"
tools:context="com.test.netrec.MainActivity">
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#fff"
android:textColor="#f00"
android:text="item"
android:textSize="20sp"
android:gravity="center"
tools:context="com.test.netrec.MainActivity">
感谢楼上的写法,亲测可行。如果必须用ScrollView又不想用别的框架,可以使用楼上的写法来
非常感谢 AngelBo 解决了我APP首页问题
这样虽然是可以,但是我滑动到第四页之后就开始有了明显的卡顿现象,大神求教怎么解决这个问题
Most helpful comment
@chailijun 可以的
public class MainActivity extends AppCompatActivity implements BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {
private final String TAG = "MainActivity";
private RecyclerView recyclerView;
private NestedScrollView scrollView;
private SwipeRefreshLayout swipe;
// testAdapter.setOnLoadMoreListener(this,recyclerView);
recyclerView.setAdapter(testAdapter);
}
}
activity_main.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
tools:context="com.test.netrec.MainActivity">
footer.xml
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#fff"
tools:context="com.test.netrec.MainActivity">
item.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#fff"
android:textColor="#f00"
android:text="item"
android:textSize="20sp"
android:gravity="center"
tools:context="com.test.netrec.MainActivity">