Recently encountered a very disgusting problem, to achieve similar to the stock list linkage problem , the General mobile phone basically no need , This effect is generally used on the tablet , If the mobile phone with this effect that there is no chat , I can only hehe ...
Let 's start with The following section , which is easy to understand :
First to illustrate this effect , first move up and down through the ScrollView to realize the left and right side of the sliding , the RH ListView by rewriting the Horizontalscrollview to achieve simultaneous linkage of the right and bottom parts
Here's the main code :
package com.thea.guo.leftrightscrool.view;import Android.content.context;import Android.util.attributeset;import android.view.view;import android.widget.horizontalscrollview;/*** @Description: This class is also a reference from the Internet */public class Synchorizontalscrollview extends Horizontalscrollview {private View Mview;public Synchorizontalscrollview (Context context) {super (context);} Public Synchorizontalscrollview (context context, AttributeSet Attrs) {Super (context, attrs);} protected void onscrollchanged (int l, int t, int oldl, int Oldt) {super.onscrollchanged (L, T, OLDL, Oldt);//Set the control to scroll through the listening, get the scrolling distance, and then let the incoming view Also set the same scrolling specific if (mview!=null) {Mview.scrollto (L, t);}} /*** sets the view* @param view*/public void Setscrollview (view view) {MView = view;}} that is linked to it;
using Scrollto to implement linkage by overriding the Onscrollchanged method of Horizontalscrollview , In fact, the above function mainly lies here , If this is achieved , the back of so Easy , the back is not to be looked at , Paste the code . , Easy to understand
Main layout file:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" vertical " > <!--This section is the title section--<linearlayout android:layout_width= "Match_parent" Android:layout_heigh t= "wrap_content" android:orientation= "Horizontal" > <!--the parent container of the left caption--<linearlayout Android:id= "@+id/left_title_container" android:layout_width= "Match_parent" Android:layout_heig ht= "Wrap_content" android:layout_weight= "3" android:orientation= "vertical" > <inclu De layout= "@layout/layout_left_title"/> </LinearLayout> < The parent container for the right title of the!--can be scrolled horizontally--< Com.thea.guo.leftrightscrool.view.SyncHorizontalScrollView android:id= "@+id/title_horsv" Android:lay Out_width= "Match_parent" android:layout_height= "Wrap_content" android:layout_weight= "1" android:scrollbars= "@null" > <linearlayout android:id= "@+id/right_title_container" android:layout_width= "Match_ Parent "android:layout_height=" wrap_content "android:orientation=" Horizontal "> <include layout= "@layout/layout_right_tab"/> </LinearLayout> </com.thea.guo.leftri Ghtscrool.view.synchorizontalscrollview> </LinearLayout> <!--This section is the Content section with ScrollView to achieve up and down scrolling effects--< ; ScrollView android:layout_width= "match_parent" android:layout_height= "wrap_content" > <linearla Yout android:layout_width= "match_parent" android:layout_height= "match_parent" > <!- -Parent container for content on the left--<linearlayout android:id= "@+id/left_container" android:layou T_width= "Match_parent" android:layout_height= "Match_parent" android:layout_weight= "3" Android:gravit Y= "center_vertical" android:orientation= "vertical" > <listview Android Oid:id= "@+id/left_container_listview" android:layout_width= "Match_parent" Android:la yout_height= "Match_parent" > </ListView> </LinearLayout> <!--the right side of the content The parent container implements horizontal scrolling--<com.thea.guo.leftrightscrool.view.synchorizontalscrollview android:id= "@+i D/CONTENT_HORSV "android:layout_width=" match_parent "android:layout_height=" Wrap_content " android:layout_weight= "1" android:scrollbars= "@null" > <linearlayout Android:id= "@+id/right_container" android:layout_width= "Match_parent" Android:layout_height= "Match_parent "android:gravity=" center_vertical "android:orientation=" Horizontal " ; <listview android:id= "@+id/right_container_listview" android:layout_width = "Match_parent" android:layout_height= "match_parent" > </ListView> </LinearLayout> </com.thea.guo.leftrightscrool.view.synchorizontalscrol Lview> </LinearLayout> </ScrollView></LinearLayout>
Activity Section Code:
Package Com.thea.guo.leftrightscrool;import Java.util.arraylist;import Java.util.list;import android.app.Activity; Import Android.graphics.color;import Android.os.bundle;import Android.widget.linearlayout;import Android.widget.listview;import Com.thea.guo.leftrightscrool.adapter.myleftadapter;import Com.thea.guo.leftrightscrool.adapter.myrightadapter;import Com.thea.guo.leftrightscrool.model.rightmodel;import Com.thea.guo.leftrightscrool.tool.utiltools;import Com.thea.guo.leftrightscrool.view.SyncHorizontalScrollView; public class Tableactivity extends Activity {private linearlayout leftcontainerview;private ListView Leftlistview; Private list<string> leftllist;private linearlayout rightcontainerview;private ListView rightlistview;private list<rightmodel> models;private Synchorizontalscrollview titlehorsv;private SyncHorizontalScrollView CONTENTHORSV; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.layout_tAb_view); Leftcontainerview = (linearlayout) Findviewbyid (r.id.left_container); Leftlistview = (ListView) findViewById (r.id.left_container_listview); Rightcontainerview = (linearlayout) Findviewbyid (R.id.right_container); Rightlistview = (ListView) Findviewbyid (r.id.right_container_listview); TITLEHORSV = (Synchorizontalscrollview) Findviewbyid (R.ID.TITLE_HORSV); CONTENTHORSV = (Synchorizontalscrollview) Findviewbyid (R.ID.CONTENT_HORSV);// Set two horizontal control linkage titlehorsv.setscrollview (CONTENTHORSV); Contenthorsv.setscrollview (TITLEHORSV);// Add left Content Data Leftcontainerview.setbackgroundcolor (color.yellow); Initleftdata (); Myleftadapter adapter=new Myleftadapter (this, leftllist); Leftlistview.setadapter (adapter); Utiltools.setlistviewheightbasedonchildren (Leftlistview);//Add the right content data Rightcontainerview.setbackgroundcolor ( Color.gray); Initrightdata (); Myrightadapter myrightadapter=new Myrightadapter (this, models); Rightlistview.setadapter (Myrightadapter); Utiltools.setlistviewheightbasedonchildren (Rightlistview);} /** * InitializationRight-side listview data */private void Initrightdata () {models=new arraylist<rightmodel> (); ...} /** * Initial left side listview data */private void Initleftdata () {leftllist=new arraylist<string> (); ...}}
It is important to note that when you use a ListView in ScrollView, you need to calculate the height of the ListView subproject, or you may show only one row
Package Com.thea.guo.leftrightscrool.tool;import Android.view.view;import Android.view.viewgroup;import Android.widget.listadapter;import Android.widget.listview;public class Utiltools {public static void Setlistviewheightbasedonchildren (ListView listview) { ListAdapter listadapter = Listview.getadapter (); if (ListAdapter = = null) { return; } int totalheight = 0; for (int i = 0, Len = Listadapter.getcount (), i < Len; i++) { View ListItem = Listadapter.getview (i, NULL, ListView ); Listitem.measure (0,0); Totalheight + = Listitem.getmeasuredheight (); } Viewgroup.layoutparams params = Listview.getlayoutparams (); Params.height = Totalheight + (listview.getdividerheight () * (Listadapter.getcount ()-1)); Listview.setlayoutparams (params);}}
All right, that's all.
Code: http://download.csdn.net/detail/elinavampire/8297359
GitHub Managed Address: Https://github.com/zimoguo/LeftRightScroll
Android implementation of flat class stock list linkage