viewpager是用在滚动中相对比较多的,最近做一个定时滚动显示viewpager内容。

步骤:

一:在布局文件添加v4包下的viewpager:

android:id="@+id/main_viewpager"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_gravity="center">

二:获取后台数据,现在就暂时自己造几条数据,并写adapter内容

//构造list集合

list=newArrayList();

list.add(R.mipmap.image1);

list.add(R.mipmap.image2);

list.add(R.mipmap.image3);

//我这边是显示的viewpager的很多信息,包括图片文字,所以构造view,在Java中写布局代码,

这个时候就需要我们构造view,privateListmList=new ArryList;

循化list集合数据,创建页面详情,通过add方法加载到mList,这样adapter中就可以传递过去值了:

for(inti =0;i

rootlayout=newRelativeLayout(this);

rootlayout.setLayoutParams(newRelativeLayout.LayoutParams(

RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT));

rootlayout.setId(0);

ImageView imageView =newImageView(this);

RelativeLayout.LayoutParams image_Params =newRelativeLayout.LayoutParams(

dip2px(250),dip2px(192));

image_Params.setMargins(dip2px(5),0,dip2px(5),dip2px(5));

imageView.setLayoutParams(image_Params);

imageView.setImageResource(list.get(i));

//Picasso.with(ViewPagersActivity.this).load(tsfbBeans.get(i).getBook_pic()).error(R.mipmap.image1).into(imageView);

imageView.setId(1);

//字体颜色

Resources resource = (Resources) getBaseContext().getResources();

ColorStateList csl = (ColorStateList) resource.getColorStateList(R.color.book_text);

TextView text_Name =newTextView(this);

RelativeLayout.LayoutParams text_Name_Params =newRelativeLayout.LayoutParams(

RelativeLayout.LayoutParams.WRAP_CONTENT,

RelativeLayout.LayoutParams.WRAP_CONTENT);

text_Name_Params.addRule(RelativeLayout.RIGHT_OF,imageView.getId());

text_Name_Params.addRule(RelativeLayout.ALIGN_TOP,imageView.getId());

text_Name_Params.setMargins(dip2px(10),dip2px(5),0,0);

text_Name.setLayoutParams(text_Name_Params);

text_Name.setText("图书描述:");

//text_Name.setText(tsfbBeans.get(i).getBook_name() + ":");

text_Name.setTextSize(TypedValue.COMPLEX_UNIT_SP,15);

text_Name.setTextColor(csl);

text_Name.setId(2);

TextView text_Email =newTextView(this);

RelativeLayout.LayoutParams text_Email_Params =newRelativeLayout.LayoutParams(

RelativeLayout.LayoutParams.WRAP_CONTENT,

RelativeLayout.LayoutParams.WRAP_CONTENT);

text_Email_Params.addRule(RelativeLayout.RIGHT_OF,imageView.getId());

text_Email_Params.addRule(RelativeLayout.BELOW,text_Name.getId());

text_Email_Params.setMargins(dip2px(10),dip2px(5),dip2px(10),0);

text_Email.setLayoutParams(text_Email_Params);

text_Email.setText("《诗经》是中国古代诗歌开端,最早的一部诗歌总集,收集了西周初年至春秋中叶(前11世纪至前6世纪)的诗歌,共311篇,其中6篇为笙诗,即只有标题,没有内容,称为笙诗六篇");

//text_Email.setText(tsfbBeans.get(i).getBook_description());

text_Email.setTextSize(TypedValue.COMPLEX_UNIT_SP,13);

text_Email.setId(3);

TextView text_author =newTextView(this);

RelativeLayout.LayoutParams text_author_Params =newRelativeLayout.LayoutParams(

RelativeLayout.LayoutParams.WRAP_CONTENT,

RelativeLayout.LayoutParams.WRAP_CONTENT);

text_author_Params.addRule(RelativeLayout.RIGHT_OF,imageView.getId());

text_author_Params.addRule(RelativeLayout.BELOW,text_Email.getId());

text_author_Params.setMargins(dip2px(10),dip2px(5),0,0);

text_author.setLayoutParams(text_author_Params);

text_author.setText("图书作者:");

//text_author.setText("图书作者:" + tsfbBeans.get(i).getBook_author());

text_author.setTextColor(csl);

text_author.setTextSize(TypedValue.COMPLEX_UNIT_SP,15);

text_author.setId(4);

TextView text_place =newTextView(this);

RelativeLayout.LayoutParams text_place_Params =newRelativeLayout.LayoutParams(

RelativeLayout.LayoutParams.WRAP_CONTENT,

RelativeLayout.LayoutParams.WRAP_CONTENT);

text_place_Params.addRule(RelativeLayout.RIGHT_OF,imageView.getId());

text_place_Params.addRule(RelativeLayout.BELOW,text_author.getId());

text_place_Params.setMargins(dip2px(10),dip2px(5),0,0);

text_place.setLayoutParams(text_place_Params);

text_place.setText("出版社:");

//text_place.setText("出版社:" + tsfbBeans.get(i).getBook_publishing());

text_place.setTextColor(csl);

text_place.setTextSize(TypedValue.COMPLEX_UNIT_SP,15);

text_place.setId(5);

TextView text_data =newTextView(this);

RelativeLayout.LayoutParams text_data_Params =newRelativeLayout.LayoutParams(

RelativeLayout.LayoutParams.WRAP_CONTENT,

RelativeLayout.LayoutParams.WRAP_CONTENT);

text_data_Params.addRule(RelativeLayout.RIGHT_OF,imageView.getId());

text_data_Params.addRule(RelativeLayout.BELOW,text_place.getId());

text_data_Params.setMargins(dip2px(10),dip2px(5),0,0);

text_data.setLayoutParams(text_data_Params);

text_data.setText("出版日期:");

// text_data.setText("出版日期:" + tsfbBeans.get(i).getBook_publication_date());

text_data.setTextColor(csl);

text_data.setTextSize(TypedValue.COMPLEX_UNIT_SP,15);

text_data.setId(6);

TextView text_description_name =newTextView(this);

RelativeLayout.LayoutParams text_DescriptionName_Params =newRelativeLayout.LayoutParams(

RelativeLayout.LayoutParams.WRAP_CONTENT,

RelativeLayout.LayoutParams.WRAP_CONTENT);

text_DescriptionName_Params.addRule(RelativeLayout.BELOW,imageView.getId());

//text_Name_Params.addRule(RelativeLayout.ALIGN_TOP, imageView.getId());

text_DescriptionName_Params.setMargins(dip2px(10),dip2px(5),0,0);

text_description_name.setLayoutParams(text_DescriptionName_Params);

text_description_name.setText("图书简介:");

text_description_name.setTextColor(csl);

text_description_name.setTextSize(TypedValue.COMPLEX_UNIT_SP,15);

text_description_name.setId(7);

TextView text_description =newTextView(this);

RelativeLayout.LayoutParams text_Description_Params =newRelativeLayout.LayoutParams(

RelativeLayout.LayoutParams.WRAP_CONTENT,

RelativeLayout.LayoutParams.WRAP_CONTENT);

text_Description_Params.addRule(RelativeLayout.BELOW,text_description_name.getId());

//text_Name_Params.addRule(RelativeLayout.ALIGN_TOP, imageView.getId());

text_Description_Params.setMargins(dip2px(10),dip2px(5),0,0);

text_description.setLayoutParams(text_Description_Params);

text_description.setText("111");

//text_description.setText(tsfbBeans.get(i).getBook_abstract());

text_description.setTextSize(TypedValue.COMPLEX_UNIT_SP,13);

text_description.setId(8);

rootlayout.addView(imageView,image_Params);

rootlayout.addView(text_Name,text_Name_Params);

rootlayout.addView(text_Email,text_Email_Params);

rootlayout.addView(text_description,text_Description_Params);

rootlayout.addView(text_author,text_author_Params);

rootlayout.addView(text_place,text_place_Params);

rootlayout.addView(text_data,text_data_Params);

rootlayout.addView(text_description_name,text_DescriptionName_Params);

//setContentView(rootlayout);

mList.add(rootlayout);

}

来讲讲adapter,基本写法就行,但是注意的是在添加view的时候必须保证是一个view的parent,要不然报错:

public classTTMyPagerAdaptersextendsPagerAdapter {

publicListlistViews=null;

publicTTestViewPagersActivitymContext;

publicTTMyPagerAdapters(List listViews,TTestViewPagersActivity mContext) {

this.listViews= listViews;

this.mContext= mContext;

Share.d("listviews"+ listViews.size());

}

@Override

public intgetCount() {

returnInteger.MAX_VALUE;

}

@Override

public booleanisViewFromObject(View arg0,Object arg1) {

returnarg0 == arg1;

}

@Override

public voiddestroyItem(View container, intposition,Object object) {

Share.d("TipsPagerAdapter,destroyItem is called  "+ position);

((ViewPager) container).removeView(listViews.get(position %listViews.size()));

}

@Override

publicCharSequencegetPageTitle(intposition) {

return super.getPageTitle(position);

}

@Override

publicObjectinstantiateItem(View container, intposition) {

Share.d("TipsPagerAdapter, instantiateItem is called "+ position);

try{

((ViewPager) container).addView(

listViews.get(position %listViews.size()),0);

}catch(Exception e) {

}

// ((ViewPager) container).addView(listViews.get(position);

// return listViews.get(position);

Share.d("instantiateItem>>>>>>"+ position %listViews.size());

returnlistViews.get(position %listViews.size());

}

}

好了现在就是直接setadapter就行了,然后写个线程循环跳

adapter=newTTMyPagerAdapters(mList, this);

mPager.setAdapter(adapter);

mPager.setCurrentItem(position);

position是初始滑动值,净量设置越大越好,一般滑不到头

private voidinitAdapter() {

Share.d("mlist"+mList.size());

//

//TODO Auto-generated method stub

//TODO Auto-generated catch block

change=newThread(newRunnable() {

@Override

public voidrun() {

//TODO Auto-generated method stub

while(true) {

handler.sendEmptyMessage(0);

try{

Thread.sleep(5000);

}catch(InterruptedException e) {

//TODO Auto-generated catch block

e.printStackTrace();

}

}

}

});

change.start();

}

@SuppressLint("HandlerLeak")

privateHandlerhandler=newHandler() {

@Override

public voidhandleMessage(Message msg) {

super.handleMessage(msg);

switch(msg.what) {

case0:

position++;

mPager.setCurrentItem(position);

break;

default:

break;

}

}

};

好了现在可没有结束,你会发现list的集合数小于3的时候又空白页出现,但是一张的时候你又不想让他滚动,怎么办?主要是数据太短销毁的时候有bug,要是list多了那就无所谓了,所以怎么办呢?其中一个办法,可以把小于3的list通过一个for循环构造出大于他不就OK了,所以那就开干,循环添加吧!总结这个问题就是数据一条的时候,和2/3条的时候和多于3条的时候情况不一样,分别处理,我的做法是先在判断集合以2为界限判断,实验多次就是2的时候容易空白,所以区分是2和不是的时候,是2的话让他for循环添加两次进去数据变成4就好了,不是2的正常执行,但是不是2的里边包括1个的时候,那么就在setadapter的时候处理,区分新的list是1还是大于3的,这样问题就解决了,下边附上全部代码!

packagecom.example.administrator.student.ui;

importandroid.annotation.SuppressLint;

importandroid.app.Activity;

importandroid.content.Intent;

importandroid.content.res.ColorStateList;

importandroid.content.res.Resources;

importandroid.os.Bundle;

importandroid.os.Handler;

importandroid.os.Message;

importandroid.support.v4.view.ViewPager;

importandroid.util.TypedValue;

importandroid.view.MotionEvent;

importandroid.view.View;

importandroid.widget.ImageView;

importandroid.widget.RelativeLayout;

importandroid.widget.TextView;

importcom.example.administrator.student.R;

importcom.example.administrator.student.adapter.TMyPagerAdapters;

importcom.example.administrator.student.adapter.TTMyPagerAdapters;

importcom.example.administrator.student.bean.Main_Data;

importcom.example.administrator.student.utils.Share;

importjava.util.ArrayList;

importjava.util.List;

/**

* Created by Administrator on 2017/10/16.

*/

public classTTestViewPagersActivityextendsActivity {

privateViewPagermPager;

privateListmList;

privateListtsfbBeans=newArrayList();

privateTTMyPagerAdaptersadapter;

privateListlist;

private intposition=50000;

privateThreadchange;

privateRelativeLayoutrootlayout;

@Override

public voidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_viewpager);

mPager= (ViewPager) findViewById(R.id.main_viewpager);

//返回

ImageView back = (ImageView) findViewById(R.id.back);

back.setOnClickListener(newView.OnClickListener() {

@Override

public voidonClick(View view) {

finish();

}

});

Intent intent = getIntent();

String message = intent.getStringExtra("data");

//解析数据

//        if (message.length() > 0) {

//            Gson gson = new Gson();

//            Main_Data main_data = gson.fromJson(message, new TypeToken() {

//            }.getType());

//            tsfbBeans = main_data.getTsfb();

//        }

//构造list集合

list=newArrayList();

list.add(R.mipmap.image1);

list.add(R.mipmap.image2);

list.add(R.mipmap.image3);

//        list.add(R.mipmap.image4);

mList=newArrayList();

initView();

}

private voidinitView() {

if(list.size() ==2) {

//如果数据是2,就添加两个数组以避免空白页

for(intm =0;m <2;m++) {

initValue();

}

}else{

//如果是一个数据那就添加一个

initValue();

}

adapter=newTTMyPagerAdapters(mList, this);

mPager.setAdapter(adapter);

if(mList.size() >1) {

mPager.setCurrentItem(position);

initAdapter();

}

//        if (mList.size() < 2) {

//

//        } else if (mList.size() == 2) {

//

//            mPager.setCurrentItem(position + 1);

//            initAdapter();

//        } else {

//            mPager.setCurrentItem(position);

//            initAdapter();

//        }

//mPager.setCurrentItem(count);

mPager.setOnPageChangeListener(listener);

mPager.setOnTouchListener(newView.OnTouchListener() {

@Override

public booleanonTouch(View view,MotionEvent motionEvent) {

return true;

}

});

}

private voidinitValue() {

for(inti =0;i

rootlayout=newRelativeLayout(this);

rootlayout.setLayoutParams(newRelativeLayout.LayoutParams(

RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT));

rootlayout.setId(0);

ImageView imageView =newImageView(this);

RelativeLayout.LayoutParams image_Params =newRelativeLayout.LayoutParams(

dip2px(250),dip2px(192));

image_Params.setMargins(dip2px(5),0,dip2px(5),dip2px(5));

imageView.setLayoutParams(image_Params);

imageView.setImageResource(list.get(i));

//Picasso.with(ViewPagersActivity.this).load(tsfbBeans.get(i).getBook_pic()).error(R.mipmap.image1).into(imageView);

imageView.setId(1);

//字体颜色

Resources resource = (Resources) getBaseContext().getResources();

ColorStateList csl = (ColorStateList) resource.getColorStateList(R.color.book_text);

TextView text_Name =newTextView(this);

RelativeLayout.LayoutParams text_Name_Params =newRelativeLayout.LayoutParams(

RelativeLayout.LayoutParams.WRAP_CONTENT,

RelativeLayout.LayoutParams.WRAP_CONTENT);

text_Name_Params.addRule(RelativeLayout.RIGHT_OF,imageView.getId());

text_Name_Params.addRule(RelativeLayout.ALIGN_TOP,imageView.getId());

text_Name_Params.setMargins(dip2px(10),dip2px(5),0,0);

text_Name.setLayoutParams(text_Name_Params);

text_Name.setText("图书描述:");

//text_Name.setText(tsfbBeans.get(i).getBook_name() + ":");

text_Name.setTextSize(TypedValue.COMPLEX_UNIT_SP,15);

text_Name.setTextColor(csl);

text_Name.setId(2);

TextView text_Email =newTextView(this);

RelativeLayout.LayoutParams text_Email_Params =newRelativeLayout.LayoutParams(

RelativeLayout.LayoutParams.WRAP_CONTENT,

RelativeLayout.LayoutParams.WRAP_CONTENT);

text_Email_Params.addRule(RelativeLayout.RIGHT_OF,imageView.getId());

text_Email_Params.addRule(RelativeLayout.BELOW,text_Name.getId());

text_Email_Params.setMargins(dip2px(10),dip2px(5),dip2px(10),0);

text_Email.setLayoutParams(text_Email_Params);

text_Email.setText("《诗经》是中国古代诗歌开端,最早的一部诗歌总集,收集了西周初年至春秋中叶(前11世纪至前6世纪)的诗歌,共311篇,其中6篇为笙诗,即只有标题,没有内容,称为笙诗六篇");

//text_Email.setText(tsfbBeans.get(i).getBook_description());

text_Email.setTextSize(TypedValue.COMPLEX_UNIT_SP,13);

text_Email.setId(3);

TextView text_author =newTextView(this);

RelativeLayout.LayoutParams text_author_Params =newRelativeLayout.LayoutParams(

RelativeLayout.LayoutParams.WRAP_CONTENT,

RelativeLayout.LayoutParams.WRAP_CONTENT);

text_author_Params.addRule(RelativeLayout.RIGHT_OF,imageView.getId());

text_author_Params.addRule(RelativeLayout.BELOW,text_Email.getId());

text_author_Params.setMargins(dip2px(10),dip2px(5),0,0);

text_author.setLayoutParams(text_author_Params);

text_author.setText("图书作者:");

//text_author.setText("图书作者:" + tsfbBeans.get(i).getBook_author());

text_author.setTextColor(csl);

text_author.setTextSize(TypedValue.COMPLEX_UNIT_SP,15);

text_author.setId(4);

TextView text_place =newTextView(this);

RelativeLayout.LayoutParams text_place_Params =newRelativeLayout.LayoutParams(

RelativeLayout.LayoutParams.WRAP_CONTENT,

RelativeLayout.LayoutParams.WRAP_CONTENT);

text_place_Params.addRule(RelativeLayout.RIGHT_OF,imageView.getId());

text_place_Params.addRule(RelativeLayout.BELOW,text_author.getId());

text_place_Params.setMargins(dip2px(10),dip2px(5),0,0);

text_place.setLayoutParams(text_place_Params);

text_place.setText("出版社:");

//text_place.setText("出版社:" + tsfbBeans.get(i).getBook_publishing());

text_place.setTextColor(csl);

text_place.setTextSize(TypedValue.COMPLEX_UNIT_SP,15);

text_place.setId(5);

TextView text_data =newTextView(this);

RelativeLayout.LayoutParams text_data_Params =newRelativeLayout.LayoutParams(

RelativeLayout.LayoutParams.WRAP_CONTENT,

RelativeLayout.LayoutParams.WRAP_CONTENT);

text_data_Params.addRule(RelativeLayout.RIGHT_OF,imageView.getId());

text_data_Params.addRule(RelativeLayout.BELOW,text_place.getId());

text_data_Params.setMargins(dip2px(10),dip2px(5),0,0);

text_data.setLayoutParams(text_data_Params);

text_data.setText("出版日期:");

// text_data.setText("出版日期:" + tsfbBeans.get(i).getBook_publication_date());

text_data.setTextColor(csl);

text_data.setTextSize(TypedValue.COMPLEX_UNIT_SP,15);

text_data.setId(6);

TextView text_description_name =newTextView(this);

RelativeLayout.LayoutParams text_DescriptionName_Params =newRelativeLayout.LayoutParams(

RelativeLayout.LayoutParams.WRAP_CONTENT,

RelativeLayout.LayoutParams.WRAP_CONTENT);

text_DescriptionName_Params.addRule(RelativeLayout.BELOW,imageView.getId());

//text_Name_Params.addRule(RelativeLayout.ALIGN_TOP, imageView.getId());

text_DescriptionName_Params.setMargins(dip2px(10),dip2px(5),0,0);

text_description_name.setLayoutParams(text_DescriptionName_Params);

text_description_name.setText("图书简介:");

text_description_name.setTextColor(csl);

text_description_name.setTextSize(TypedValue.COMPLEX_UNIT_SP,15);

text_description_name.setId(7);

TextView text_description =newTextView(this);

RelativeLayout.LayoutParams text_Description_Params =newRelativeLayout.LayoutParams(

RelativeLayout.LayoutParams.WRAP_CONTENT,

RelativeLayout.LayoutParams.WRAP_CONTENT);

text_Description_Params.addRule(RelativeLayout.BELOW,text_description_name.getId());

//text_Name_Params.addRule(RelativeLayout.ALIGN_TOP, imageView.getId());

text_Description_Params.setMargins(dip2px(10),dip2px(5),0,0);

text_description.setLayoutParams(text_Description_Params);

text_description.setText("111");

//text_description.setText(tsfbBeans.get(i).getBook_abstract());

text_description.setTextSize(TypedValue.COMPLEX_UNIT_SP,13);

text_description.setId(8);

rootlayout.addView(imageView,image_Params);

rootlayout.addView(text_Name,text_Name_Params);

rootlayout.addView(text_Email,text_Email_Params);

rootlayout.addView(text_description,text_Description_Params);

rootlayout.addView(text_author,text_author_Params);

rootlayout.addView(text_place,text_place_Params);

rootlayout.addView(text_data,text_data_Params);

rootlayout.addView(text_description_name,text_DescriptionName_Params);

//setContentView(rootlayout);

mList.add(rootlayout);

}

}

ViewPager.OnPageChangeListenerlistener=newViewPager.OnPageChangeListener() {

@Override

public voidonPageSelected(intarg0) {

//TODO Auto-generated method stub

//LogUtil.e("当前页面索引:" + arg0);

}

@Override

public voidonPageScrolled(intarg0, floatarg1, intarg2) {

//TODO Auto-generated method stub

}

@Override

public voidonPageScrollStateChanged(intarg0) {

//TODO Auto-generated method stub

}

};

private voidinitAdapter() {

Share.d("mlist"+mList.size());

//

//TODO Auto-generated method stub

//TODO Auto-generated catch block

change=newThread(newRunnable() {

@Override

public voidrun() {

//TODO Auto-generated method stub

while(true) {

handler.sendEmptyMessage(0);

try{

Thread.sleep(5000);

}catch(InterruptedException e) {

//TODO Auto-generated catch block

e.printStackTrace();

}

}

}

});

change.start();

}

@SuppressLint("HandlerLeak")

privateHandlerhandler=newHandler() {

@Override

public voidhandleMessage(Message msg) {

super.handleMessage(msg);

switch(msg.what) {

case0:

position++;

mPager.setCurrentItem(position);

break;

default:

break;

}

}

};

public intdip2px(floatdpValue) {

final floatscale =this.getResources().getDisplayMetrics().density;

return(int) (dpValue * scale +0.5f);

}

@Override

protected voidonDestroy() {

super.onDestroy();

}

}

更多相关文章

  1. 使用android访问SQLServer数据库
  2. [Android]Android数据的四种存储方式
  3. Android系统自带的常用数据库
  4. Android数据的四种存储方式
  5. Android旋转屏幕不销毁数据的方法
  6. Android使用SQLiteDatabase直接存取数据与图像
  7. android 数据库操作 GreenDAO 第三方开源项目
  8. android 使用Okhttp封装上传JSON格式数据的工具类
  9. Android 数据库 短信 监听

随机推荐

  1. android - 为安全而设计 - 2 - 开发文档
  2. Eclipse项目导入Android(安卓)Studio,.9
  3. 畅享英特尔® 移动科技 开启Android(安卓
  4. Android控件美化Shape
  5. Android(安卓)高手进阶教程(十三)之----A
  6. Android之NDK开发
  7. Xamarin.Android(安卓)8.0 变得更加甜蜜
  8. [zz]Android中各种JAVA包的功能描述
  9. Android(安卓)activity-alias 多入口配置
  10. Android(安卓)Animation 用法简单介绍