Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NestedScrollView嵌套时的loadmore处理 #300

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ ext {

xDesign : 'com.google.android.material:material:1.0.0',
xAppCompat : 'androidx.appcompat:appcompat:1.0.2',
xRecyclerView: 'androidx.recyclerview:recyclerview:1.0.0',
xRecyclerView: 'androidx.recyclerview:recyclerview:1.1.0',
xCardView : 'androidx.cardview:cardview:1.0.0']
}
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ public void onScrolled(int dx, int dy) {
}
}

private void dispatchLoadMore() {
public void dispatchLoadMore() {
if (isLoadError) return;

if (!isAutoLoadMore) {
Expand Down
3 changes: 3 additions & 0 deletions xSample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@
<activity
android:name=".activity.group.MenuActivity"
android:label="Item菜单Sticky分组"/>
<activity
android:name=".activity.nestscrollview.NestscrollViewActivity"
android:label="NestscrollView嵌套"/>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.yanzhenjie.recyclerview.sample.activity.menu.MenuActivity;
import com.yanzhenjie.recyclerview.sample.activity.move.MoveActivity;
import com.yanzhenjie.recyclerview.sample.activity.nested.NestedActivity;
import com.yanzhenjie.recyclerview.sample.activity.nestscrollview.NestscrollViewActivity;

import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -79,6 +80,10 @@ public void onItemClick(View itemView, int position) {
startActivity(new Intent(this, GroupActivity.class));
break;
}
case 7: {
startActivity(new Intent(this, NestscrollViewActivity.class));
break;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package com.yanzhenjie.recyclerview.sample.activity.nestscrollview;

import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.yanzhenjie.recyclerview.SwipeRecyclerView;
import com.yanzhenjie.recyclerview.sample.R;

import java.util.ArrayList;
import java.util.List;

public class NestscrollViewActivity extends AppCompatActivity {

private NestedScrollView mNestedScrollView;

private SwipeRecyclerView mSwipeRecyclerView;

private List<Integer> mList ;

private Handler mHandler = new Handler();

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.item_nestscorllview);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
assert actionBar != null;
actionBar.setDisplayHomeAsUpEnabled(true);


mNestedScrollView = findViewById(R.id.nestedScrollView);
mSwipeRecyclerView = findViewById(R.id.swipeRecyclerView);

mList = new ArrayList<>();
mSwipeRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mSwipeRecyclerView.setAdapter(new Adapter());
setData();
mSwipeRecyclerView.getAdapter().notifyDataSetChanged();

mSwipeRecyclerView.useDefaultLoadMore();
mSwipeRecyclerView.setLoadMoreListener(new SwipeRecyclerView.LoadMoreListener() {
@Override
public void onLoadMore() {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
setData();
mSwipeRecyclerView.getAdapter().notifyDataSetChanged();
}
},1 * 1000);
}
});

mNestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
@Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
View onlyChild = v.getChildAt(0);
if (onlyChild.getHeight() <= scrollY + v.getHeight()) {
// 如果满足就是到底部了
//该方法需要是public
mSwipeRecyclerView.dispatchLoadMore();

/* //如果dispatchLoadMore 是private
mSwipeRecyclerView.onScrollStateChanged(2);
mSwipeRecyclerView.onScrolled(scrollX,scrollY);*/

}

}
});

}

/**
* 制作模拟数据
*/
private void setData() {
int baseNo = mList.size() ;
for(int i = baseNo ; i < baseNo + 20 ; i++ ){
mList.add(i);
}
/*//如果dispatchLoadMore是private
//去掉RecyclerView内的滚动状态,要不页面滚动会卡顿
//mSwipeRecyclerView.onScrollStateChanged(-1);*/

mSwipeRecyclerView.loadMoreFinish(false,true);
}

private class Adapter extends RecyclerView.Adapter<ViewHolder> {


@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_nestscorllview_item,parent,false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
TextView tvItemText = holder.itemView.findViewById(R.id.tvItemText);
tvItemText.setText("第" + mList.get(position).toString() + "个");
}

@Override
public int getItemCount() {
return mList.size();
}
}

private class ViewHolder extends RecyclerView.ViewHolder {

View itemView;

public ViewHolder(@NonNull View itemView) {
super(itemView);
this.itemView = itemView;
}
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
}
return true;
}

}
19 changes: 19 additions & 0 deletions xSample/src/main/res/layout/item_nestscorllview.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/toolbar_scroll"/>

<androidx.core.widget.NestedScrollView
android:id="@+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.yanzhenjie.recyclerview.SwipeRecyclerView
android:id="@+id/swipeRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.yanzhenjie.recyclerview.SwipeRecyclerView>

</androidx.core.widget.NestedScrollView>

</LinearLayout>
19 changes: 19 additions & 0 deletions xSample/src/main/res/layout/item_nestscorllview_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/tvItemText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:gravity="center"
android:textSize="16sp"
android:text=""></TextView>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#eaeaea"></View>
</LinearLayout>
1 change: 1 addition & 0 deletions xSample/src/main/res/values/array.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<item>和滑动布局嵌套使用</item>
<item>二级列表</item>
<item>Sticky分组效果</item>
<item>NestedScrollView嵌套</item>
</string-array>

<string-array name="menu_item">
Expand Down