Skip to content

Commit

Permalink
整合 createView
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaomsh committed Sep 26, 2019
1 parent 814f1a3 commit 460a3af
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 71 deletions.
13 changes: 5 additions & 8 deletions LibBanner/src/main/java/com/ms/banner/Banner.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ private void initViewPagerScroll() {
scroller.setDuration(scrollTime);
mField.set(viewPager, scroller);
} catch (Exception e) {

e.printStackTrace();
}
}

Expand Down Expand Up @@ -206,7 +206,7 @@ public Banner setBannerAnimation(Class<? extends ViewPager.PageTransformer> tran
try {
viewPager.setPageTransformer(true, transformer.newInstance());
} catch (Exception e) {

e.printStackTrace();
}
return this;
}
Expand Down Expand Up @@ -440,14 +440,14 @@ else if (bannerStyle == BannerConfig.CUSTOM_INDICATOR)
private void setData() {
if (isLoop) {
if (mCurrentPage > 0 && mCurrentPage < count) {
this.currentItem = NUM / 2 - ((NUM / 2) % count) + 1 + mCurrentPage;
currentItem = NUM / 2 - ((NUM / 2) % count) + 1 + mCurrentPage;
} else {
currentItem = NUM / 2 - ((NUM / 2) % count) + 1;
}
lastPosition = 1;
} else {
if (mCurrentPage > 0 && mCurrentPage < count) {
this.currentItem = mCurrentPage;
currentItem = mCurrentPage;
} else {
currentItem = 0;
}
Expand Down Expand Up @@ -572,12 +572,9 @@ public Object instantiateItem(ViewGroup container, final int position) {
if (creator == null) {
throw new RuntimeException("[Banner] --> The layout is not specified,please set holder");
}
View view = creator.createView(container.getContext());
View view = creator.createView(container.getContext(), toRealPosition(position), mDatas.get(toRealPosition(position)));
container.addView(view);

if (mDatas != null && mDatas.size() > 0) {
creator.onBind(container.getContext(), toRealPosition(position), mDatas.get(toRealPosition(position)));
}
if (listener != null) {
view.setOnClickListener(new OnClickListener() {
@Override
Expand Down
35 changes: 14 additions & 21 deletions LibBanner/src/main/java/com/ms/banner/BannerNew.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class BannerNew extends FrameLayout implements ViewPager.OnPageChangeList
private int titleTextSize;
private int count = 0;
private int currentItem = -1;
private int mCurrentPage = 0;
private int gravity = -1;
private int lastPosition;
private List<String> titles;
Expand Down Expand Up @@ -237,21 +238,13 @@ public BannerNew setViewPagerIsScroll(boolean isScroll) {
}

public BannerNew setCurrentPage(@IntRange(from = 0) int page) {
if (count == 0)
return this;
if (page > count) {
throw new RuntimeException("[Banner] --> The current page is out of range");
}
if (isLoop) {
this.currentItem = page + 1;
} else {
this.currentItem = page;
}
mCurrentPage = page;
return this;
}

public BannerNew setPages(List<?> datas, BannerViewHolder creator) {
this.mDatas = datas;
this.mDatas.clear();
this.mDatas.addAll(datas);
this.creator = creator;
this.count = datas.size();
return this;
Expand All @@ -271,19 +264,18 @@ public void update(List<?> imageUrls) {
if (imageUrls == null) {
imageUrls = new ArrayList<>();
}
this.mDatas.clear();
this.indicatorImages.clear();
if (imageUrls.size() == 0) {
bannerDefaultImage.setVisibility(VISIBLE);
this.mDatas.clear();
this.indicatorImages.clear();
this.count = 0;
if (adapter != null) {
adapter.notifyDataSetChanged();
}
} else {
this.mDatas.clear();
this.indicatorImages.clear();
this.mDatas.addAll(imageUrls);
this.count = this.mDatas.size();
setOffscreenPageLimit(imageUrls.size());
start();
}
}
Expand Down Expand Up @@ -446,12 +438,16 @@ else if (bannerStyle == BannerConfig.CUSTOM_INDICATOR)

private void setData() {
if (isLoop) {
if (currentItem < 0) {
if (mCurrentPage > 0 && mCurrentPage < count) {
currentItem = 1 + mCurrentPage;
} else {
currentItem = 1;
}
lastPosition = 1;
} else {
if (currentItem < 0) {
if (mCurrentPage > 0 && mCurrentPage < count) {
currentItem = mCurrentPage;
} else {
currentItem = 0;
}
lastPosition = 0;
Expand Down Expand Up @@ -571,12 +567,9 @@ public Object instantiateItem(ViewGroup container, final int position) {
if (creator == null) {
throw new RuntimeException("[Banner] --> The layout is not specified,please set holder");
}
View view = creator.createView(container.getContext());
View view = creator.createView(container.getContext(), toRealPosition(position), mDatas.get(toRealPosition(position)));
container.addView(view);

if (mDatas != null && mDatas.size() > 0) {
creator.onBind(container.getContext(), toRealPosition(position), mDatas.get(toRealPosition(position)));
}
if (listener != null) {
view.setOnClickListener(new OnClickListener() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

public interface BannerViewHolder<T> {

View createView(Context context);
View createView(Context context, int position, T data);

void onBind(Context context, int position, T data);
}
20 changes: 8 additions & 12 deletions demo/src/main/java/com/test/ui/CustomViewHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,19 @@
*/
public class CustomViewHolder implements BannerViewHolder<Object> {

private ImageView mImageView;

@Override
public View createView(Context context) {
public View createView(Context context, int position, Object data) {
// 返回mImageView页面布局
mImageView = new ImageView(context);
ImageView imageView = new ImageView(context);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT
);
mImageView.setLayoutParams(params);
mImageView.setScaleType(ImageView.ScaleType.FIT_XY);
return mImageView;
}
imageView.setLayoutParams(params);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);

@Override
public void onBind(Context context, int position, Object data) {
// 数据绑定
Glide.with(context).load(data).into(mImageView);
Glide.with(context).load(data).into(imageView);

return imageView;
}

}
21 changes: 9 additions & 12 deletions demo/src/main/java/com/test/ui/CustomViewHolder2.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.test.ui;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -19,21 +20,17 @@
*/
public class CustomViewHolder2 implements BannerViewHolder<CustomData> {

private TextView mTitle;
private TextView mPosition;

@SuppressLint("InflateParams")
@Override
public View createView(Context context) {
public View createView(Context context, int position, CustomData data) {
View view = LayoutInflater.from(context).inflate(R.layout.banner_item, null);
mTitle = (TextView) view.findViewById(R.id.title);
mPosition = (TextView) view.findViewById(R.id.position);
TextView title = view.findViewById(R.id.title);
TextView position1 = view.findViewById(R.id.position);

title.setText(data.getName());
position1.setText(String.valueOf(position));

return view;
}

@Override
public void onBind(Context context, int position, CustomData data) {
// 数据绑定
mTitle.setText(data.getName());
mPosition.setText(position + "");
}
}
26 changes: 10 additions & 16 deletions demo/src/main/java/com/test/ui/CustomViewHolder3.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.test.ui;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -22,25 +23,16 @@
*/
public class CustomViewHolder3 implements BannerViewHolder<CustomData> {

private TextView mPosition;
private LinearLayout ll;
private ImageView image1;
private ImageView image2;

@SuppressLint("InflateParams")
@Override
public View createView(Context context) {
public View createView(Context context, int position, CustomData data) {
View view = LayoutInflater.from(context).inflate(R.layout.banner_item2, null);
mPosition = (TextView) view.findViewById(R.id.position);
ll = (LinearLayout) view.findViewById(R.id.ll_group);
image1 = (ImageView) view.findViewById(R.id.image1);
image2 = (ImageView) view.findViewById(R.id.image2);
return view;
}
TextView position1 = view.findViewById(R.id.position);
LinearLayout ll = view.findViewById(R.id.ll_group);
ImageView image1 = view.findViewById(R.id.image1);
ImageView image2 = view.findViewById(R.id.image2);

@Override
public void onBind(Context context, int position, CustomData data) {
// 数据绑定
mPosition.setText(position + "");
position1.setText(String.valueOf(position));
if (data.isMovie()) {
ll.setVisibility(View.GONE);
image2.setVisibility(View.VISIBLE);
Expand All @@ -52,5 +44,7 @@ public void onBind(Context context, int position, CustomData data) {
Glide.with(context).load(data.getUrl()).into(image1);
Glide.with(context).load(R.mipmap.b1).into(image2);
}
return view;
}

}

0 comments on commit 460a3af

Please sign in to comment.