From 7f462e3afb37e840f134e444e4d9ae5382e24bbb Mon Sep 17 00:00:00 2001 From: jingbin <770413277@qq.com> Date: Fri, 3 Jan 2020 17:28:17 +0800 Subject: [PATCH] =?UTF-8?q?demo=20=E5=AE=8C=E5=96=84=E7=A4=BA=E4=BE=8B?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../byrecyclerview/activity/HeaderFooterActivity.java | 11 ++++++++--- .../byrecyclerview/activity/MultiItemActivity.java | 3 ++- .../byrecyclerview/activity/SimpleActivity.java | 5 ++--- .../jingbin/byrecyclerview/adapter/MultiAdapter.java | 8 +++++--- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/me/jingbin/byrecyclerview/activity/HeaderFooterActivity.java b/app/src/main/java/me/jingbin/byrecyclerview/activity/HeaderFooterActivity.java index af04128..6f0ff33 100644 --- a/app/src/main/java/me/jingbin/byrecyclerview/activity/HeaderFooterActivity.java +++ b/app/src/main/java/me/jingbin/byrecyclerview/activity/HeaderFooterActivity.java @@ -5,7 +5,6 @@ import android.view.View; import android.view.ViewGroup; -import androidx.core.content.ContextCompat; import androidx.databinding.DataBindingUtil; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -52,7 +51,7 @@ private void initAdapter() { LinearLayoutManager layoutManager = new LinearLayoutManager(this); layoutManager.setOrientation(RecyclerView.VERTICAL); binding.recyclerView.setLayoutManager(layoutManager); - binding.recyclerView.addItemDecoration(new SpacesItemDecoration(this, SpacesItemDecoration.VERTICAL).setDrawable(R.drawable.shape_line)); + binding.recyclerView.addItemDecoration(new SpacesItemDecoration(this, SpacesItemDecoration.VERTICAL,4).setDrawable(R.drawable.shape_line)); binding.recyclerView.setAdapter(mAdapter); binding.recyclerView.setOnLoadMoreListener(new ByRecyclerView.OnLoadMoreListener() { @Override @@ -60,7 +59,7 @@ public void onLoadMore() { binding.recyclerView.postDelayed(new Runnable() { @Override public void run() { - if (page == 1) { + if (page == 2) { binding.recyclerView.loadMoreEnd(); return; } @@ -84,6 +83,12 @@ public void onClick(View v, int position) { binding.recyclerView.addHeaderView(R.layout.layout_header_view); binding.recyclerView.addHeaderView(headerBinding.getRoot()); + binding.recyclerView.setOnRefreshListener(new ByRecyclerView.OnRefreshListener() { + @Override + public void onRefresh() { + binding.recyclerView.setRefreshing(false); + } + }); headerBinding.tvText.setText("头布局1\n(点我添加Header2)"); footerBinding.tvText.setText("尾布局1\n(点我添加Footer2)"); header2Binding.tvText.setText("头布局2"); diff --git a/app/src/main/java/me/jingbin/byrecyclerview/activity/MultiItemActivity.java b/app/src/main/java/me/jingbin/byrecyclerview/activity/MultiItemActivity.java index 7f69a2f..f33f12f 100644 --- a/app/src/main/java/me/jingbin/byrecyclerview/activity/MultiItemActivity.java +++ b/app/src/main/java/me/jingbin/byrecyclerview/activity/MultiItemActivity.java @@ -41,10 +41,11 @@ private void initAdapter() { GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 6, RecyclerView.VERTICAL, false); binding.recyclerView.setLayoutManager(gridLayoutManager); - SpacesItemDecoration itemDecoration = new SpacesItemDecoration(this, SpacesItemDecoration.VERTICAL); + SpacesItemDecoration itemDecoration = new SpacesItemDecoration(this, SpacesItemDecoration.VERTICAL, 1); itemDecoration.setDrawable(R.drawable.shape_line); binding.recyclerView.addItemDecoration(itemDecoration); binding.recyclerView.setAdapter(mAdapter); + binding.recyclerView.addHeaderView(R.layout.layout_header_view); binding.recyclerView.setOnRefreshListener(new ByRecyclerView.OnRefreshListener() { @Override public void onRefresh() { diff --git a/app/src/main/java/me/jingbin/byrecyclerview/activity/SimpleActivity.java b/app/src/main/java/me/jingbin/byrecyclerview/activity/SimpleActivity.java index 537e53d..bb6b09a 100644 --- a/app/src/main/java/me/jingbin/byrecyclerview/activity/SimpleActivity.java +++ b/app/src/main/java/me/jingbin/byrecyclerview/activity/SimpleActivity.java @@ -38,9 +38,8 @@ private void initAdapter() { LinearLayoutManager layoutManager = new LinearLayoutManager(this); layoutManager.setOrientation(RecyclerView.VERTICAL); binding.recyclerView.setLayoutManager(layoutManager); - SpacesItemDecoration itemDecoration = new SpacesItemDecoration(this, SpacesItemDecoration.VERTICAL); - itemDecoration.setDrawable(R.drawable.shape_line); - binding.recyclerView.addItemDecoration(itemDecoration); + binding.recyclerView.setRefreshEnabled(true); + binding.recyclerView.addItemDecoration(new SpacesItemDecoration(this).setHeaderNoShowDivider(1).setDrawable(R.drawable.shape_line)); binding.recyclerView.setAdapter(mAdapter); binding.recyclerView.setOnLoadMoreListener(new ByRecyclerView.OnLoadMoreListener() { @Override diff --git a/app/src/main/java/me/jingbin/byrecyclerview/adapter/MultiAdapter.java b/app/src/main/java/me/jingbin/byrecyclerview/adapter/MultiAdapter.java index 73d047d..81aeb95 100644 --- a/app/src/main/java/me/jingbin/byrecyclerview/adapter/MultiAdapter.java +++ b/app/src/main/java/me/jingbin/byrecyclerview/adapter/MultiAdapter.java @@ -30,7 +30,7 @@ public MultiAdapter(List data) { @Override public int getItemViewType(int position) { - if (position < getData().size()) { + if (0 <= position && position < getData().size()) { DataItemBean itemData = getItemData(position); if ("title".equals(itemData.getType())) { return StickyView.TYPE_STICKY_VIEW; @@ -52,10 +52,12 @@ public void onAttachedToRecyclerView(@NonNull final RecyclerView recyclerView) { gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { @Override public int getSpanSize(int position) { - int type = getItemViewType(position); - if (byRecyclerView.isLoadMoreView(position)) { + if (byRecyclerView.isLoadMoreView(position) + || byRecyclerView.isRefreshHeader(position) + || byRecyclerView.isHeaderView(position)) { return gridManager.getSpanCount(); } + int type = getItemViewType(position - byRecyclerView.getCustomTopItemViewCount()); switch (type) { case StickyView.TYPE_STICKY_VIEW: // title栏显示一列