Android系统。 2滚动列表视图在一起 [英] Android. Scrolling 2 listviews together
问题描述
确定。我正在努力实现的布局,做同样的效果在Excel冻结窗格。这就是我想要的水平滚动与主ListView和左手的ListView垂直滚动与主ListView的标题行。标题行和左手列表视图中的其他维度滚动时要保持不动。
OK. What I'm trying to achieve is a layout that does the same effect as frozen panes in Excel. That is I want a header row that scrolls horizontally with the main ListView and a left hand ListView that scrolls vertically with the main ListView. The header row and the left hand listview should remain stationary when scrolling in the other dimension.
下面是XML布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recordViewLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout android:layout_width="160dp"
android:layout_height="match_parent"
android:orientation="vertical">
<CheckBox
android:id="@+id/checkBoxTop"
android:text="Check All"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView android:id="@+id/engNameList"
android:layout_width="160dp"
android:layout_height="wrap_content"/>
</LinearLayout>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout android:id="@+id/scroll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="@layout/record_view_line" android:id="@+id/titleLine" />
<ListView
android:id="@android:id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
然后我使用了这个code在ListActivity
I'm then using this code in the ListActivity
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
View v = recordsListView.getChildAt(0);
int top = (v == null) ? 0 : v.getTop();
((ListView)findViewById(R.id.engNameList)).setSelectionFromTop(firstVisibleItem, top);
}
此应该引起左手的ListView滚动时右手之一由用户滚动。不幸的是它不
This should cause the left hand ListView to scroll when the right hand one is scrolled by the user. Unfortunately it doesn't.
我已经有点对谷歌和似乎setSelectionFromTop()函数不会对嵌套在一个以上的布局是一个ListView工作。
I've had a bit of a google about and it seems the setSelectionFromTop() function will not work on a ListView that is nested inside more than one layout.
如果是这样的话任何人都可以提出一个方法,让他们一起滚动,或以不同的方式来设置布局或不同的技术完全。
If this is the case can anyone suggest a way to get them to scroll together or a different way to set up the layout or a different technique altogether.
推荐答案
确定。我现在有一个答案。问题在于.setSelectionFromTop()只会工作,如果列表视图是在顶部的布局(即没有嵌套)。 Afters有些挠头,我意识到,我可以让我的布局RelativeLayout的,并得到了相同的外观,但无需窝布局复选框和列表视图。这是新的布局:
OK. I have an answer now. The problem being that .setSelectionFromTop() would only work if the listview was in the top layout (ie. not nested). Afters some head scratching I realised that I could make my layout a RelativeLayout and get the same look but without having to nest layouts for the checkbox and listview. This is the new layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recordViewLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<CheckBox android:id="@+id/checkBoxTop"
android:text="Check All"
android:layout_width="160dp"
android:layout_height="wrap_content"/>"
<ListView android:id="@+id/engNameList"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_below="@+id/checkBoxTop"/>
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/checkBoxTop">
<LinearLayout android:id="@+id/scroll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="@layout/record_view_line" android:id="@+id/titleLine" />
<ListView
android:id="@android:id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
这基本上是code表示去与布局。
This basically is the code that goes with the layout.
在的onCreate()
In onCreate()
engListView=getListView();
engListView.setOnTouchListener(this);
recordListView=(ListView)findViewById(R.id.recordList);
recordListView.setOnScrollListener(this);
和侦听器方法:
public boolean onTouch(View arg0, MotionEvent event) {
recordListView.dispatchTouchEvent(event);
return false;
}
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
View v=view.getChildAt(0);
if(v != null)
engListView.setSelectionFromTop(firstVisibleItem, v.getTop());
}
这篇关于Android系统。 2滚动列表视图在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!