当前位置: 代码迷 >> Android >> listview怎么实现水平滚动
  详细解决方案

listview怎么实现水平滚动

热度:56   发布时间:2016-05-01 15:11:17.0
listview如何实现水平滚动
如题~~~
列太多,如何实现水平滚动条。

------解决方案--------------------
添加一个类似苹果的左右滑动切屏幕功能

Java code
package com.xu81.testflip;import android.app.Activity;import android.os.Bundle;import android.view.GestureDetector;import android.view.MotionEvent;import android.view.View;import android.view.GestureDetector.OnGestureListener;import android.view.animation.AnimationUtils;import android.widget.TextView;import android.widget.ViewFlipper;public class TestFlip extends Activity implements OnGestureListener {    private ViewFlipper flipper;    private GestureDetector detector;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        detector = new GestureDetector(this);        flipper = (ViewFlipper) this.findViewById(R.id.ViewFlipper01);        flipper.addView(addTextView("step 1"));        flipper.addView(addTextView("step 2"));        flipper.addView(addTextView("step 3"));        flipper.addView(addTextView("step 4"));        flipper.addView(addTextView("step 5"));    }    private View addTextView(String text) {        TextView tv = new TextView(this);        tv.setText(text);        tv.setGravity(1);        return tv;    }    @Override    public boolean onTouchEvent(MotionEvent event) {        return this.detector.onTouchEvent(event);    }    @Override    public boolean onDown(MotionEvent e) {        return false;    }    @Override    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,            float velocityY) {        if (e1.getX() - e2.getX() > 120) {            this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));            this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));            this.flipper.showNext();            return true;        } else if (e1.getX() - e2.getX() < -120) {            this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_in));            this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_out));            this.flipper.showPrevious();            return true;        }        return false;    }    @Override    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,            float distanceY) {        return false;    }    @Override    public boolean onSingleTapUp(MotionEvent e) {        return false;    }    @Override    public void onLongPress(MotionEvent arg0) {        // TODO Auto-generated method stub            }    @Override    public void onShowPress(MotionEvent arg0) {        // TODO Auto-generated method stub            }}
------解决方案--------------------
fragment里面的demo有你想要的效果
  相关解决方案