Skip to content

Commit

Permalink
1.抽离基类BaseViewPagerAdapter,图片加载也抽象成一个方法。具体的加载需要实现一个BaseViewPagerAdapter
Browse files Browse the repository at this point in the history
2.传入的参数设置为泛型,可以直接传URL地址,也可以传对象集合
  • Loading branch information
caikaidev committed Oct 19, 2016
1 parent a6fcb56 commit 9b8a6a3
Show file tree
Hide file tree
Showing 12 changed files with 298 additions and 198 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
###AutoViewPager
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ dependencies {
compile 'com.android.support:appcompat-v7:24.2.0'
testCompile 'junit:junit:4.12'
compile project(':autoScrollViewPager')
compile 'com.squareup.picasso:picasso:2.3.2'
}
46 changes: 37 additions & 9 deletions app/src/main/java/com/kcode/autoviewpager/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,63 @@

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.kcode.autoscrollviewpager.view.AutoScrollViewPager;
import com.kcode.autoscrollviewpager.view.AutoViewPager;
import com.kcode.autoscrollviewpager.view.ViewPagerAdapter;
import com.kcode.autoscrollviewpager.view.BaseViewPagerAdapter;
import com.kcode.autoviewpager.bean.Picture;

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

public class MainActivity extends AppCompatActivity {

private AutoViewPager mViewPager;
private ViewPagerAdapter mAdapter;
private PictureViewPagerAdapter mAdapter;

private AutoScrollViewPager autoScrollViewPager;

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

autoScrollViewPager = (AutoScrollViewPager) findViewById(R.id.viewPager);
mViewPager = autoScrollViewPager.getViewPager();
mAdapter = new ViewPagerAdapter(getApplicationContext(),mViewPager);
mAdapter = new PictureViewPagerAdapter(getApplicationContext(),initData(),mViewPager, listener);

mAdapter.add("https://ss3.baidu.com/-fo3dSag_xI4khGko9WTAnF6hhy/image/h%3D200/sign=c493b482b47eca800d053ee7a1229712/8cb1cb1349540923abd671df9658d109b2de49d7.jpg");
mAdapter.add("https://ss0.baidu.com/94o3dSag_xI4khGko9WTAnF6hhy/image/h%3D200/sign=45fbfa5555da81cb51e684cd6267d0a4/2f738bd4b31c8701491ea047237f9e2f0608ffe3.jpg");
mAdapter.add("https://ss2.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/image/h%3D200/sign=ae0e95c0fc1986185e47e8847aec2e69/0b46f21fbe096b63eb314ef108338744ebf8ac62.jpg");
}

mViewPager.start();
mAdapter.add("https://ss3.baidu.com/9fo3dSag_xI4khGko9WTAnF6hhy/image/h%3D200/sign=1fad2b46952397ddc9799f046983b216/dc54564e9258d109c94bbb13d558ccbf6d814de2.jpg");
mAdapter.add("https://ss1.baidu.com/9vo3dSag_xI4khGko9WTAnF6hhy/image/h%3D200/sign=ff0999f6d4160924c325a51be406359b/86d6277f9e2f070861ccd4a0ed24b899a801f241.jpg");
private List<Picture> initData() {
List<Picture> data = new ArrayList<>();
Picture picture ;
for (int i = 0 ; i < paths.length ;i++){
picture = new Picture(paths[i],"图片" + i);
data.add(picture);
}
return data;
}

@Override
protected void onDestroy() {
super.onDestroy();
mViewPager.onDestroy();
}

private BaseViewPagerAdapter.OnAutoViewPagerItemClickListener listener = new BaseViewPagerAdapter.OnAutoViewPagerItemClickListener<Picture>() {

@Override
public void onItemClick(int position, Picture picture) {
Toast.makeText(getApplicationContext(),

picture.getName(), Toast.LENGTH_SHORT).show();
}
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.kcode.autoviewpager;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;

import com.kcode.autoscrollviewpager.view.AutoViewPager;
import com.kcode.autoscrollviewpager.view.BaseViewPagerAdapter;
import com.kcode.autoviewpager.bean.Picture;
import com.squareup.picasso.Picasso;

import java.util.List;

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

public class PictureViewPagerAdapter extends BaseViewPagerAdapter<Picture> implements ViewPager.OnPageChangeListener{


private Context mContext;

public PictureViewPagerAdapter(Context context, AutoViewPager viewPager) {
super(context,viewPager);
mContext = context;
}

public PictureViewPagerAdapter(Context context, AutoViewPager viewPager, OnAutoViewPagerItemClickListener listener) {
super(context, viewPager, listener);
this.mContext = context;
}

@Override
public void loadImage(ImageView view, int position, Picture picture) {
Picasso.with(mContext).load(picture.getPath()).into(view);
}

public PictureViewPagerAdapter(Context context, List<Picture> data, AutoViewPager viewPager, OnAutoViewPagerItemClickListener listener) {
super(context, data, viewPager, listener);
this.mContext = context;
}
}
41 changes: 41 additions & 0 deletions app/src/main/java/com/kcode/autoviewpager/ViewPagerAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.kcode.autoviewpager;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;

import com.kcode.autoscrollviewpager.view.AutoViewPager;
import com.kcode.autoscrollviewpager.view.BaseViewPagerAdapter;
import com.squareup.picasso.Picasso;

import java.util.List;

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

public class ViewPagerAdapter extends BaseViewPagerAdapter<String> implements ViewPager.OnPageChangeListener{


private Context mContext;

public ViewPagerAdapter(Context context, AutoViewPager viewPager) {
super(context,viewPager);
mContext = context;
}

public ViewPagerAdapter(Context context, AutoViewPager viewPager, OnAutoViewPagerItemClickListener listener) {
super(context, viewPager, listener);
this.mContext = context;
}

public ViewPagerAdapter(Context context, List<String> data, AutoViewPager viewPager, OnAutoViewPagerItemClickListener listener) {
super(context, data, viewPager, listener);
this.mContext = context;
}

@Override
public void loadImage(ImageView view, int position, String url) {
Picasso.with(mContext).load(url).into(view);
}
}
31 changes: 31 additions & 0 deletions app/src/main/java/com/kcode/autoviewpager/bean/Picture.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.kcode.autoviewpager.bean;

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

public class Picture {
private String path;
private String name;

public Picture(String path, String name) {
this.path = path;
this.name = name;
}

public String getPath() {
return path;
}

public void setPath(String path) {
this.path = path;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
88 changes: 0 additions & 88 deletions autoScrollViewPager/build.gradle
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
apply plugin: 'com.android.library'
//配置插件方法1
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'
//提交到仓库中的版本号
version = "0.1.0"

android {
compileSdkVersion 24
Expand Down Expand Up @@ -32,87 +27,4 @@ dependencies {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.0'
compile 'com.squareup.picasso:picasso:2.3.2'
}

//方法1
def siteUrl = 'https://github.com/fccaikai/AutoScrollViewPager' // 项目的主页 这个是说明,可随便填
def gitUrl = 'https://github.com/fccaikai/AutoScrollViewPager.git' // Git仓库的url 这个是说明,可随便填
group = "com.kcode.github" // 这里是groupId ,必须填写 一般填你唯一的包名

install {
repositories.mavenInstaller {
// This generates POM.xml with proper parameters
pom {
project {
packaging 'aar'
// Add your description here
name 'Android auto scroll viewpager' //项目描述
url siteUrl
// Set your license
licenses {
license {
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
id 'caikai' //填写开发者的一些基本信息
name 'caikai' //填写开发者的一些基本信息
email '18942334641@163.com' //填写开发者的一些基本信息
}
}
scm {
connection gitUrl
developerConnection gitUrl
url siteUrl
}
}
}
}
}

task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}
task javadoc(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives javadocJar
archives sourcesJar
}

Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
bintray {
user = properties.getProperty("bintray.user") //读取 local.properties 文件里面的 bintray.user
key = properties.getProperty("bintray.apikey") //读取 local.properties 文件里面的 bintray.apikey
configurations = ['archives']
pkg {
repo = "maven"
name = "AutoViewPager" //发布到JCenter上的项目名字,必须填写
websiteUrl = siteUrl
vcsUrl = gitUrl
licenses = ["Apache-2.0"]
publish = true
}
}

//这段代码一会给你们解释哈
javadoc {
options{
encoding "UTF-8"
charSet 'UTF-8'
author true
version true
links "http://docs.oracle.com/javase/7/docs/api"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ public class AutoScrollViewPager extends RelativeLayout{

public AutoScrollViewPager(Context context) {
super(context);
mContext = context;
mViewPager = new AutoViewPager(context);
addView(mViewPager);
init(context);
}

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

private void init(Context context){
mContext = context;
mViewPager = new AutoViewPager(context);
layout = new LinearLayout(mContext);
Expand All @@ -48,7 +50,7 @@ public void initPointView(int size){
for (int i = 0; i < size; i++) {
ImageView imageView = new ImageView(mContext);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(20,20);
params.leftMargin = 20;
params.leftMargin = 8;
params.gravity = CENTER;
imageView.setLayoutParams(params);
if (i == 0) {
Expand All @@ -63,7 +65,7 @@ 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);
layoutParams.setMargins(10,20,10,20);
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 @@ -59,8 +59,12 @@ public void run() {
private AutoHandler mHandler = new AutoHandler();

public void updatePointView(int size) {
AutoScrollViewPager pager = (AutoScrollViewPager) getParent();
pager.initPointView(size);
if (getParent() instanceof AutoScrollViewPager){
AutoScrollViewPager pager = (AutoScrollViewPager) getParent();
pager.initPointView(size);
}else {
Log.e(TAG,"parent view not be AutoScrollViewPager");
}
}

public void onPageSelected(int position) {
Expand Down Expand Up @@ -92,6 +96,10 @@ public void onStop(){
}
}

public void onDestroy(){
onStop();
}

public void onResume(){
start();
}
Expand Down
Loading

0 comments on commit 9b8a6a3

Please sign in to comment.