如何一次在所有屏幕上显示单个列表项?

时间:2012-12-25 09:47:01

标签: android listview

我在android上有列表。每当用户向左或向右滑动屏幕时,我需要向他们显示带有详细信息的单个项目。我应该在这种情况下使用哪种方法。

屏幕截图示例: 第一项

first item http://imageshack.us/a/img707/9789/slide1t.png

当用户向左滑动

时出现第二项

first item http://imageshack.us/a/img163/7351/slide2nl.png

4 个答案:

答案 0 :(得分:1)

您可以使用ViewPagerIndicator {{3p>

答案 1 :(得分:1)

TOMKA和ExpandableListView提到的ViewPageIndicator组合可以解决这个问题:http://developer.android.com/reference/android/widget/ExpandableListView.html

答案 2 :(得分:1)

答案 3 :(得分:0)

我找到了解决问题的简单方法。使用OnGestureListener的ViewSwitcher解决了我的问题。

http://www.youtube.com/watch?v=mGwG8-chUEM

参考文献:

ViewFlipper:http://developer.android.com/reference/android/widget/ViewFlipper.html

ViewSwitcher:http://developer.android.com/reference/android/widget/ViewSwitcher.html

GestureDetector.OnGestureListener:http://developer.android.com/reference/android/view/GestureDetector.OnGestureListener.html

源代码: http://www.mediafire.com/?fg32sn1345xn5dl

我的活动没有所有代码,所以可能无效。但逻辑在这里。

-------我的解决方案--------

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ViewSwitcher
        android:id="@+id/viewSwitcher"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#65ffffff" >

            <TextView
                android:id="@+id/txtHeader"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="0" />

            <TextView
                android:id="@+id/txtDetail"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/txtKurum"
                android:layout_centerHorizontal="true"
                android:text="0" />
        </RelativeLayout>
    </ViewSwitcher>

</LinearLayout>

-activity -

public class MyActivity extends Activity implements OnClickListener,
        OnGestureListener {

    ArrayList<String> liste;

    TextView txtHeader;
    TextView txtDetail;

    private ViewSwitcher switcher;

    int bakiyeCounter = 0;
    private GestureDetector gesturedetector = null;

    int SWIPE_MIN_VELOCITY = 100;
    int SWIPE_MIN_DISTANCE = 100;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bakiye);
        switcher = (ViewSwitcher) findViewById(R.id.viewSwitcher);
        txtHeader = (TextView) findViewById(R.id.txtHeader);
        txtDetail = (TextView) findViewById(R.id.txtDetail);
        gesturedetector = new GestureDetector(this, this);
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        // Get Position
        float ev1X = e1.getX();
        float ev2X = e2.getX();

        // Get distance of X (e1) to X (e2)
        final float xdistance = Math.abs(ev1X - ev2X);

        final float xvelocity = Math.abs(velocityX);

        if ((xvelocity > SWIPE_MIN_VELOCITY)
                && (xdistance > SWIPE_MIN_DISTANCE)) {
            if (ev1X > ev2X)// Switch Left
            {
                if (bakiyeCounter < liste.size() - 1) {
                    bakiyeCounter++;
                    previousView();
                }
            } else// Switch Right
            {
                if (bakiyeCounter > 0) {
                    bakiyeCounter--;
                    nextView();
                }
            }
        }

        return false;
    }

    private void previousView() {


        txtHeader.setText(liste.get(bakiyeCounter).getHeader());
        txtDetail.setText(liste.get(bakiyeCounter).getDetail());
        switcher.showPrevious();

    }

    private void nextView() {

        txtDetail.setText(liste.get(bakiyeCounter).getHeader());
        txtHeader.setText(liste.get(bakiyeCounter).getDetail());
        switcher.showNext();

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return gesturedetector.onTouchEvent(event);
    }

    @Override
    public boolean onDown(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
            float distanceY) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}

}