ViewFlipper用法
ViewFlipper可以添加任意view对象,包括图片,文本,按钮,listview等。
示例代码:
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.ViewGroup.LayoutParams;
- import android.view.animation.AnimationUtils;
- import android.widget.Button;
- import android.widget.ImageView;
- import android.widget.TextView;
- import android.widget.ViewFlipper;
- public class ViewFlipperTest extends Activity {
- private Button previous, next;
- private ViewFlipper flipper;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- initiaView();
- flipper.addView(addButtonByText("Start"),
- new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
- flipper.addView(addTextByText("Gutouwang"));
- flipper.addView(addImageById(R.drawable.food_gutouwang));
- flipper.addView(addTextByText("Fushan Liaoli"));
- flipper.addView(addImageById(R.drawable.food_foshanliaoli));
- flipper.addView(addButtonByText("End"),
- new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
- }
- /**
- * Initialize view
- */
- private void initiaView(){
- previous = (Button) findViewById(R.id.btnPrevious);
- next = (Button) findViewById(R.id.btnNext);
- flipper = (ViewFlipper) findViewById(R.id.flipper);
- flipper.setInAnimation(AnimationUtils.loadAnimation(this,
- android.R.anim.fade_in));
- flipper.setOutAnimation(AnimationUtils.loadAnimation(this,
- android.R.anim.fade_out));
- previous.setOnClickListener(listener);
- next.setOnClickListener(listener);
- }
- private OnClickListener listener = new OnClickListener(){
- public void onClick(View v) {
- // TODO Auto-generated method stub
- switch(v.getId()){
- case R.id.btnPrevious:
- flipper.showPrevious();
- break;
- case R.id.btnNext:
- flipper.showNext();
- break;
- }
- }
- };
- public View addTextByText(String text){
- TextView tv = new TextView(this);
- tv.setText(text);
- tv.setGravity(1);
- return tv;
- }
- public View addImageById(int id){
- ImageView iv = new ImageView(this);
- iv.setImageResource(id);
- return iv;
- }
- public View addButtonByText(String text){
- Button btn = new Button(this);
- btn.setText(text);
- return btn;
- }
- }
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- >
- <Button
- android:id="@+id/btnPrevious"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginRight="5dip"
- android:text="Previous"
- />
- <Button
- android:id="@+id/btnNext"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="5dip"
- android:text="Next"
- />
- </LinearLayout>
- <ViewFlipper
- android:id="@+id/flipper"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:gravity="center"
- >
- </ViewFlipper>
- </LinearLayout>
增加以下处理可以解决触摸button,listview等view上滑动的问题
- @Override
- public boolean dispatchTouchEvent(MotionEvent ev){
- this.detector.onTouchEvent(ev);//在这里先处理下你的手势左右滑动事件
- return super.dispatchTouchEvent(ev);
- }
参考地址:
https://topic.csdn.net/u/20101229/10/9bd48452-08a4-4e82-b92c-2c6fd7c95756.html
最后更新:2017-04-02 06:51:43