依赖:
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
compile 'com.facebook.fresco:fresco:1.5.0'
package hc.com.day12rikao;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ViewPager mImgViewPager;
private List<Integer> list ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mImgViewPager = (ViewPager) findViewById(R.id.img_viewPager);
//得到图片
list = new ArrayList<>();
list.add(R.mipmap.ic_launcher);
list.add(R.mipmap.ic_launcher);
list.add(R.mipmap.ic_launcher);
MyAdapter myAdapter = new MyAdapter(MainActivity.this, list);
mImgViewPager.setAdapter(myAdapter);
}
}
MyAdapter
package hc.com.day12rikao;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import java.util.List;
/**
* Created by kang on 2018/1/9.
*/
public class MyAdapter extends PagerAdapter {
private Context context;
private List<Integer> lsit;
public MyAdapter(Context context, List<Integer> lsit) {
this.context = context;
this.lsit = lsit;
}
@Override
public int getCount() {
return lsit.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
imageView.setImageResource(lsit.get(position));
container.addView(imageView);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
package hc.com.day12rikao;
import android.app.Application;
APP
import com.facebook.drawee.backends.pipeline.Fresco;
/**
* Created by kang on 2018/1/9.
*/
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
Fresco.initialize(this);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="hc.com.day12rikao.MainActivity">
<android.support.v4.view.ViewPager
android:id="@+id/img_viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>