An_viewpager+小圆点02

Version:1.0StartHTML:0000000128EndHTML:0000019763StartFragment:0000000128EndFragment:0000019763SourceURL:about:blank

packagecom.example.taodemo;

importandroid.content.Context;

importandroid.os.AsyncTask;

importandroid.os.Handler;

importandroid.os.Message;

importandroid.support.v4.view.PagerAdapter;

importandroid.support.v4.view.ViewPager;

importandroid.support.v7.app.AppCompatActivity;

importandroid.os.Bundle;

importandroid.support.v7.widget.LinearLayoutCompat;

importandroid.view.View;

importandroid.view.ViewGroup;

importandroid.view.ViewParent;

importandroid.widget.ImageView;

importandroid.widget.LinearLayout;

importjava.lang.ref.WeakReference;

importjava.util.ArrayList;

importjava.util.List;

public classMainActivityextendsAppCompatActivity {

ViewPagervp;

intindex=0;

Listlist_doc;

Listlist_vp;

intimages[]={R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher};

Handlerhandler=newHandler();

@Override

protected voidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

vp= (ViewPager) findViewById(R.id.main_vp);

initvpData();

}

private voidinitvpData() {

newAsyncTask(){

@Override

protectedString doInBackground(String... strings) {

return null;

}

@Override

protected voidonPostExecute(String s) {

initvp();

super.onPostExecute(s);

}

}.execute();

}

private voidinitvp() {

list_vp=newArrayList<>();

for(inti=0;i<4;i++){

ImageView imgv=newImageView(this);

imgv.setScaleType(ImageView.ScaleType.FIT_XY);

imgv.setImageResource(images[i]);

list_vp.add(imgv);

}

MyAdapter myAdapter=newMyAdapter();

vp.setAdapter(myAdapter);

handler.postDelayed(newRunnable() {

@Override

public voidrun() {

index=vp.getCurrentItem();

index++;

vp.setCurrentItem(index);

handler.postDelayed(this,3000);

}

},3000);

initDoc();

vp.addOnPageChangeListener(newViewPager.OnPageChangeListener() {

@Override

public voidonPageScrolled(intposition,floatpositionOffset,intpositionOffsetPixels) {

}

@Override

public voidonPageSelected(intposition) {

//                int index=position%list_doc.size();

for(inti=0;i

if(i==position%images.length){

list_doc.get(i).setImageResource(R.drawable.doc_check);

}else{

list_doc.get(i).setImageResource(R.drawable.doc_uncheck);

}

}

//                list_doc.get(index);

}

@Override

public voidonPageScrollStateChanged(intstate) {

}

});

}

private voidinitDoc() {

LinearLayout ll_doc= (LinearLayout) findViewById(R.id.ll_doc);

list_doc=newArrayList();

if(list_vp!=null){

for(inti=0;i

ImageView iv=newImageView(MainActivity.this);

LinearLayoutCompat.LayoutParamsparams=newLinearLayoutCompat.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

params.rightMargin=10;

iv.setLayoutParams(params);

if(i==0){

iv.setImageResource(R.drawable.doc_check);

}else{

iv.setImageResource(R.drawable.doc_uncheck);

}

ll_doc.addView(iv,params);

list_doc.add(iv);

}

}

}

classMyAdapterextendsPagerAdapter{

@Override

public intgetCount() {

return10000;

}

@Override

public booleanisViewFromObject(View view, Object object) {

returnview==object;

}

@Override

publicObject instantiateItem(ViewGroup container,intposition) {

position%=list_vp.size();

if(position<0){

position=list_vp.size()+position;

}

ImageView view=list_vp.get(position);

ViewParent viewParent=view.getParent();

if(viewParent!=null){

ViewGroup parent= (ViewGroup) viewParent;

parent.removeView(view);

}

container.addView(view);

returnview ;

}

@Override

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

}

}

}



An_viewpager+小圆点02_第1张图片


An_viewpager+小圆点02_第2张图片


An_viewpager+小圆点02_第3张图片
An_viewpager+小圆点02_第4张图片
An_viewpager+小圆点02_第5张图片


An_viewpager+小圆点02_第6张图片


An_viewpager+小圆点02_第7张图片

你可能感兴趣的:(An_viewpager+小圆点02)