码迷,mamicode.com
首页 > 其他好文 > 详细

多个ListView同步滚动

时间:2014-10-29 12:59:30      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:listview   listview同步滚动   

引言:

开发的过程中可能会遇到2个或者更多的listview为了保持对应关系一起滚动的情况,这篇文章给大家演示这种效果。


效果:

bubuko.com,布布扣

实现原理:

在滚动其中任何一个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;
		}

	}
}



全文完




多个ListView同步滚动

标签:listview   listview同步滚动   

原文地址:http://blog.csdn.net/zhangweiwtmdbf/article/details/40584253

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!