是否有可能使ListView水平?我使用图库视图完成了此操作,但所选项目会自动出现在屏幕的中心。我不希望选中的项目位于我点击的同一位置。我该如何纠正这个问题?我的想法是用水平滚动来设置ListView。分享你的想法?


当前回答

@Paul回答链接到一个伟大的解决方案,但代码不允许对项目的孩子使用onClickListeners(回调函数永远不会被调用)。我一直在努力寻找一个解决方案,我决定在这里张贴你需要修改的代码(以防有人需要它)。

而不是重写dispatchTouchEvent重写onTouchEvent。使用dispatchTouchEvent的相同代码并删除方法(您可以在这里阅读两者之间的区别http://developer.android.com/guide/topics/ui/ui-events.html#EventHandlers)

@Override
public boolean onTouchEvent(MotionEvent event) {
    boolean handled = mGesture.onTouchEvent(event);
    return handled;
}

然后,添加下面的代码,它将决定从子项中窃取事件,并将其交给我们的onTouchEvent,或让它由它们处理。

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    switch( ev.getActionMasked() ){
        case MotionEvent.ACTION_DOWN:
             mInitialX = ev.getX();
             mInitialY = ev.getY();             
             return false;
        case MotionEvent.ACTION_MOVE:
             float deltaX = Math.abs(ev.getX() - mInitialX);
             float deltaY = Math.abs(ev.getY() - mInitialY);
             return ( deltaX > 5 || deltaY > 5 );
        default:
             return super.onInterceptTouchEvent(ev);
    }
}

最后,不要忘记在你的类中声明变量:

private float mInitialX;
private float mInitialY;

其他回答

根据Android文档RecyclerView是新的方式来组织项目在列表视图和水平显示

优点:

由于使用Recyclerview适配器,ViewHolder模式是 自动实现 动画很容易执行 更多特性

关于RecyclerView的更多信息:

grokkingandroid.com antonioleiva.com

示例:

survivingwithandroid.com

只需添加下面的块,使ListView水平从垂直

代码片段

LinearLayoutManager layoutManager= new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL, false);
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(layoutManager);

其实很简单: 简单地旋转列表视图躺在它的一边

mlistView.setRotation(-90);

然后在膨胀子对象时,它应该在getView方法中。你让孩子们站直:

 mylistViewchild.setRotation(90);

编辑: 如果你的ListView在旋转后不合适,把ListView放在这个RotateLayout中,就像这样:

 <com.github.rongi.rotate_layout.layout.RotateLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:angle="90"> <!-- Specify rotate angle here -->

    <ListView
       android:layout_width="match_parent"
       android:layout_height="match_parent">
    </ListView>
</com.github.rongi.rotate_layout.layout.RotateLayout>

你总是可以创建你的textviews等动态和设置你的onclicklistener,就像你会做一个适配器

这不是一个很好的答案,但是如何使用水平滚动视图呢?

有一个很棒的库,叫做TwoWayView,它很容易实现,只需要将项目库包含到你的工作空间中,并将其作为库项目添加到你的原始项目中,然后遵循以下步骤,这是最初在这里提到的:

首先,让我们添加一个样式来指示ListView的方向 (水平或垂直)in (res/values/styles.xml):

<style name="TwoWayView">
    <item name="android:orientation">horizontal</item>
</style>

然后,

在你的布局XML中,使用下面的代码来添加TwoWayView:

<org.lucasr.twowayview.TwoWayView 
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:id="@+id/lvItems"
     style="@style/TwoWayView"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:drawSelectorOnTop="false"
     tools:context=".MainActivity" />

最后,声明它并像任何常规的ListView一样处理它:

TwoWayView lvTest = (TwoWayView) findViewById(R.id.lvItems);

ListView的所有方法将在这里照常工作,但只有一个区别,我注意到,这是设置选择模式时,方法setChoiceMode不接受int值,而是从enum称为ChoiceMode的值,所以list_view.setChoiceMode(ListView. choice_mode_single);将lvTest.setChoiceMode(choicmode . single);//或多重或无。