布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.adapterviewflipper.MainActivity" > <AdapterViewFlipper android:id="@+id/flipper" android:layout_height="match_parent" android:layout_width="match_parent" android:flipInterval="3000" android:layout_alignParentTop="true"></AdapterViewFlipper> <Button android:id="@+id/last" android:text="上一个" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentBottom="true"/> <Button android:id="@+id/auto" android:text="自动" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_toRightOf="@id/last" android:layout_marginLeft="10dp"/> <Button android:id="@+id/next" android:text="下一个" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_toRightOf="@id/auto" android:layout_marginLeft="10dp"/> </RelativeLayout>
主Activity
package com.example.adapterviewflipper; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.widget.AdapterViewFlipper; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity { private int img[] = {R.drawable.img,R.drawable.img2,R.drawable.img3,R.drawable.img4} ; private Button next,auto ,last; private AdapterViewFlipper flipper ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.flipper = (AdapterViewFlipper)findViewById(R.id.flipper) ; this.last = (Button)findViewById(R.id.last) ; this.next = (Button)findViewById(R.id.next) ; this.auto = (Button)findViewById(R.id.auto) ; BaseAdapter adapter = new BaseAdapter() { @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ImageView image = new ImageView(getApplicationContext()) ; image.setImageResource(img[position]); image.setScaleType(ImageView.ScaleType.FIT_XY); image.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT)); return image ; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return position; } @Override public int getCount() { // TODO Auto-generated method stub return img.length; } }; this.flipper.setAdapter(adapter); addListener() ; } private void addListener() { this.last.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { flipper.showNext(); flipper.stopFlipping(); } }); this.next.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { flipper.showPrevious(); flipper.stopFlipping(); } }); this.auto.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { flipper.startFlipping(); } }); } }
效果图 ,一个类似于相册的程序。
时间: 2024-10-26 11:01:22