是否有可能使ListView水平?我使用图库视图完成了此操作,但所选项目会自动出现在屏幕的中心。我不希望选中的项目位于我点击的同一位置。我该如何纠正这个问题?我的想法是用水平滚动来设置ListView。分享你的想法?
当前回答
我在自己的一个项目中也做了同样的事情,最后我也写了自己的项目。我称之为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
其他回答
因为谷歌引入了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
其实很简单: 简单地旋转列表视图躺在它的一边
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文档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);
您可以在支持库中使用RecyclerView。RecyclerView是ListView的一个通用版本,支持:
用于定位项目的布局管理器 默认动画 项目操作
Android循环查看文档
当适配器中的数据涉及到另一个线程时,HorizontialListView不能工作。一切都100%在UI线程上运行。这是多线程中的一个大问题。我认为使用HorizontialListView不是解决你的问题的最佳方案。HorzListView是一个更好的方法。你只需用HorzListView替换你以前的画廊。您不需要修改关于适配器的代码。然后一切都会如你所愿。查看https://stackoverflow.com/a/12339708/1525777关于HorzListView。
推荐文章
- 警告:API ' variable . getjavacompile()'已过时,已被' variable . getjavacompileprovider()'取代
- 安装APK时出现错误
- 碎片中的onCreateOptionsMenu
- TextView粗体通过XML文件?
- 如何使线性布局的孩子之间的空间?
- DSL元素android.dataBinding。enabled'已过时,已被'android.buildFeatures.dataBinding'取代
- ConstraintLayout:以编程方式更改约束
- PANIC: AVD系统路径损坏。检查ANDROID_SDK_ROOT值
- 如何生成字符串类型的buildConfigField
- Recyclerview不调用onCreateViewHolder
- Android API 21工具栏填充
- Android L中不支持操作栏导航模式
- 如何在TextView中添加一个子弹符号?
- PreferenceManager getDefaultSharedPreferences在Android Q中已弃用
- 在Android Studio中创建aar文件