实现类似于股票列表联动 转自:http://blog.csdn.net/elinavampire/article/details/42142551

先上效果图,便于理解:

Android实现平板的类股票列表联动


先来说明一下这个效果,首先上下移动通过scrollview实现左右侧同时滑动,右侧listview通过重写HorizontalScrollView实现右侧上下部分能够同时联动

下面贴一下主要代码:

[java] view plain copy
  1. package com.thea.guo.leftrightscrool.view;  
  2.   
  3. import android.content.Context;  
  4. import android.util.AttributeSet;  
  5. import android.view.View;  
  6. import android.widget.HorizontalScrollView;  
  7. /** 
  8. * @Description:这个类也是从网上找的参考 
  9. */  
  10. public class SyncHorizontalScrollView extends HorizontalScrollView {  
  11.       
  12.     private View mView;  
  13.       
  14.     public SyncHorizontalScrollView(Context context) {  
  15.         super(context);  
  16.     }  
  17.       
  18.     public SyncHorizontalScrollView(Context context, AttributeSet attrs) {  
  19.         super(context, attrs);  
  20.     }  
  21.    
  22.     protected void onScrollChanged(int l, int t, int oldl, int oldt) {  
  23.         super.onScrollChanged(l, t, oldl, oldt);  
  24.         //设置控件滚动监听,得到滚动的距离,然后让传进来的view也设置相同的滚动具体  
  25.         if(mView!=null) {  
  26.             mView.scrollTo(l, t);  
  27.         }  
  28.     }  
  29.       
  30.     /** 
  31.     * 设置跟它联动的view 
  32.     * @param view 
  33.     */  
  34.     public void setScrollView(View view) {  
  35.         mView = view;  
  36.     }  
  37. }  

通过重写HorizontalScrollViewonScrollChanged方法使用scrollto实现联动,其实上面这个功能主要就在于这里,如果这里实现了,后面的就so easy,后面的可看可不看,贴一下代码,方便理解

主布局文件:

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <!-- 此部分是标题部分 -->  
  8.   
  9.     <LinearLayout  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:orientation="horizontal" >  
  13.   
  14.         <!-- 左侧标题的父容器 -->  
  15.   
  16.         <LinearLayout  
  17.             android:id="@+id/left_title_container"  
  18.             android:layout_width="match_parent"  
  19.             android:layout_height="wrap_content"  
  20.             android:layout_weight="3"  
  21.             android:orientation="vertical" >  
  22.   
  23.             <include layout="@layout/layout_left_title" />  
  24.         </LinearLayout>  
  25.   
  26.         <!-- 右侧标题的父容器可实现水平滚动 -->  
  27.   
  28.         <com.thea.guo.leftrightscrool.view.SyncHorizontalScrollView  
  29.             android:id="@+id/title_horsv"  
  30.             android:layout_width="match_parent"  
  31.             android:layout_height="wrap_content"  
  32.             android:layout_weight="1"  
  33.             android:scrollbars="@null" >  
  34.   
  35.             <LinearLayout  
  36.                 android:id="@+id/right_title_container"  
  37.                 android:layout_width="match_parent"  
  38.                 android:layout_height="wrap_content"  
  39.                 android:orientation="horizontal" >  
  40.   
  41.                 <include layout="@layout/layout_right_tab" />  
  42.             </LinearLayout>  
  43.         </com.thea.guo.leftrightscrool.view.SyncHorizontalScrollView>  
  44.     </LinearLayout>  
  45.   
  46.     <!-- 此部分是内容部分 用ScrollView实现上下滚动效果 -->  
  47.   
  48.     <ScrollView  
  49.         android:layout_width="match_parent"  
  50.         android:layout_height="wrap_content" >  
  51.   
  52.         <LinearLayout  
  53.             android:layout_width="match_parent"  
  54.             android:layout_height="match_parent" >  
  55.   
  56.             <!-- 左侧内容的父容器 -->  
  57.   
  58.             <LinearLayout  
  59.                 android:id="@+id/left_container"  
  60.                 android:layout_width="match_parent"  
  61.                 android:layout_height="match_parent"  
  62.                 android:layout_weight="3"  
  63.                 android:gravity="center_vertical"  
  64.                 android:orientation="vertical" >  
  65.   
  66.                 <ListView  
  67.                     android:id="@+id/left_container_listview"  
  68.                     android:layout_width="match_parent"  
  69.                     android:layout_height="match_parent" >  
  70.                 </ListView>  
  71.             </LinearLayout>  
  72.   
  73.             <!-- 右侧内容的父容器 实现水平滚动 -->  
  74.   
  75.             <com.thea.guo.leftrightscrool.view.SyncHorizontalScrollView  
  76.                 android:id="@+id/content_horsv"  
  77.                 android:layout_width="match_parent"  
  78.                 android:layout_height="wrap_content"  
  79.                 android:layout_weight="1"  
  80.                 android:scrollbars="@null" >  
  81.   
  82.                 <LinearLayout  
  83.                     android:id="@+id/right_container"  
  84.                     android:layout_width="match_parent"  
  85.                     android:layout_height="match_parent"  
  86.                     android:gravity="center_vertical"  
  87.                     android:orientation="horizontal" >  
  88.   
  89.                     <ListView  
  90.                         android:id="@+id/right_container_listview"  
  91.                         android:layout_width="match_parent"  
  92.                         android:layout_height="match_parent" >  
  93.                     </ListView>  
  94.                       
  95.                 </LinearLayout>  
  96.             </com.thea.guo.leftrightscrool.view.SyncHorizontalScrollView>  
  97.         </LinearLayout>  
  98.     </ScrollView>  
  99.   
  100. </LinearLayout>  

activity部分代码:

[java] view plain copy
  1. package com.thea.guo.leftrightscrool;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.app.Activity;  
  7. import android.graphics.Color;  
  8. import android.os.Bundle;  
  9. import android.widget.LinearLayout;  
  10. import android.widget.ListView;  
  11.   
  12. import com.thea.guo.leftrightscrool.adapter.MyLeftAdapter;  
  13. import com.thea.guo.leftrightscrool.adapter.MyRightAdapter;  
  14. import com.thea.guo.leftrightscrool.model.RightModel;  
  15. import com.thea.guo.leftrightscrool.tool.UtilTools;  
  16. import com.thea.guo.leftrightscrool.view.SyncHorizontalScrollView;  
  17.   
  18. public class TableActivity extends Activity {  
  19.     private LinearLayout leftContainerView;  
  20.     private ListView leftListView;  
  21.     private List<String> leftlList;  
  22.     private LinearLayout rightContainerView;  
  23.     private ListView rightListView;  
  24.     private List<RightModel> models;  
  25.     private SyncHorizontalScrollView titleHorsv;  
  26.     private SyncHorizontalScrollView contentHorsv;  
  27.   
  28.     @Override  
  29.     protected void onCreate(Bundle savedInstanceState) {  
  30.         super.onCreate(savedInstanceState);  
  31.         setContentView(R.layout.layout_tab_view);  
  32.         leftContainerView = (LinearLayout) findViewById(R.id.left_container);  
  33.         leftListView = (ListView) findViewById(R.id.left_container_listview);  
  34.         rightContainerView = (LinearLayout) findViewById(R.id.right_container);  
  35.         rightListView = (ListView) findViewById(R.id.right_container_listview);  
  36.         titleHorsv = (SyncHorizontalScrollView) findViewById(R.id.title_horsv);  
  37.         contentHorsv = (SyncHorizontalScrollView) findViewById(R.id.content_horsv);  
  38.         // 设置两个水平控件的联动  
  39.         titleHorsv.setScrollView(contentHorsv);  
  40.         contentHorsv.setScrollView(titleHorsv);  
  41.   
  42.         // 添加左边内容数据  
  43.         leftContainerView.setBackgroundColor(Color.YELLOW);  
  44.         initLeftData();  
  45.         MyLeftAdapter adapter=new MyLeftAdapter(this, leftlList);  
  46.         leftListView.setAdapter(adapter);  
  47.         UtilTools.setListViewHeightBasedOnChildren(leftListView);  
  48.         // 添加右边内容数据  
  49.         rightContainerView.setBackgroundColor(Color.GRAY);  
  50.         initRightData();  
  51.         MyRightAdapter myRightAdapter=new MyRightAdapter(this, models);  
  52.         rightListView.setAdapter(myRightAdapter);  
  53.         UtilTools.setListViewHeightBasedOnChildren(rightListView);  
  54.     }  
  55.   
  56.     /** 
  57.      * 初始化右侧listview数据 
  58.      */  
  59.     private void initRightData() {  
  60.         models=new ArrayList<RightModel>();  
  61.         ...  
  62.     }  
  63.   
  64.     /** 
  65.      * 初始左侧侧listview数据 
  66.      */  
  67.     private void initLeftData() {  
  68.         leftlList=new ArrayList<String>();  
  69.         ...  
  70.     }  
  71. }  

这里需要注意的是在ScrollView中使用ListView时,需要计算ListView子项目的高度,否则可能只给你显示一行

[java] view plain copy
  1. package com.thea.guo.leftrightscrool.tool;  
  2.   
  3. import android.view.View;  
  4. import android.view.ViewGroup;  
  5. import android.widget.ListAdapter;  
  6. import android.widget.ListView;  
  7.   
  8. public class UtilTools {  
  9.   
  10.     public static void setListViewHeightBasedOnChildren(ListView listView) {  
  11.         ListAdapter listAdapter = listView.getAdapter();  
  12.         if (listAdapter == null) {  
  13.                 return;  
  14.         }  
  15.         int totalHeight = 0;  
  16.         for (int i = 0, len = listAdapter.getCount(); i < len; i++) {  
  17.                 View listItem = listAdapter.getView(i, null, listView);  
  18.                 listItem.measure(0,0);  
  19.                 totalHeight += listItem.getMeasuredHeight();  
  20.         }  
  21.               
  22.         ViewGroup.LayoutParams params = listView.getLayoutParams();  
  23.         params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));  
  24.         listView.setLayoutParams(params);  
  25. }  
  26. }  

好了,就扯这么多


代码下载地址:http://download.csdn.net/detail/elinavampire/8297359

github托管地址:https://github.com/zimoguo/LeftRightScroll







4
0

相关文章:

  • 2021-06-09
  • 2021-08-03
  • 2021-11-07
  • 2021-12-04
  • 2021-12-03
  • 2021-10-23
  • 2021-11-18
  • 2022-12-23
猜你喜欢
  • 2021-06-06
  • 2021-10-23
  • 2022-12-23
  • 2022-02-05
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案