是否有可能使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 L预览版的支持库中,有一个RecyclerView,它完全是你想要的。

现在,你只能通过L预览SDK获得它,你需要将你的minSdk设置为L。但是你可以将所有必要的文件复制到你的项目中,并以这种方式使用它们,直到L正式发布。

你可以在这里下载预览文档。

警告:回收器视图的API可能会改变,它可能有bug。

更新

水平列表视图的源代码是:

LinearLayoutManager layoutManager
    = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);

RecyclerView myList = findViewById(R.id.my_recycler_view);
myList.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>

因为谷歌引入了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

从这里下载jar文件

现在把它放到你的libs文件夹中,右键点击它并选择“添加为库”

现在在main.xml中放入这段代码

 <com.devsmart.android.ui.HorizontalListView
    android:id="@+id/hlistview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />

现在在Activity类中,如果你想要带有图像的水平列表视图,那么就把这段代码放进去

  HorizontalListView hListView = (HorizontalListView) findViewById(R.id.hlistview);
    hListView.setAdapter(new HAdapter(this));


 private class HAdapter extends BaseAdapter {

    LayoutInflater inflater;

    public HAdapter(Context context) {
        inflater = LayoutInflater.from(context);

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return Const.template.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        HViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.listinflate, null);
            holder = new HViewHolder();
            convertView.setTag(holder);

        } else {
            holder = (HViewHolder) convertView.getTag();
        }
        holder.img = (ImageView) convertView.findViewById(R.id.image);
        holder.img.setImageResource(Const.template[position]);
        return convertView;
    }

}

class HViewHolder {
    ImageView img;
}