有什么方法可以一次只滚动一个ListView项。
就像您向上滑动一样,它只会向上滚动一个项目,而不会像向下滚动的普通滚动一样向下滚动一个项目。

请帮忙!

这是我的代码:

public class ListViewTestExample extends Activity {

    ListView listView;

    private GestureDetectorCompat mDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_list_view_test);

        mDetector = new GestureDetectorCompat(this, new MyGestureListener());
        // Grid ListView object from XML
        listView = (ListView) findViewById(R.id.list);

        mDetector = new GestureDetectorCompat(this, new MyGestureListener());
        // Defined Array values to show in ListView

        String[] values = new String[] { "Android List View",
         "Adapter implementation", "Simple List View In Android",
         "Create List View Android", "Android Example",
         "List View Source Code", "List View Array Adapter",
         "Android Example List View" };

         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
         android.R.layout.simple_list_item_1, android.R.id.text1, values);

         listView.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public boolean onTouchEvent(MotionEvent event) {
        this.mDetector.onTouchEvent(event);
        return super.onTouchEvent(event);
    }

    class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
        Context context = getApplicationContext();

        public boolean onDown(MotionEvent event) {
                return true;
        }

        public void onLongPress(MotionEvent event) {
            Toast.makeText(context, "Long Press", Toast.LENGTH_SHORT).show();
        }

        public boolean onFling(MotionEvent event1, MotionEvent event2,
                float velocityX, float velocityY) {

            float sensitvity = 50;

            // TODO Auto-generated method stub
            if ((event1.getX() - event2.getX()) > sensitvity) {
                Toast.makeText(context, "Swipe Left", Toast.LENGTH_SHORT)
                    .show();
            } else if ((event2.getX() - event1.getX()) > sensitvity) {
                Toast.makeText(context, "Swipe Right", Toast.LENGTH_SHORT)
                    .show();
            }
            if ((event1.getY() - event2.getY()) > sensitvity) {
                if (event2.getPressure() > event1.getPressure()) {
                    Toast.makeText(context, "Swipe Up + Hold",
                            Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(context, "Swipe Up", Toast.LENGTH_SHORT).show();
                }
            } else if ((event2.getY() - event1.getY()) > sensitvity) {
                if (event2.getPressure() > event1.getPressure()) {
                    Toast.makeText(context, "Swipe Down + Hold",
                        Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(context, "Swipe Down", Toast.LENGTH_SHORT).show();
                }
            }
            return true;
        }

        public boolean onSingleTapConfirmed(MotionEvent event) {
            Toast.makeText(context, "Single Tap Confirmed", Toast.LENGTH_SHORT)
                .show();
            return true;
        }

        public boolean onDoubleTap(MotionEvent event) {
            Toast.makeText(context, "Double Tap Confirmed", Toast.LENGTH_SHORT).show();
            return true;
        }

    }

}

最佳答案

您可以在onTouchListener()中使用listView.setScrollY(int value)。您可以拦截触摸事件并测量项目 View 的高度,然后再测量setScrollY(listview的当前高度+项目的高度)。

关于android - Android中的ListView项一次滚动一个,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18822017/

10-09 03:28