手记

Android实现列表联动

实现一个类似股票列表联动的功能,先上图:

一看也许会觉得很困难,上手之后逻辑还是很简单的。

1.首先上下滚动通过scrollview实现控制左右两侧同时滚动,右侧listview通过重写HorizontalScrollView实现右侧的标题部分跟内容能够同时联动

下面是布局文件

[代码]xml代码:

?

001

002

003

004

005

006

007

008

009

010

011

012

013

014

015

016

017

018

019

020

021

022

023

024

025

026

027

028

029

030

031

032

033

034

035

036

037

038

039

040

041

042

043

044

045

046

047

048

049

050

051

052

053

054

055

056

057

058

059

060

061

062

063

064

065

066

067

068

069

070

071

072

073

074

075

076

077

078

079

080

081

082

083

084

085

086

087

088

089

090

091

092

093

094

095

096

097

098

099

100

<?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" >

 

    <!-- 此部分是标题部分 -->

 

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal" >

 

        <!--   左侧标题的父容器   -->

 

        <LinearLayout

            android:id="@+id/left_title_container"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_weight="3"

            android:orientation="vertical" >

 

            <include layout="@layout/layout_left_title" />

        </LinearLayout>

 

        <!--   右侧标题的父容器可实现水平滚动 -->

 

        <com.thea.guo.leftrightscrool.view.SyncHorizontalScrollView

            android:id="@+id/title_horsv"

            android:layout_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.leftrightscrool.view.SyncHorizontalScrollView>

    </LinearLayout>

 

    <!-- 此部分是内容部分 用ScrollView实现上下滚动效果 -->

 

    <ScrollView

        android:layout_width="match_parent"

        android:layout_height="wrap_content" >

 

        <LinearLayout

            android:layout_width="match_parent"

            android:layout_height="match_parent" >

 

            <!--   左侧内容的父容器   -->

 

            <LinearLayout

                android:id="@+id/left_container"

                android:layout_width="match_parent"

                android:layout_height="match_parent"

                android:layout_weight="3"

                android:gravity="center_vertical"

                android:orientation="vertical" >

 

                <ListView

                    android:id="@+id/left_container_listview"

                    android:layout_width="match_parent"

                    android:layout_height="match_parent" >

                </ListView>

            </LinearLayout>

 

            <!--   右侧内容的父容器 实现水平滚动 -->

 

            <com.thea.guo.leftrightscrool.view.SyncHorizontalScrollView

                android:id="@+id/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.SyncHorizontalScrollView>

        </LinearLayout>

    </ScrollView>

 

</LinearLayout>

2.其中SyncHorizontalScrollView 就是重写的那个实现联动的HorizontalScrollView

[代码]xml代码:

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

package   com.thea.guo.leftrightscrool.view;

 

import android.content.Context;

import android.util.AttributeSet;

import android.view.View;

import   android.widget.HorizontalScrollView;

/**

* @Description:这个类也是从网上找的参考 

*/

public class SyncHorizontalScrollView   extends HorizontalScrollView {

     

    private View   mView;

     

    public   SyncHorizontalScrollView(Context context) {

        super(context);

        //   TODO Auto-generated constructor stub

    }

     

    public   SyncHorizontalScrollView(Context context, AttributeSet attrs) {

        super(context,   attrs);

        //   TODO Auto-generated constructor stub

    }

  

    protected void   onScrollChanged(int l, int t, int oldl, int oldt) {

        super.onScrollChanged(l,   t, oldl, oldt);

        //设置控件滚动监听,得到滚动的距离,然后让传进来的view也设置相同的滚动具体

        if(mView!=null)   {

            mView.scrollTo(l,   t);

        }

    }

     

    /**

    * 设置跟它联动的view

    * @param view

    */

    public void   setScrollView(View view) {

        mView   = view;

    }

}

3.最后就是Activity的逻辑处理

[代码]xml代码:

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

package com.thea.guo.leftrightscrool;

 

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;

 

import java.util.ArrayList;

import java.util.List;

 

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;

 

    @Override

    protected 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);

        //   设置两个水平控件的联动

        titleHorsv.setScrollView(contentHorsv);

        contentHorsv.setScrollView(titleHorsv);

 

        //  

        leftContainerView.setBackgroundColor(Color.YELLOW);

        initLeftData();

        MyLeftAdapter   adapter=new MyLeftAdapter(this, leftlList);

        leftListView.setAdapter(adapter);

        //控制高度

        UtilTools.setListViewHeightBasedOnChildren(leftListView);

        //  

        rightContainerView.setBackgroundColor(Color.GRAY);

        initRightData();

        MyRightAdapter   myRightAdapter=new MyRightAdapter(this, models);

        rightListView.setAdapter(myRightAdapter);

        //控制高度

        UtilTools.setListViewHeightBasedOnChildren(rightListView);

    }

 

    private void   initRightData() {

        models=new   ArrayList<RightModel>();

        for(int   i=0;i<30;i++){

            models.add(new   RightModel(i+"_0",i+"_1",i+"_2",i+"_3",i+"_4",i+"_5",i+"_6"));

        }

    }

 

    private void   initLeftData() {

        leftlList=new ArrayList<String>();

        for(int   i=0;i<30;i++){

            leftlList.add("Y_"+i);

        }

    }

}

原文链接:http://www.apkbus.com/blog-865765-62723.html

0人推荐
随时随地看视频
慕课网APP