以前寫過Banner的使用能夠實現輪播圖並且很是好用如下的是我以前寫的輪播圖;java
首先是PageAdapter:android
public class PagAdapter extends PagerAdapter{ Context context; ArrayList<Tu> tlist; public PagAdapter(Context context, ArrayList<Tu> tlist) { super(); this.context = context; this.tlist = tlist; } public PagAdapter() { super(); // TODO Auto-generated constructor stub } @Override public int getCount() { // TODO Auto-generated method stub return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0==arg1; } @Override public Object instantiateItem(ViewGroup container, int position) { int pos=position%tlist.size(); Tu tu = tlist.get(pos); View view = LayoutInflater.from(context).inflate(R.layout.tu,null); ImageView img=(ImageView)view.findViewById(R.id.img); img.setImageResource(tu.img); TextView tv=(TextView) view.findViewById(R.id.tv); tv.setText(tu.name); container.addView(view); return view; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View)object); } }
而後是實體類:app
public class Tu { public String name; public int img; public Tu(String name, int img) { super(); this.name = name; this.img = img; } public Tu() { super(); // TODO Auto-generated constructor stub } }
最後是MianActivity:ide
import java.util.ArrayList; import android.os.Bundle; import android.os.Handler; import android.app.Activity; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.view.Menu; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; public class MainActivity extends Activity implements OnPageChangeListener,OnTouchListener{ ViewPager vp; ArrayList<Tu> tlist; int[] imgs={R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e,R.drawable.f}; LinearLayout ll; int pre=0; Handler handler=new Handler(){ public void handleMessage(android.os.Message msg) { vp.setCurrentItem(vp.getCurrentItem()+1); sendEmptyMessageDelayed(999,500); }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); vp=(ViewPager) findViewById(R.id.vp); vp.setOnPageChangeListener(this); ll=(LinearLayout) findViewById(R.id.pointLayout); tlist=new ArrayList<Tu>(); for(int i=0;i<imgs.length;i++){ tlist.add(new Tu("第"+i+"張",imgs[i])); } for(int i=0;i<imgs.length;i++){ ImageView view = new ImageView(this); LayoutParams params=new LayoutParams(8,8);//這個要倒widget.LinearLayout int left=i==0?0:20; params.leftMargin = left; view.setBackgroundResource(R.drawable.buj); view.setEnabled(i==0); view.setLayoutParams(params); ll.addView(view); } vp.setAdapter(new PagAdapter(MainActivity.this,tlist)); int pos=Integer.MAX_VALUE/2%imgs.length; int index=Integer.MAX_VALUE/2-pos; vp.setCurrentItem(index); handler.sendEmptyMessageDelayed(999,500); vp.setOnTouchListener(this); } @Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO Auto-generated method stub } @Override public void onPageSelected(int arg0) { ll.getChildAt(pre).setEnabled(false); int pos=arg0%imgs.length; pre=pos; ll.getChildAt(pos).setEnabled(true); } @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: handler.removeCallbacksAndMessages(null); break; case MotionEvent.ACTION_MOVE: handler.removeCallbacksAndMessages(null); break; case MotionEvent.ACTION_UP: handler.sendEmptyMessageDelayed(999,250); break; } return false; } }
drawable包下:佈局
buj:this
<selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_enabled="true" android:drawable="@drawable/pointy"></item> <item android:state_enabled="false" android:drawable="@drawable/pointn"></item> </selector>
pointn:spa
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <size android:width="6dp" android:height="6dp" /> <solid android:color="@android:color/white"/> </shape>
pointy:code
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <size android:width="6dp" android:height="6dp" /> <solid android:color="@android:color/holo_red_dark"/> </shape>
主佈局:xml
<android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="200dp"> </android.support.v4.view.ViewPager> <LinearLayout android:id="@+id/pointLayout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@id/vp" android:layout_centerHorizontal="true" android:orientation="horizontal" > </LinearLayout>
tu:blog
<ImageView android:id="@+id/img" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@mipmap/a" /> <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="sss" android:textSize="22sp" android:padding="8dp" android:gravity="center" />
dimens:
<dimen name="activity_horizontal_margin">16dp</dimen> <dimen name="activity_vertical_margin">16dp</dimen>
圖片須要本身換,這是我以前寫的;