From 42f055bff809a985734ed5f37ef14ed02a37d158 Mon Sep 17 00:00:00 2001 From: fccaikai <714564390@qq.com> Date: Mon, 31 Oct 2016 19:06:05 +0800 Subject: [PATCH] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0=E6=A0=87=E9=A2=98=E6=98=BE?= =?UTF-8?q?=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- .../com/kcode/autoviewpager/MainActivity.java | 6 ++ .../view/AutoScrollViewPager.java | 62 ++++++++----------- .../view/AutoViewPager.java | 10 +++ .../view/BaseViewPagerAdapter.java | 24 +++++++ .../src/main/res/layout/point_center_text.xml | 28 +++++++++ .../src/main/res/layout/point_right_text.xml | 33 ++++++++++ 7 files changed, 129 insertions(+), 36 deletions(-) create mode 100644 autoScrollViewPager/src/main/res/layout/point_center_text.xml create mode 100644 autoScrollViewPager/src/main/res/layout/point_right_text.xml diff --git a/README.md b/README.md index f154f53..4bfb851 100644 --- a/README.md +++ b/README.md @@ -140,7 +140,7 @@ compile 'com.kcode.github:autoScrollViewPager:0.9.3' * PointLayout add ```xmlns:app="http://schemas.android.com/apk/res-auto"``` to layout file. And , - user ```app:point_layout="left|right|center"```. + user ```app:point_layout="right|center"```. sample:[AutoViewPager](https://github.com/fccaikai/AutoScrollViewPager/tree/master/app) diff --git a/app/src/main/java/com/kcode/autoviewpager/MainActivity.java b/app/src/main/java/com/kcode/autoviewpager/MainActivity.java index d72ba04..dcca7fd 100644 --- a/app/src/main/java/com/kcode/autoviewpager/MainActivity.java +++ b/app/src/main/java/com/kcode/autoviewpager/MainActivity.java @@ -3,6 +3,7 @@ import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.ImageView; +import android.widget.TextView; import android.widget.Toast; import com.kcode.autoscrollviewpager.view.AutoScrollViewPager; @@ -35,6 +36,11 @@ protected void onCreate(Bundle savedInstanceState) { public void loadImage(ImageView view, int position, String url) { Picasso.with(MainActivity.this).load(url).into(view); } + + @Override + public void setSubTitle(TextView textView, int position, String s) { + textView.setText(s); + } }; mViewPager.setAdapter(adapter); diff --git a/autoScrollViewPager/src/main/java/com/kcode/autoscrollviewpager/view/AutoScrollViewPager.java b/autoScrollViewPager/src/main/java/com/kcode/autoscrollviewpager/view/AutoScrollViewPager.java index ab6d821..770b7f6 100644 --- a/autoScrollViewPager/src/main/java/com/kcode/autoscrollviewpager/view/AutoScrollViewPager.java +++ b/autoScrollViewPager/src/main/java/com/kcode/autoscrollviewpager/view/AutoScrollViewPager.java @@ -3,11 +3,13 @@ import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; -import android.view.Gravity; +import android.view.LayoutInflater; +import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout; +import android.widget.TextView; import com.kcode.autoscrollviewpager.R; @@ -17,7 +19,6 @@ public class AutoScrollViewPager extends RelativeLayout { - private final static String LEFT_POINT = "left"; private final static String RIGHT_POINT = "right"; private final static String CENTER_POINT = "center"; @@ -32,8 +33,18 @@ public class AutoScrollViewPager extends RelativeLayout { private Context mContext; private LinearLayout layout; + private View view;//底部文字和小圆点 + private TextView mSubTitle;//标题文字 - public AutoScrollViewPager(Context context) { + public TextView getSubTitle() { + return mSubTitle; + } + + public AutoViewPager getViewPager() { + return mViewPager; + } + + private AutoScrollViewPager(Context context) { super(context); init(context); } @@ -49,16 +60,16 @@ public AutoScrollViewPager(Context context, AttributeSet attrs, int defStyleAttr String pointLayoutStr = typedArray.getString(R.styleable.AutoScrollViewPager_point_layout); switch (pointLayoutStr) { - case LEFT_POINT: - pointLayout = LEFT_INT; - break; case RIGHT_POINT: pointLayout = RIGHT_INT; + view = LayoutInflater.from(context).inflate(R.layout.point_right_text,null); break; case CENTER_POINT: pointLayout = CENTER_INT; + view = LayoutInflater.from(context).inflate(R.layout.point_center_text,null); break; default: + view = LayoutInflater.from(context).inflate(R.layout.point_center_text,null); break; } @@ -71,8 +82,17 @@ public AutoScrollViewPager(Context context, AttributeSet attrs, int defStyleAttr private void init(Context context) { mContext = context; mViewPager = new AutoViewPager(context); - layout = new LinearLayout(mContext); addView(mViewPager); + + if (view != null) { + mSubTitle = (TextView) view.findViewById(R.id.subTitle); + layout = (LinearLayout) view.findViewById(R.id.pointLayout); + LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); + params.addRule(ALIGN_PARENT_BOTTOM); + view.setLayoutParams(params); + addView(view); + } + } public void setAdapter(BaseViewPagerAdapter adapter) { @@ -81,18 +101,12 @@ public void setAdapter(BaseViewPagerAdapter adapter) { } } - public AutoViewPager getViewPager() { - return mViewPager; - } - public void initPointView(int size) { - layout = new LinearLayout(mContext); for (int i = 0; i < size; i++) { ImageView imageView = new ImageView(mContext); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(20, 20); params.leftMargin = 8; - params.gravity = Gravity.CENTER; imageView.setLayoutParams(params); if (i == 0) { imageView.setBackgroundResource(R.drawable.point_checked); @@ -102,28 +116,6 @@ public void initPointView(int size) { layout.addView(imageView); } - - LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); - layoutParams.addRule(ALIGN_PARENT_BOTTOM); - switch (pointLayout) { - case RIGHT_INT: - layoutParams.addRule(ALIGN_PARENT_RIGHT); - break; - case LEFT_INT: - layoutParams.addRule(ALIGN_PARENT_LEFT); - break; - case CENTER_INT: - layoutParams.addRule(CENTER_HORIZONTAL); - break; - default: - layoutParams.addRule(ALIGN_PARENT_RIGHT); - break; - } - - - layoutParams.setMargins(12, 20, 12, 20); - layout.setLayoutParams(layoutParams); - addView(layout); } diff --git a/autoScrollViewPager/src/main/java/com/kcode/autoscrollviewpager/view/AutoViewPager.java b/autoScrollViewPager/src/main/java/com/kcode/autoscrollviewpager/view/AutoViewPager.java index 557fbcb..7cc3726 100644 --- a/autoScrollViewPager/src/main/java/com/kcode/autoscrollviewpager/view/AutoViewPager.java +++ b/autoScrollViewPager/src/main/java/com/kcode/autoscrollviewpager/view/AutoViewPager.java @@ -6,6 +6,7 @@ import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; +import android.widget.TextView; import java.util.Timer; import java.util.TimerTask; @@ -119,4 +120,13 @@ public boolean onTouchEvent(MotionEvent ev) { } return super.onTouchEvent(ev); } + + public TextView getSubTitle() { + if (getParent() instanceof AutoScrollViewPager){ + AutoScrollViewPager pager = (AutoScrollViewPager) getParent(); + return pager.getSubTitle(); + } + + return null; + } } diff --git a/autoScrollViewPager/src/main/java/com/kcode/autoscrollviewpager/view/BaseViewPagerAdapter.java b/autoScrollViewPager/src/main/java/com/kcode/autoscrollviewpager/view/BaseViewPagerAdapter.java index c71e5de..3c62e1b 100644 --- a/autoScrollViewPager/src/main/java/com/kcode/autoscrollviewpager/view/BaseViewPagerAdapter.java +++ b/autoScrollViewPager/src/main/java/com/kcode/autoscrollviewpager/view/BaseViewPagerAdapter.java @@ -7,6 +7,7 @@ import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; +import android.widget.TextView; import com.kcode.autoscrollviewpager.R; @@ -91,6 +92,21 @@ public void add(List list){ } + public void init(List list){ + if (data == null) { + data = new ArrayList<>(); + } + + data.clear(); + data.addAll(list); + + notifyDataSetChanged(); + + mView.start(); + mView.updatePointView(getRealCount()); + + } + @Override public int getCount() { return (data == null || data.size() == 0 ) ? 0 : Integer.MAX_VALUE; @@ -121,10 +137,18 @@ public void onClick(View view) { loadImage(view,position, data.get(position % getRealCount())); container.addView(view); + //设置标题 + if (mView.getSubTitle() != null){ + setSubTitle(mView.getSubTitle(),position,data.get(position % getRealCount())); + }else { + + } + return view; } public abstract void loadImage(ImageView view,int position,T t); + public abstract void setSubTitle(TextView textView,int position,T t); @Override public boolean isViewFromObject(View view, Object object) { diff --git a/autoScrollViewPager/src/main/res/layout/point_center_text.xml b/autoScrollViewPager/src/main/res/layout/point_center_text.xml new file mode 100644 index 0000000..ad4cf69 --- /dev/null +++ b/autoScrollViewPager/src/main/res/layout/point_center_text.xml @@ -0,0 +1,28 @@ + + + + + + + + + \ No newline at end of file diff --git a/autoScrollViewPager/src/main/res/layout/point_right_text.xml b/autoScrollViewPager/src/main/res/layout/point_right_text.xml new file mode 100644 index 0000000..425960d --- /dev/null +++ b/autoScrollViewPager/src/main/res/layout/point_right_text.xml @@ -0,0 +1,33 @@ + + + + + + + + + + \ No newline at end of file