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


当前回答

对于我的应用程序,我使用一个包含LinearLayout的HorizontalScrollView,它的方向设置为水平。为了在里面添加图像,我在活动中创建ImageViews,并将它们添加到我的LinearLayout中。例如:

<HorizontalScrollView 
        android:id="@+id/photo_scroll"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:scrollbars="horizontal"
        android:visibility="gone">

        <LinearLayout 
            android:id="@+id/imageview_holder"
            android:layout_width="wrap_content"
            android:orientation="horizontal"
            android:layout_height="match_parent">

        </LinearLayout>
    </HorizontalScrollView>

这对我来说很好。在活动中,我所要做的就是下面的代码:

LinearLayout imgViewHolder = findViewById(R.id.imageview_holder);
ImageView img1 = new ImageView(getApplicationContext());
//set bitmap
//set img1 layout params
imgViewHolder.add(img1);
ImageView img2 = new ImageView(getApplicationContext());
//set bitmap
//set img2 layout params
imgViewHolder.add(img2); 

就像我说的,这对我来说很有用,我希望它也能帮助那些希望实现这一目标的人。

其他回答

因为谷歌引入了Android支持库v7 21.0.0,你可以使用RecyclerView水平滚动项目。RecyclerView小部件是ListView的一个更高级、更灵活的版本。

要使用RecyclerView,只需添加依赖项:

com.android.support:recyclerview-v7:23.0.1

下面是一个例子:

public class MyActivity extends Activity {

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

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(layoutManager);

        MyAdapter adapter = new MyAdapter(myDataset);
        recyclerView.setAdapter(adapter);
    }
}

关于RecyclerView的更多信息:

https://developer.android.com/training/material/lists-cards.html https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html

我在自己的一个项目中也做了同样的事情,最后我也写了自己的项目。我称之为HorzListView现在是我的开源Aniqroid库的一部分。

http://aniqroid.sileria.com/doc/api/(在底部寻找下载或使用谷歌代码项目查看更多下载选项:http://code.google.com/p/aniqroid/downloads/list)

类文档在这里:http://aniqroid.sileria.com/doc/api/com/sileria/android/view/HorzListView.html

我的解决方案是简单地使用ViewPager小部件。它不像Gallery那样中心锁定,并且有一个用于循环视图的内置功能(如ListView)。你可能会看到类似的方法在谷歌播放应用程序,每当你处理水平滚动列表。

你只需要扩展PagerAdapter并在那里执行一些调整:

public class MyPagerAdapter extends PagerAdapter {

    private Context mContext;

    public MyPagerAdapter(Context context) {
        this.mContext = context;
    }

    // As per docs, you may use views as key objects directly 
    // if they aren't too complex
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.item, null);
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public int getCount() {
        return 10;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    // Important: page takes all available width by default,
    // so let's override this method to fit 5 pages within single screen
    @Override
    public float getPageWidth(int position) {
        return 0.2f;
    }
}

因此,你将拥有一个带有适配器的水平滚动小部件,如下所示:

有一个很棒的库,叫做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);//或多重或无。

对于我的应用程序,我使用一个包含LinearLayout的HorizontalScrollView,它的方向设置为水平。为了在里面添加图像,我在活动中创建ImageViews,并将它们添加到我的LinearLayout中。例如:

<HorizontalScrollView 
        android:id="@+id/photo_scroll"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:scrollbars="horizontal"
        android:visibility="gone">

        <LinearLayout 
            android:id="@+id/imageview_holder"
            android:layout_width="wrap_content"
            android:orientation="horizontal"
            android:layout_height="match_parent">

        </LinearLayout>
    </HorizontalScrollView>

这对我来说很好。在活动中,我所要做的就是下面的代码:

LinearLayout imgViewHolder = findViewById(R.id.imageview_holder);
ImageView img1 = new ImageView(getApplicationContext());
//set bitmap
//set img1 layout params
imgViewHolder.add(img1);
ImageView img2 = new ImageView(getApplicationContext());
//set bitmap
//set img2 layout params
imgViewHolder.add(img2); 

就像我说的,这对我来说很有用,我希望它也能帮助那些希望实现这一目标的人。