团队——和谐共生(3)
作者:互联网
和谐共生(3)
今天实现了Android轮播图效果,将会把它插入到整体的项目中
实现效果是这样的
这个效果是根据网上资料来的,边学边做。
核心代码
package com.example.loopviewdemo;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ViewPager viewPager; //轮播图模块
private int[] mImg;
private int[] mImg_id;
private String[] mDec;
private ArrayList<ImageView> mImgList;
private LinearLayout ll_dots_container;
private TextView loop_dec;
private int previousSelectedPosition = 0;
boolean isRunning = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initLoopView(); //实现轮播图
}
private void initLoopView() {
viewPager = (ViewPager)findViewById(R.id.loopviewpager);
ll_dots_container = (LinearLayout)findViewById(R.id.ll_dots_loop);
loop_dec = (TextView)findViewById(R.id.loop_dec);
// 图片资源id数组
mImg = new int[]{
R.drawable.test3,
R.drawable.test3,
R.drawable.test3,
R.drawable.test3,
R.drawable.test3
};
// 文本描述
mDec = new String[]{
"Test1",
"Test2",
"Test3",
"Test4",
"Test5"
};
mImg_id = new int[]{
R.id.pager_img1,
R.id.pager_img2,
R.id.pager_img3,
R.id.pager_img4,
R.id.pager_img5
};
// 初始化要展示的5个ImageView
mImgList = new ArrayList<ImageView>();
ImageView imageView;
View dotView;
LinearLayout.LayoutParams layoutParams;
for(int i=0;i<mImg.length;i++){
//初始化要显示的图片对象
imageView = new ImageView(this);
imageView.setBackgroundResource(mImg[i]);
imageView.setId(mImg_id[i]);
imageView.setOnClickListener(new pagerOnClickListener(getApplicationContext()));
mImgList.add(imageView);
//加引导点
dotView = new View(this);
dotView.setBackgroundResource(R.drawable.dot);
layoutParams = new LinearLayout.LayoutParams(10,10);
if(i!=0){
layoutParams.leftMargin=10;
}
//设置默认所有都不可用
dotView.setEnabled(false);
ll_dots_container.addView(dotView,layoutParams);
}
ll_dots_container.getChildAt(0).setEnabled(true);
loop_dec.setText(mDec[0]);
previousSelectedPosition=0;
//设置适配器
viewPager.setAdapter(new LoopViewAdapter(mImgList));
// 把ViewPager设置为默认选中Integer.MAX_VALUE / t2,从十几亿次开始轮播图片,达到无限循环目的;
int m = (Integer.MAX_VALUE / 2) %mImgList.size();
int currentPosition = Integer.MAX_VALUE / 2 - m;
viewPager.setCurrentItem(currentPosition);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
int newPosition = i % mImgList.size();
loop_dec.setText(mDec[newPosition]);
ll_dots_container.getChildAt(previousSelectedPosition).setEnabled(false);
ll_dots_container.getChildAt(newPosition).setEnabled(true);
previousSelectedPosition = newPosition;
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
// 开启轮询
new Thread(){
public void run(){
isRunning = true;
while(isRunning){
try{
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//下一条
runOnUiThread(new Runnable() {
@Override
public void run() {
viewPager.setCurrentItem(viewPager.getCurrentItem()+1);
}
});
}
}
}.start();
}
}
import android.support.annotation.NonNull;import android.support.v4.view.PagerAdapter;import android.view.View;import android.view.ViewGroup;import android.widget.ImageView;
import java.util.ArrayList;
class LoopViewAdapter extends PagerAdapter{
private ArrayList<ImageView> imageViewList;
public LoopViewAdapter(ArrayList<ImageView> mImgList){
imageViewList = mImgList;
}
// 1. 返回要显示的条目内容, 创建条目 @NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
// container: 容器: ViewPager
// position: 当前要显示条目的位置 0 -> 4
//newPosition = position % 5
int newPosition = position % imageViewList.size();
ImageView img = imageViewList.get(newPosition);
// a. 把View对象添加到container中 container.addView(img);
// b. 把View对象返回给框架, 适配器
return img;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View)object);
}
@Override
public int getCount() {
return Integer.MAX_VALUE; //返回一个无限大的值,可以 无限循环!!!!! }
/**
* 判断是否使用缓存, 如果返回的是true, 使用缓存. 不去调用instantiateItem方法创建一个新的对象
*/
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
return view == o ;
}
}
标签:container,int,共生,和谐,new,import,android,团队,id 来源: https://www.cnblogs.com/jztuandui/p/16376305.html