多于ListView同步滚动

简介:

发展过程中可能遇到的2一个或多个其他listview为了用相应的关系保持滚动的情况下一起,本文演示了这种效应为大家。

功效:

多于ListView同步滚动

实现原理:

在滚动当中不论什么一个ListView的时候,同一时候设置其它ListView的滚动位置。

演示样例中使用的是水平ListView。

HListView的项目托管地址是:https://github.com/sephiroth74/HorizontalVariableListView   有兴趣的能够研究一下

步骤:

1、为了能实现同一时候滚动的情况,有一些条件。首先是HListView的Item的宽度是一一直的,如我设置的

<?

xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="60dp"
android:layout_height="60dp"
android:gravity="center"
android:background="#abcdef"
android:text="Terry10" />

2、为了大家仅仅管的感受做法,我将源代码贴出例如以下,大家着重看关键代码就可以:

public class MainActivity extends Activity {

	private HListView mListView1;
private HListView mListView2;
private HListView mListView3;
private HListView mListView4;
private HListView mListView5;
private List<HListView> viewList = new ArrayList<HListView>(); private String[] contentArray = new String[] { "Terry1", "Terry2",
"Terry3", "Terry4", "Terry5", "Terry6", "Terry7", "Terry8",
"Terry9", "Terry10", "Terry11", "Terry12", "Terry13", "Terry14",
"Terry15", "Terry16", "Terry17", "Terry18" }; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView1 = (HListView) findViewById(R.id.listview1);
mListView2 = (HListView) findViewById(R.id.listview2);
mListView3 = (HListView) findViewById(R.id.listview3);
mListView4 = (HListView) findViewById(R.id.listview4);
mListView5 = (HListView) findViewById(R.id.listview5); mListView1.setAdapter(new MyAdapter());
mListView2.setAdapter(new MyAdapter());
mListView3.setAdapter(new MyAdapter());
mListView4.setAdapter(new MyAdapter());
mListView5.setAdapter(new MyAdapter()); viewList.add(mListView1);
viewList.add(mListView2);
viewList.add(mListView3);
viewList.add(mListView4);
viewList.add(mListView5); MyScrollListener mListener = new MyScrollListener();
for (HListView item : viewList) {
item.setOnScrollListener(mListener);
}
} private class MyScrollListener implements OnScrollListener { @Override
public void onScrollStateChanged(AbsHListView view, int scrollState) {
// 关键代码
if (scrollState == SCROLL_STATE_IDLE
|| scrollState == SCROLL_STATE_TOUCH_SCROLL) {
View subView = view.getChildAt(0);
if (subView != null) {
final int top = subView.getLeft();
final int position = view.getFirstVisiblePosition();
for (HListView item : viewList) {
item.setSelectionFromLeft(position, top);
}
}
}
} @Override
public void onScroll(AbsHListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// 关键代码
View subView = view.getChildAt(0);
if (subView != null) {
final int top = subView.getLeft();
for (HListView item : viewList) {
item.setSelectionFromLeft(firstVisibleItem, top);
}
}
} } private class MyAdapter extends BaseAdapter { @Override
public int getCount() {
return contentArray.length;
} @Override
public Object getItem(int position) {
return contentArray[position];
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv = (TextView) getLayoutInflater().inflate(R.layout.item,
parent, false);
tv.setText(contentArray[position]);
return tv;
} }
}

全文完

版权声明:本文博主原创文章,博客,未经同意不得转载。

上一篇:码源中国.gitignore忽略文件配置


下一篇:记一次由于Java泛型类型擦除而导致的问题,及解决办法