Skip to content

Commit

Permalink
1.添加标题显示
Browse files Browse the repository at this point in the history
  • Loading branch information
caikaidev committed Oct 31, 2016
1 parent 6a97412 commit 42f055b
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 36 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
6 changes: 6 additions & 0 deletions app/src/main/java/com/kcode/autoviewpager/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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";

Expand All @@ -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);
}
Expand All @@ -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;
}

Expand All @@ -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) {
Expand All @@ -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);
Expand All @@ -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);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -91,6 +92,21 @@ public void add(List<T> list){

}

public void init(List<T> 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;
Expand Down Expand Up @@ -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) {
Expand Down
28 changes: 28 additions & 0 deletions autoScrollViewPager/src/main/res/layout/point_center_text.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">

<TextView
android:id="@+id/subTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:textColor="@android:color/white"
android:textSize="18sp"/>

<LinearLayout
android:id="@+id/pointLayout"
android:layout_below="@id/subTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="16dp"
android:layout_centerHorizontal="true"
android:orientation="horizontal">
</LinearLayout>

</RelativeLayout>
33 changes: 33 additions & 0 deletions autoScrollViewPager/src/main/res/layout/point_right_text.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
>


<LinearLayout
android:id="@+id/pointLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_alignParentRight="true"
android:layout_margin="16dp"
android:layout_centerHorizontal="true"
android:orientation="horizontal">
</LinearLayout>

<TextView
android:id="@+id/subTitle"
android:layout_toLeftOf="@id/pointLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:textColor="@android:color/white"
android:gravity="center_vertical"
android:maxLines="1"
android:textSize="18sp"/>

</RelativeLayout>

0 comments on commit 42f055b

Please sign in to comment.