Android 多个列表视图和 setSelectionFromTop() 错误?
我尝试使用 ListView API 中的 setSelectionFromTop() 方法同时滚动 2 个列表视图。这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@+id/list_1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"/>
<ListView
android:id="@+id/list_2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"/>
</LinearLayout>
和我的活动:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ListView listView1 = (ListView)findViewById(R.id.list_1);
listView1.setAdapter(new ListAdapter());
ListView listView2 = (ListView)findViewById(R.id.list_2);
listView2.setAdapter(new ListAdapter());
listView2.setOnScrollListener(new OnScrollListener() {
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
View v = view.getChildAt(0);
final int top = (v == null) ? 0 : v.getTop();
listView1.setSelectionFromTop(firstVisibleItem, top);
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
});
}
一切正常,滚动流畅。但是,当我将 list_1
包装在 LinearLayout 的 xml 文件中时,同步滚动被破坏。这是一个错误还是我忘记做某事?
注意:ListView 的数据来源取自 Android 开发人员网站上的 ListView 教程,并且两个 ListView 都使用同一适配器的实例。假设我正确实现了适配器的使用模式。另外,我尝试过使用RelativeLayout,甚至对像素值进行硬编码。我仍然遇到同样的问题。
I'm trying to scroll 2 listviews simultaneously using the setSelectionFromTop() method from the ListView API. Here's my layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@+id/list_1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"/>
<ListView
android:id="@+id/list_2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"/>
</LinearLayout>
and my Activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ListView listView1 = (ListView)findViewById(R.id.list_1);
listView1.setAdapter(new ListAdapter());
ListView listView2 = (ListView)findViewById(R.id.list_2);
listView2.setAdapter(new ListAdapter());
listView2.setOnScrollListener(new OnScrollListener() {
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
View v = view.getChildAt(0);
final int top = (v == null) ? 0 : v.getTop();
listView1.setSelectionFromTop(firstVisibleItem, top);
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
});
}
Everything works fine and the scrolling is smooth. However, when I wrap list_1
in the xml file in a LinearLayout, the synchronized scrolling is broken. Is this a bug or am I forgetting to do something?
Note: The data sourcing the ListViews is taken from the ListView tutorial on the Android developer's site and both ListViews are using instances of the same Adapter. Assume that I implemented the usage patterns for the Adapters correctly. Also, I've tried using RelativeLayout and even hard-coding the pixel values in. I still get the same issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通过重构布局并将两个列表视图置于视图层次结构中的同一级别来解决此问题。
I resolved this issue by re-factoring the layout and putting both listviews at the same level on in the view hierarchy.
实际上,我可能被迫使用一种布局,该布局不会使两个 ListView 位于层次结构树中的同一级别。我不明白的是,为什么当 list_1 被包装在另一个视图中时同步滚动会被破坏。
Actually, I might be forced to use a layout that won't have both ListViews at the same level in the hierarchy tree. What I don't understand is why the synchronized scrolling is broken when list_1 is wrapped in another View.