AdapterViewFlipper learning notes, adapterviewflipper
Layout:
<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/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/plugin" 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 = "previous" 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 = "automatic" 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 = "next" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignParentBottom = "true" android: layout_toRightOf = "@ id/auto" android: layout_marginLeft = "10dp"/> </RelativeLayout>
Main 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 ;@Overrideprotected 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() {@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubImageView 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 ;}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn img.length;}};this.flipper.setAdapter(adapter);addListener() ;}private void addListener() {this.last.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {flipper.showNext();flipper.stopFlipping();}});this.next.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {flipper.showPrevious();flipper.stopFlipping();}});this.auto.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {flipper.startFlipping();}});} }
, A program similar to the album.