1. 程式人生 > >Android之實現上下左右翻頁效果

Android之實現上下左右翻頁效果

Aphid FlipView是一個能夠實現Flipboard翻頁效果的UI元件。

下載完畢後匯入到當前你的專案中,我們來下上下翻頁的效果圖:

             

直接貼出程式碼:

佈局檔案:

<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:background="@android:color/white" >
    <ImageView 
        android:id="@+id/tv_number"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent" 
        android:gravity="center"
        android:scaleType="fitXY"
       />

</LinearLayout></span>


主檔案:

<span style="font-size:14px;">public class MainActivity extends Activity {
	private int[] image=new int[]{R.drawable.qd,R.drawable.qg,R.drawable.qw};
	private Context activity=this;
	protected FlipViewController flipViewController;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		flipViewController=new FlipViewController(activity);
		flipViewController.setAdapter(new baseAdapter());
		setContentView(flipViewController);
		
		
	}
	public class baseAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return image.length;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			if(convertView==null){
				convertView=new NumberTextVeiw(activity);
			}
			((ImageView)(convertView.findViewById(R.id.tv_number))).setBackgroundResource(image[position]);
			return convertView;
		}
		
		
		
	}
	private class NumberTextVeiw extends LinearLayout{
		ViewHolder viewHolder;
		public NumberTextVeiw(Context context) {
			super(context);
			inflate(context, R.layout.text_number_view, this);
			viewHolder=new ViewHolder();
			viewHolder.tv_number=(ImageView)findViewById(R.id.tv_number);
		}
	}
	private class ViewHolder {
		public ImageView tv_number;

	}

	
}</span>


以上是實現上下翻頁效果的程式碼,如果想實現左右翻頁只需更改一處程式碼即可:

<span style="font-size:14px;">flipViewController=new FlipViewController(activity);</span>

更改為:

<span style="font-size:14px;">flipViewController=new FlipViewController(this, FlipViewController.HORIZONTAL);</span>