From 27bfd4cecb4ae2d5dc0210049b29f23b01295b77 Mon Sep 17 00:00:00 2001 From: yubing <374735994@qq.com> Date: Tue, 4 Feb 2020 14:59:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=B5=8B=E8=AF=95=E4=B8=AD?= =?UTF-8?q?=E9=81=87=E5=88=B0=E7=9A=84=E4=B8=80=E4=BA=9B=E9=97=AE=E9=A2=98?= =?UTF-8?q?=EF=BC=8C=E4=BC=98=E5=8C=96=E4=B8=8D=E6=96=AD=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../activity/RecyclerActivity.java | 71 ++++++++++++++----- .../aboomy/bannersample/adapter/Adapter.java | 28 ++++++-- .../adapter/Test1ChildAdapter.java | 24 +++++-- .../fragment/Test1ChildFragment.java | 28 ++++++-- app/src/main/res/layout/activity_recycler.xml | 14 +++- .../main/res/layout/fragment_test1_child.xml | 1 + .../java/com/to/aboomy/banner/Banner.java | 4 +- .../com/to/aboomy/banner/BannerViewPager.java | 4 ++ .../to/aboomy/banner/ViewPagerScroller.java | 2 +- 9 files changed, 132 insertions(+), 44 deletions(-) diff --git a/app/src/main/java/com/to/aboomy/bannersample/activity/RecyclerActivity.java b/app/src/main/java/com/to/aboomy/bannersample/activity/RecyclerActivity.java index 06c6acf..c672fe0 100644 --- a/app/src/main/java/com/to/aboomy/bannersample/activity/RecyclerActivity.java +++ b/app/src/main/java/com/to/aboomy/bannersample/activity/RecyclerActivity.java @@ -3,9 +3,12 @@ import android.graphics.Color; import android.os.Bundle; import android.support.annotation.Nullable; +import android.support.v4.view.ViewPager; +import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; +import android.util.Log; import android.view.View; import android.widget.TextView; @@ -30,6 +33,10 @@ */ public class RecyclerActivity extends AppCompatActivity { + private Adapter adapter; + private SwipeRefreshLayout swipeRefreshLayout; + private Banner banner; + @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -37,37 +44,44 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { StatusBarUtil.setStatusBarColor(this, Color.WHITE); RecyclerView recyclerView = findViewById(R.id.list); - final Adapter adapter = new Adapter(); + swipeRefreshLayout = findViewById(R.id.swipe); + + adapter = new Adapter(); recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setAdapter(adapter); + swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { + @Override + public void onRefresh() { + loadData(); + } + }); View inflate = View.inflate(this, R.layout.item_view_banner, null); TextView textView = inflate.findViewById(R.id.text); textView.setText("我是被addHeaderView添加进来的"); adapter.addHeaderView(inflate); - Banner banner = inflate.findViewById(R.id.banner); + banner = inflate.findViewById(R.id.banner); banner.setHolderCreator(new ImageHolderCreator()); banner.setIndicator(new IndicatorView(this).setIndicatorColor(Color.GRAY).setIndicatorSelectorColor(Color.WHITE)); - banner.setPages(Utils.getImage(4)); - + banner.setOuterPageChangeListener(new ViewPager.OnPageChangeListener() { + @Override + public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { - List list = new ArrayList<>(); + } - BannerBean bannerBean = new BannerBean(); - bannerBean.urls = new ArrayList<>(); - for (int i = 0; i < 5; i++) { - bannerBean.urls.add(Utils.getRandomImage()); - } - list.add(bannerBean); + @Override + public void onPageSelected(int position) { + Log.e("aa" , " position " + position); + } - for (int i = 0; i < 100; i++) { - TextBean textBean = new TextBean(); - textBean.text = "--- " + i; - list.add(textBean); - } - adapter.addData(list); + @Override + public void onPageScrollStateChanged(int state) { + } + }); + banner.setPages(Utils.getImage(4)); + loadData(); findViewById(R.id.update).setOnClickListener(new View.OnClickListener() { @Override @@ -75,7 +89,7 @@ public void onClick(View v) { BannerBean b = (BannerBean) adapter.getItem(0); b.urls.clear(); int i = new Random().nextInt(5); - if(i == 0){ + if (i == 0) { i = 3; } List data = Utils.getImage(i); @@ -86,4 +100,25 @@ public void onClick(View v) { } + private void loadData() { + + List list = new ArrayList<>(); + + BannerBean bannerBean = new BannerBean(); + bannerBean.urls = new ArrayList<>(); + for (int i = 0; i < 5; i++) { + bannerBean.urls.add(Utils.getRandomImage()); + } + list.add(bannerBean); + for (int i = 0; i < 100; i++) { + TextBean textBean = new TextBean(); + textBean.text = "--- " + i; + list.add(textBean); + } + adapter.replaceData(list); + + swipeRefreshLayout.setRefreshing(false); + + } + } diff --git a/app/src/main/java/com/to/aboomy/bannersample/adapter/Adapter.java b/app/src/main/java/com/to/aboomy/bannersample/adapter/Adapter.java index 00d2099..77fa3db 100644 --- a/app/src/main/java/com/to/aboomy/bannersample/adapter/Adapter.java +++ b/app/src/main/java/com/to/aboomy/bannersample/adapter/Adapter.java @@ -2,16 +2,17 @@ import android.graphics.Color; import android.support.annotation.NonNull; +import android.view.ViewGroup; import com.chad.library.adapter.base.BaseMultiItemQuickAdapter; import com.chad.library.adapter.base.BaseViewHolder; import com.chad.library.adapter.base.entity.MultiItemEntity; import com.to.aboomy.banner.Banner; import com.to.aboomy.banner.IndicatorView; -import com.to.aboomy.bannersample.creator.ImageHolderCreator; import com.to.aboomy.bannersample.R; import com.to.aboomy.bannersample.bean.BannerBean; import com.to.aboomy.bannersample.bean.TextBean; +import com.to.aboomy.bannersample.creator.ImageHolderCreator; /** * auth aboom @@ -25,17 +26,30 @@ public Adapter() { addItemType(2, R.layout.item_text); } + + @Override + protected BaseViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) { + BaseViewHolder baseViewHolder = super.onCreateDefViewHolder(parent, viewType); + if(viewType == 1){ + Banner banner = baseViewHolder.getView(R.id.banner); + IndicatorView qyIndicator = new IndicatorView(mContext) + .setIndicatorColor(Color.BLACK) + .setIndicatorSelectorColor(Color.WHITE); + banner. setIndicator(qyIndicator) + .setHolderCreator(new ImageHolderCreator()); + + } + + return baseViewHolder; + } + @Override protected void convert(@NonNull BaseViewHolder helper, MultiItemEntity item) { if (item.getItemType() == 1) { BannerBean bannerBean = (BannerBean) item; Banner itemView = helper.getView(R.id.banner); - IndicatorView qyIndicator = new IndicatorView(itemView.getContext()) - .setIndicatorColor(Color.BLACK) - .setIndicatorSelectorColor(Color.WHITE); - itemView.setIndicator(qyIndicator) - .setHolderCreator(new ImageHolderCreator()) - .setPages(bannerBean.urls, itemView.getCurrentPager()); + itemView.setPages(bannerBean.urls, itemView.getCurrentPager()); + helper.setText(R.id.text, "我是recyclerView的一个item"); } else { TextBean textBean = (TextBean) item; diff --git a/app/src/main/java/com/to/aboomy/bannersample/adapter/Test1ChildAdapter.java b/app/src/main/java/com/to/aboomy/bannersample/adapter/Test1ChildAdapter.java index 2ee0524..9863c4a 100644 --- a/app/src/main/java/com/to/aboomy/bannersample/adapter/Test1ChildAdapter.java +++ b/app/src/main/java/com/to/aboomy/bannersample/adapter/Test1ChildAdapter.java @@ -2,6 +2,7 @@ import android.graphics.Color; import android.support.annotation.NonNull; +import android.view.ViewGroup; import com.chad.library.adapter.base.BaseMultiItemQuickAdapter; import com.chad.library.adapter.base.BaseViewHolder; @@ -26,17 +27,28 @@ public Test1ChildAdapter() { addItemType(2, R.layout.item_text); } + @Override + protected BaseViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) { + BaseViewHolder baseViewHolder = super.onCreateDefViewHolder(parent, viewType); + if(viewType == 1){ + Banner banner = baseViewHolder.getView(R.id.banner); + IndicatorView qyIndicator = new IndicatorView(mContext) + .setIndicatorColor(Color.BLACK) + .setIndicatorSelectorColor(Color.WHITE); + banner. setIndicator(qyIndicator) + .setHolderCreator(new ImageTest1ChildHolderCreator()); + + } + + return baseViewHolder; + } + @Override protected void convert(@NonNull BaseViewHolder helper, MultiItemEntity item) { if (item.getItemType() == 1) { BannerBean bannerBean = (BannerBean) item; Banner itemView = helper.getView(R.id.banner); - IndicatorView qyIndicator = new IndicatorView(itemView.getContext()) - .setIndicatorColor(Color.BLACK) - .setIndicatorSelectorColor(Color.WHITE); - itemView.setIndicator(qyIndicator) - .setHolderCreator(new ImageTest1ChildHolderCreator()) - .setPages(bannerBean.urls, itemView.getCurrentPager()); + itemView.setPages(bannerBean.urls, itemView.getCurrentPager()); } else { TextBean textBean = (TextBean) item; helper.setText(R.id.text, textBean.text); diff --git a/app/src/main/java/com/to/aboomy/bannersample/fragment/Test1ChildFragment.java b/app/src/main/java/com/to/aboomy/bannersample/fragment/Test1ChildFragment.java index e5df225..80728a4 100644 --- a/app/src/main/java/com/to/aboomy/bannersample/fragment/Test1ChildFragment.java +++ b/app/src/main/java/com/to/aboomy/bannersample/fragment/Test1ChildFragment.java @@ -4,6 +4,7 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; +import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; @@ -19,6 +20,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.Random; /** * auth aboom @@ -26,6 +28,9 @@ */ public class Test1ChildFragment extends Fragment { + private SwipeRefreshLayout swipeRefreshLayout; + private Test1ChildAdapter adapter; + @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { @@ -36,17 +41,27 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); RecyclerView recyclerView = view.findViewById(R.id.list); + swipeRefreshLayout = view.findViewById(R.id.swipe); + swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { + @Override + public void onRefresh() { + loadData(); + } + }); - final Test1ChildAdapter adapter = new Test1ChildAdapter(); + adapter = new Test1ChildAdapter(); recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); recyclerView.setAdapter(adapter); + loadData(); - List list = new ArrayList<>(); - + } + private void loadData() { + List list = new ArrayList<>(); BannerBean bannerBean = new BannerBean(); bannerBean.urls = new ArrayList<>(); - for (int i = 0; i < 2; i++) { + int count = new Random().nextInt(4) + 1; + for (int i = 0; i < count; i++) { bannerBean.urls.add(Utils.getRandomImage()); } list.add(bannerBean); @@ -56,8 +71,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat textBean.text = "--- " + i; list.add(textBean); } - adapter.addData(list); - - + adapter.replaceData(list); + swipeRefreshLayout.setRefreshing(false); } } diff --git a/app/src/main/res/layout/activity_recycler.xml b/app/src/main/res/layout/activity_recycler.xml index bfe5975..9a0643a 100644 --- a/app/src/main/res/layout/activity_recycler.xml +++ b/app/src/main/res/layout/activity_recycler.xml @@ -3,10 +3,18 @@ android:layout_width="match_parent" android:layout_height="match_parent"> - + android:layout_height="wrap_content"> + + + + + diff --git a/banner/src/main/java/com/to/aboomy/banner/Banner.java b/banner/src/main/java/com/to/aboomy/banner/Banner.java index 31ee334..71d0a06 100644 --- a/banner/src/main/java/com/to/aboomy/banner/Banner.java +++ b/banner/src/main/java/com/to/aboomy/banner/Banner.java @@ -205,6 +205,7 @@ private void startPager(int startPosition) { viewPager.setAdapter(adapter); currentPage = startPosition + sidePage; viewPager.setScrollable(realCount > 1); + viewPager.setFirstLayoutToField(false); viewPager.setFocusable(true); viewPager.setCurrentItem(currentPage); if (indicator != null) { @@ -223,7 +224,6 @@ private void createImages(List items) { return; } realCount = items.size(); - isAutoPlay = isAutoPlay && realCount > 1; sidePage = needPage / NORMAL_COUNT; needCount = realCount + needPage; for (int i = 0; i < needCount; i++) { @@ -377,7 +377,7 @@ public void setPages(List items) { * 是否正在轮播 */ public boolean isAutoPlay() { - return isAutoPlay; + return isAutoPlay && realCount > 1; } /** diff --git a/banner/src/main/java/com/to/aboomy/banner/BannerViewPager.java b/banner/src/main/java/com/to/aboomy/banner/BannerViewPager.java index 1106877..753a87c 100644 --- a/banner/src/main/java/com/to/aboomy/banner/BannerViewPager.java +++ b/banner/src/main/java/com/to/aboomy/banner/BannerViewPager.java @@ -147,6 +147,10 @@ protected void onAttachedToWindow() { isFirstLayoutToField = true; } + public void setFirstLayoutToField(boolean firstLayoutToField) { + isFirstLayoutToField = firstLayoutToField; + } + private void onAttachedToRestFirstLayout() { try { //解决在RecyclerView中使用的bug,当viewPager画出屏幕时,并执行了onDetachedFromWindow,再回来时,第一次滑动时没有动画效果 diff --git a/banner/src/main/java/com/to/aboomy/banner/ViewPagerScroller.java b/banner/src/main/java/com/to/aboomy/banner/ViewPagerScroller.java index cf0ce25..442af5c 100755 --- a/banner/src/main/java/com/to/aboomy/banner/ViewPagerScroller.java +++ b/banner/src/main/java/com/to/aboomy/banner/ViewPagerScroller.java @@ -20,7 +20,7 @@ public void startScroll(int startX, int startY, int dx, int dy) { super.startScroll(startX, startY, dx, dy, scrollDuration); } - public void setScrollDuration(int scrollDuration) { + void setScrollDuration(int scrollDuration) { this.scrollDuration = scrollDuration; } } \ No newline at end of file