Skip to content

Commit

Permalink
fix 悬浮置顶加入下拉刷新 ,悬浮错位 #57
Browse files Browse the repository at this point in the history
  • Loading branch information
youlookwhat committed Jul 12, 2022
1 parent 602e94e commit f9ce5b1
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ private void cacheHeaderPositions() {
}
for (int i = 0; i < adapterData.size(); i++) {
if (StickyHeaderHandler.TYPE_STICKY_VIEW == mBaseAdapter.getItemViewType(i)) {
mHeaderPositions.add(i);
int customTopItemViewCount = mBaseAdapter.getCustomTopItemViewCount();
mHeaderPositions.add(i + customTopItemViewCount);
}
}
if (mHeaderHandler != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import java.util.List;
import java.util.Map;

import me.jingbin.library.adapter.BaseByRecyclerViewAdapter;

/**
* Created by jingbin on 2020-02-11.
*/
Expand Down Expand Up @@ -76,8 +78,8 @@ public void updateHeaderState(int firstVisiblePosition, Map<Integer, View> visib
} else {
// 否则就创建一个header视图
lastBoundPosition = headerPositionToShow;
RecyclerView.ViewHolder viewHolder = viewFactory.getViewHolderForPosition(headerPositionToShow);
attachHeader(viewHolder, headerPositionToShow);
RecyclerView.ViewHolder viewHolder = viewFactory.getViewHolderForPosition(headerPositionToShow - getRecyclerViewCustomTopItemViewCount());
attachHeader(viewHolder, headerPositionToShow - getRecyclerViewCustomTopItemViewCount());
}
} else if (checkMargins && headerAwayFromEdge(headerToCopy)) {
detachHeader(lastBoundPosition);
Expand All @@ -92,6 +94,21 @@ public void run() {
});
}

/**
* 处理RefreshView + HeaderView + EmptyView
* issues:https://github.com/youlookwhat/ByRecyclerView/issues/57
*/
private int getRecyclerViewCustomTopItemViewCount() {
int customTopItemViewCount = 0;
try {
BaseByRecyclerViewAdapter adapter = (BaseByRecyclerViewAdapter) mRecyclerView.getAdapter();
customTopItemViewCount = adapter.getCustomTopItemViewCount();
} catch (Exception e) {
e.printStackTrace();
}
return customTopItemViewCount;
}

private void checkHeaderPositions(final Map<Integer, View> visibleHeaders) {
if (currentHeader == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
public class StickyLinearLayoutManager extends LinearLayoutManager {

private BaseByRecyclerViewAdapter mHeaderProvider;
private BaseByRecyclerViewAdapter mBaseAdapter;
private StickyHeaderHandler mHeaderHandler;

private List<Integer> mHeaderPositions = new ArrayList<>();
Expand All @@ -34,7 +34,7 @@ public StickyLinearLayoutManager(Context context, BaseByRecyclerViewAdapter head

public StickyLinearLayoutManager(Context context, int orientation, boolean reverseLayout, BaseByRecyclerViewAdapter headerProvider) {
super(context, orientation, reverseLayout);
this.mHeaderProvider = headerProvider;
this.mBaseAdapter = headerProvider;
}

public void elevateHeaders(boolean elevateHeaders) {
Expand Down Expand Up @@ -132,7 +132,7 @@ private Map<Integer, View> getVisibleHeaders() {

private void cacheHeaderPositions() {
mHeaderPositions.clear();
List adapterData = mHeaderProvider.getData();
List adapterData = mBaseAdapter.getData();
if (adapterData == null) {
if (mHeaderHandler != null) {
mHeaderHandler.setHeaderPositions(mHeaderPositions);
Expand All @@ -141,8 +141,9 @@ private void cacheHeaderPositions() {
}

for (int i = 0; i < adapterData.size(); i++) {
if (StickyHeaderHandler.TYPE_STICKY_VIEW == mHeaderProvider.getItemViewType(i)) {
mHeaderPositions.add(i);
if (StickyHeaderHandler.TYPE_STICKY_VIEW == mBaseAdapter.getItemViewType(i)) {
int customTopItemViewCount = mBaseAdapter.getCustomTopItemViewCount();
mHeaderPositions.add(i+customTopItemViewCount);
}
}
if (mHeaderHandler != null) {
Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"
implementation "androidx.viewpager2:viewpager2:1.0.0"
testImplementation 'junit:junit:4.12'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'androidx.multidex:multidex:2.0.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public BaseByViewHolder<DataItemBean> onCreateViewHolder(@NonNull ViewGroup pare
}
}

private class TitleHolder extends BaseByViewHolder<DataItemBean> {
private static class TitleHolder extends BaseByViewHolder<DataItemBean> {
TitleHolder(ViewGroup viewGroup, int layoutId) {
super(viewGroup, layoutId);
}
Expand All @@ -93,7 +93,7 @@ protected void onBaseBindView(BaseByViewHolder<DataItemBean> holder, DataItemBea
}
}

private class ViewHolder extends BaseBindingHolder<DataItemBean, ItemHomeBinding> {
private static class ViewHolder extends BaseBindingHolder<DataItemBean, ItemHomeBinding> {
ViewHolder(ViewGroup viewGroup, int layoutId) {
super(viewGroup, layoutId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ private void initAdapter() {
itemDecoration.setDrawable(R.drawable.shape_line);
binding.recyclerView.addItemDecoration(itemDecoration);
binding.recyclerView.setAdapter(mAdapter);
binding.recyclerView.setOnRefreshListener(new ByRecyclerView.OnRefreshListener() {
@Override
public void onRefresh() {
page = 1;
mAdapter.setNewData(DataUtil.getMultiData(getActivity(), 20));
}
});
binding.recyclerView.setOnLoadMoreListener(new ByRecyclerView.OnLoadMoreListener() {
@Override
public void onLoadMore() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ private void initAdapter() {
itemDecoration.setDrawable(R.drawable.shape_line);
binding.recyclerView.addItemDecoration(itemDecoration);
binding.recyclerView.setAdapter(mAdapter);
// binding.recyclerView.setOnRefreshListener(new ByRecyclerView.OnRefreshListener() {
// @Override
// public void onRefresh() {
// page = 1;
// mAdapter.setNewData(DataUtil.getMultiData(getActivity(), 20));
// }
// });
binding.recyclerView.setOnRefreshListener(new ByRecyclerView.OnRefreshListener() {
@Override
public void onRefresh() {
page = 1;
mAdapter.setNewData(DataUtil.getMultiData(getActivity(), 20));
}
});
binding.recyclerView.setOnLoadMoreListener(new ByRecyclerView.OnLoadMoreListener() {
@Override
public void onLoadMore() {
Expand Down
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ buildscript {
repositories {
google()
jcenter()
mavenCentral()
maven {
url "https://maven.google.com"
}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.2'
classpath 'com.android.tools.build:gradle:3.4.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
// NOTE: Do not place your application dependencies here; they belong
Expand All @@ -22,6 +23,7 @@ allprojects {
repositories {
google()
jcenter()
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Expand Down

0 comments on commit f9ce5b1

Please sign in to comment.