我想要滚动两个ListView
个对象。它们是并排的,所以如果一个滚动一定量,另一个滚动相同数量。我已经找到了一些关于如何做到这一点的例子,但我相信它们依赖于ListView
中相同高度的项目(如果我错了,请纠正我)。我的一个ListView
对象中的项目比另一个中的项目高,跨越2-3项。
如何将这两个ListView
个对象“锁定”在一起?
答案 0 :(得分:6)
我创建了一个粗略的课程,基本上完成了我想要做的事情。如果第二个列表比第一个列表长或者方向改变,那么处理它并不够智能,但它足以让概念失效。
进行设置:
list1.setOnScrollListener(new SyncedScrollListener(list2));
list2.setOnScrollListener(new SyncedScrollListener(list1));
package com.xorbix.util;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
public class SyncedScrollListener implements OnScrollListener{
int offset;
int oldVisibleItem = -1;
int currentHeight;
int prevHeight;
private View mSyncedView;
public SyncedScrollListener(View syncedView){
if(syncedView == null){
throw new IllegalArgumentException("syncedView is null");
}
mSyncedView = syncedView;
}
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
int[] location = new int[2];
if(visibleItemCount == 0){
return;
}
if(oldVisibleItem != firstVisibleItem){
if(oldVisibleItem < firstVisibleItem){
prevHeight = currentHeight;
currentHeight = view.getChildAt(0).getHeight();
offset += prevHeight;
}else{
currentHeight = view.getChildAt(0).getHeight();
View prevView;
if((prevView = view.getChildAt(firstVisibleItem - 1)) != null){
prevHeight = prevView.getHeight();
}else{
prevHeight = 0;
}
offset -= currentHeight;
}
oldVisibleItem = firstVisibleItem;
}
view.getLocationOnScreen(location);
int listContainerPosition = location[1];
view.getChildAt(0).getLocationOnScreen(location);
int currentLocation = location[1];
int blah = listContainerPosition - currentLocation + offset;
mSyncedView.scrollTo(0, blah);
}
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
}
答案 1 :(得分:0)
我认为使用带有2列的GridView更合适,如下所示:
<GridView
android:id="@+id/grid_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="2"
/>
答案 2 :(得分:0)
我想要做的是收听左侧列表视图的滚动事件,然后通过适当的偏移滚动右侧列表视图。下面是我的代码,我测试了它(非常简单,只是告诉你我的想法),你可以根据它添加你的代码。
package viewTest.example.hy;
import android.app.Activity;
import android.os.Bundle;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AbsListView.OnScrollListener;
public class ViewTestActivity extends Activity {
private ArrayAdapter<String> adapter0;
private ArrayAdapter<String> adapter1;
private String[] array0;
private String[] array1;
private ListView lv0;
private ListView lv1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
array0 = getResources().getStringArray(R.array.LV0);//letters from A to O
array1 = getResources().getStringArray(R.array.LV1);//numbers from 1 to 14
adapter0 = new ArrayAdapter<String>(this, R.layout.item, array0);
adapter1 = new ArrayAdapter<String>(this, R.layout.item, array1);
lv0 = (ListView) findViewById(R.id.listView1);
lv1 = (ListView) findViewById(R.id.listView2);
lv0.setAdapter(adapter0);
lv1.setAdapter(adapter1);
lv0.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
lv1.setSelection(firstVisibleItem);//force the right listview to scrollyou may have to do some calculation to sync the scrolling status of the two listview.
}
});
}
}
这是main.xml布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scrollbars="none">
</ListView>
<ListView
android:id="@+id/listView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scrollbars="none" >
</ListView>
</LinearLayout>
这是我的截图:
答案 3 :(得分:0)
我最终在这里使用了顶级s / o答案:Android. Scrolling 2 listviews together
拦截和重定向事件似乎比尝试对列表视图的维度和状态进行自己的数学运算更优雅和一致。