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


当前回答

为了找到解决这个问题的办法,我做了很多研究。简短的回答是,没有好的解决方案,除非重写私有方法之类的东西。我发现最好的方法是自己从头开始通过扩展AdapterView来实现它。这很悲惨。请看我关于水平listview的SO问题。

其他回答

从这里下载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;
}

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

根据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);

这有点(非常)晚了,但我把它贴出来,以防以后有人来看。

在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);

当适配器中的数据涉及到另一个线程时,HorizontialListView不能工作。一切都100%在UI线程上运行。这是多线程中的一个大问题。我认为使用HorizontialListView不是解决你的问题的最佳方案。HorzListView是一个更好的方法。你只需用HorzListView替换你以前的画廊。您不需要修改关于适配器的代码。然后一切都会如你所愿。查看https://stackoverflow.com/a/12339708/1525777关于HorzListView。