Skip to content

Commit

Permalink
1.自定义指示条的位置。左、中、右
Browse files Browse the repository at this point in the history
  • Loading branch information
caikaidev committed Oct 28, 2016
1 parent 06bb02c commit 494ff04
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 93 deletions.
176 changes: 88 additions & 88 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,112 +31,112 @@ compile 'com.kcode.github:autoScrollViewPager:0.7.0'
##Usage
* in layout

```
<com.kcode.autoscrollviewpager.view.AutoScrollViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="220dp">
</com.kcode.autoscrollviewpager.view.AutoScrollViewPager>
```
```
<com.kcode.autoscrollviewpager.view.AutoScrollViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="220dp">
</com.kcode.autoscrollviewpager.view.AutoScrollViewPager>
```
* in activity:

```
package com.kcode.autoviewpager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.Toast;
import com.kcode.autoscrollviewpager.view.AutoScrollViewPager;
import com.kcode.autoscrollviewpager.view.BaseViewPagerAdapter;
import com.kcode.autoviewpager.bean.Picture;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private AutoScrollViewPager mViewPager;
private String[] paths = {"https://ss3.baidu.com/-fo3dSag_xI4khGko9WTAnF6hhy/image/h%3D200/sign=c493b482b47eca800d053ee7a1229712/8cb1cb1349540923abd671df9658d109b2de49d7.jpg",
"https://ss0.baidu.com/94o3dSag_xI4khGko9WTAnF6hhy/image/h%3D200/sign=45fbfa5555da81cb51e684cd6267d0a4/2f738bd4b31c8701491ea047237f9e2f0608ffe3.jpg",
"https://ss2.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/image/h%3D200/sign=ae0e95c0fc1986185e47e8847aec2e69/0b46f21fbe096b63eb314ef108338744ebf8ac62.jpg",
"https://ss3.baidu.com/9fo3dSag_xI4khGko9WTAnF6hhy/image/h%3D200/sign=1fad2b46952397ddc9799f046983b216/dc54564e9258d109c94bbb13d558ccbf6d814de2.jpg",
"https://ss1.baidu.com/9vo3dSag_xI4khGko9WTAnF6hhy/image/h%3D200/sign=ff0999f6d4160924c325a51be406359b/86d6277f9e2f070861ccd4a0ed24b899a801f241.jpg"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = (AutoScrollViewPager) findViewById(R.id.viewPager);
mViewPager.setAdapter(new BaseViewPagerAdapter<String>(this,initData(),listener) {
```
package com.kcode.autoviewpager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.Toast;
import com.kcode.autoscrollviewpager.view.AutoScrollViewPager;
import com.kcode.autoscrollviewpager.view.BaseViewPagerAdapter;
import com.kcode.autoviewpager.bean.Picture;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private AutoScrollViewPager mViewPager;
private String[] paths = {"https://ss3.baidu.com/-fo3dSag_xI4khGko9WTAnF6hhy/image/h%3D200/sign=c493b482b47eca800d053ee7a1229712/8cb1cb1349540923abd671df9658d109b2de49d7.jpg",
"https://ss0.baidu.com/94o3dSag_xI4khGko9WTAnF6hhy/image/h%3D200/sign=45fbfa5555da81cb51e684cd6267d0a4/2f738bd4b31c8701491ea047237f9e2f0608ffe3.jpg",
"https://ss2.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/image/h%3D200/sign=ae0e95c0fc1986185e47e8847aec2e69/0b46f21fbe096b63eb314ef108338744ebf8ac62.jpg",
"https://ss3.baidu.com/9fo3dSag_xI4khGko9WTAnF6hhy/image/h%3D200/sign=1fad2b46952397ddc9799f046983b216/dc54564e9258d109c94bbb13d558ccbf6d814de2.jpg",
"https://ss1.baidu.com/9vo3dSag_xI4khGko9WTAnF6hhy/image/h%3D200/sign=ff0999f6d4160924c325a51be406359b/86d6277f9e2f070861ccd4a0ed24b899a801f241.jpg"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = (AutoScrollViewPager) findViewById(R.id.viewPager);
mViewPager.setAdapter(new BaseViewPagerAdapter<String>(this,initData(),listener) {
@Override
public void loadImage(ImageView view, int position, String url) {
Picasso.with(MainActivity.this).load(url).into(view);
}
});
}
private List<String> initData() {
List<String> data = new ArrayList<>();
Picture picture ;
for (int i = 0 ; i < paths.length ;i++){
data.add(paths[i]);
}
return data;
}
@Override
protected void onDestroy() {
super.onDestroy();
mViewPager.onDestroy();
}
private BaseViewPagerAdapter.OnAutoViewPagerItemClickListener listener = new BaseViewPagerAdapter.OnAutoViewPagerItemClickListener<Picture>() {
@Override
public void loadImage(ImageView view, int position, String url) {
Picasso.with(MainActivity.this).load(url).into(view);
public void onItemClick(int position, Picture picture) {
Toast.makeText(getApplicationContext(),
picture.getName(), Toast.LENGTH_SHORT).show();
}
});
};
}
private List<String> initData() {
List<String> data = new ArrayList<>();
Picture picture ;
for (int i = 0 ; i < paths.length ;i++){
data.add(paths[i]);
}
return data;
}
@Override
protected void onDestroy() {
super.onDestroy();
mViewPager.onDestroy();
}
```
Override```public void loadImage(ImageView view, int position, String url)``` to load image to ImageView.
* OnClick
private BaseViewPagerAdapter.OnAutoViewPagerItemClickListener listener = new BaseViewPagerAdapter.OnAutoViewPagerItemClickListener<Picture>() {
```
private BaseViewPagerAdapter.OnAutoViewPagerItemClickListener listener = new BaseViewPagerAdapter.OnAutoViewPagerItemClickListener<String>() {
@Override
public void onItemClick(int position, Picture picture) {
public void onItemClick(int position, String url) {
Toast.makeText(getApplicationContext(),
picture.getName(), Toast.LENGTH_SHORT).show();
url, Toast.LENGTH_SHORT).show();
}
};
}
```

```
Override```public void loadImage(ImageView view, int position, String url)``` to load image to ImageView.


* OnClick

```
private BaseViewPagerAdapter.OnAutoViewPagerItemClickListener listener = new BaseViewPagerAdapter.OnAutoViewPagerItemClickListener<String>() {
@Override
public void onItemClick(int position, String url) {
Toast.makeText(getApplicationContext(),
url, Toast.LENGTH_SHORT).show();
}
};
```

init OnClickListener

```
public BaseViewPagerAdapter(Context context, List<T> data,OnAutoViewPagerItemClickListener listener)
```
init OnClickListener
```
public BaseViewPagerAdapter(Context context, List<T> data,OnAutoViewPagerItemClickListener listener)
```
sample:[AutoViewPager](https://github.com/fccaikai/AutoScrollViewPager/tree/master/app)
1 change: 1 addition & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
package com.kcode.autoscrollviewpager.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.kcode.autoscrollviewpager.R;

import static android.view.Gravity.CENTER;

/**
* Created by caik on 2016/10/17.
*/

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";

private final static int RIGHT_INT = 0;
private final static int CENTER_INT = 1;
private final static int LEFT_INT = 2;

private int pointLayout;

private AutoViewPager mViewPager;

private Context mContext;
Expand All @@ -29,8 +39,33 @@ public AutoScrollViewPager(Context context) {
}

public AutoScrollViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
this(context,attrs,0);
}

public AutoScrollViewPager(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);

TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.AutoScrollViewPager, defStyleAttr, 0);

String pointLayoutStr = typedArray.getString(R.styleable.AutoScrollViewPager_point_layout);
switch (pointLayoutStr) {
case LEFT_POINT:
pointLayout = LEFT_INT;
break;
case RIGHT_POINT:
pointLayout = RIGHT_INT;
break;
case CENTER_POINT:
pointLayout = CENTER_INT;
break;
default:
break;
}

typedArray.recycle();

init(context);

}

private void init(Context context) {
Expand All @@ -57,7 +92,7 @@ public void initPointView(int size) {
ImageView imageView = new ImageView(mContext);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(20, 20);
params.leftMargin = 8;
params.gravity = CENTER;
params.gravity = Gravity.CENTER;
imageView.setLayoutParams(params);
if (i == 0) {
imageView.setBackgroundResource(R.drawable.point_checked);
Expand All @@ -70,7 +105,22 @@ public void initPointView(int size) {

LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(ALIGN_PARENT_BOTTOM);
layoutParams.addRule(ALIGN_PARENT_RIGHT);
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
10 changes: 10 additions & 0 deletions autoScrollViewPager/src/main/res/values/attr.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<declare-styleable name="AutoScrollViewPager">

<attr name="point_layout" format="string"></attr>

</declare-styleable>

</resources>

0 comments on commit 494ff04

Please sign in to comment.