Skip to content

Commit

Permalink
修复测试中遇到的一些问题,优化不断完善
Browse files Browse the repository at this point in the history
  • Loading branch information
zguop committed Feb 4, 2020
1 parent d07da3a commit 27bfd4c
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -30,52 +33,63 @@
*/
public class RecyclerActivity extends AppCompatActivity {

private Adapter adapter;
private SwipeRefreshLayout swipeRefreshLayout;
private Banner banner;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler);
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<MultiItemEntity> 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
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<Integer> data = Utils.getImage(i);
Expand All @@ -86,4 +100,25 @@ public void onClick(View v) {

}

private void loadData() {

List<MultiItemEntity> 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);

}

}
28 changes: 21 additions & 7 deletions app/src/main/java/com/to/aboomy/bannersample/adapter/Adapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,13 +20,17 @@

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

/**
* auth aboom
* date 2019-12-28
*/
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) {
Expand All @@ -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<MultiItemEntity> list = new ArrayList<>();

}

private void loadData() {
List<MultiItemEntity> 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);
Expand All @@ -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);
}
}
14 changes: 11 additions & 3 deletions app/src/main/res/layout/activity_recycler.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
android:id="@+id/list"
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</android.support.v4.widget.SwipeRefreshLayout>



<LinearLayout
android:layout_width="match_parent"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/fragment_test1_child.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
android:orientation="vertical">

<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe"
android:layout_width="match_parent"
android:layout_height="wrap_content">

Expand Down
4 changes: 2 additions & 2 deletions banner/src/main/java/com/to/aboomy/banner/Banner.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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++) {
Expand Down Expand Up @@ -377,7 +377,7 @@ public void setPages(List<?> items) {
* 是否正在轮播
*/
public boolean isAutoPlay() {
return isAutoPlay;
return isAutoPlay && realCount > 1;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ protected void onAttachedToWindow() {
isFirstLayoutToField = true;
}

public void setFirstLayoutToField(boolean firstLayoutToField) {
isFirstLayoutToField = firstLayoutToField;
}

private void onAttachedToRestFirstLayout() {
try {
//解决在RecyclerView中使用的bug,当viewPager画出屏幕时,并执行了onDetachedFromWindow,再回来时,第一次滑动时没有动画效果
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 27bfd4c

Please sign in to comment.