开发的过程中可能会遇到2个或者更多的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" />
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; } } }
全文完
原文地址:http://blog.csdn.net/zhangweiwtmdbf/article/details/40584253