使用HorizontalListView仿车来了公交时刻表

Posted wukj_litai

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用HorizontalListView仿车来了公交时刻表相关的知识,希望对你有一定的参考价值。

最近公司的项目中的一个模块做了一个和车来了app的公交时刻表相似的功能,里面使用了一个水平的ListView 来显示在不同车站上车辆的运行情况,公司采用网页做的,今天周六,我使用Listview来实现下,下面上图,给大家看下效果,最近也不知道咋的了,我写博客上传图片一直不能成功,我会尽快找到办法,成功上传图片。

开始吧,做这个我们会使用到水平的ListView,这个我直接上GitHub,在上面找了一个评价最好的,下载下来看了下,发现效果还行就直接用了。我们在水平的Listview中设置的item 就是每站的车辆情况和车站的信息,为了做成和车来了很相似,我给Item设置了3种样式,其实也可是直接使用一种,然后根据情况来决定其中一些View 的显示和不显示来完成。在下面我会贴出关键的代码,对其中的重要代码进行解释,文章的最后我会上传这个Demo的代码,是AS的。

activity_main 主页面布局文件:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:widget="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:padding="5dp"
            android:text="公交路线"
            android:textSize="18sp" />

        <com.meetme.android.horizontallistview.HorizontalListView
            android:id="@+id/hlvSimpleList"
            android:layout_width="match_parent"
            android:layout_height="300dp" />
    </LinearLayout>

</ScrollView>

MainActivity 的代码:

package com.example.dell.myhlistview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;


import com.meetme.android.horizontallistview.HorizontalListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity 

    private HorizontalListView hlvSimpleList; // 水平的ListView
    private  ArrayList<String> arrayList;
    private  BusAdapter busAdapter; // 水平的Listview的adapter
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        hlvSimpleList = (HorizontalListView)findViewById(R.id.hlvSimpleList);
        // 在List中添加车站的名称信息
        arrayList = new ArrayList<String>();
        arrayList.add("三森美居广场");
        arrayList.add("三森家具广场");
        arrayList.add("雁塔交警大队");
        arrayList.add("书刊批发市场");
        arrayList.add("日杂市场");
        arrayList.add("雁滩路西口");
        arrayList.add("滩尖子");
        arrayList.add("宁卧庄宾馆");
        arrayList.add("南昌路十字");
        arrayList.add("盘旋路西口");
        busAdapter = new BusAdapter(this,arrayList);
        hlvSimpleList.setAdapter(busAdapter);


    


适配器的代码,BusAdapter:

package com.example.dell.myhlistview;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by wukj on 2016/10/20.
 */

public class BusAdapter extends BaseAdapter 

    private LayoutInflater layoutInflater;
    private Context context;
    private ArrayList<String> arrayList;
    final int VIEW_TYPE = 3;
    final int TYPE_1 = 0;
    final int TYPE_2 = 1;
    final int TYPE_3 = 2;

    public BusAdapter(Context context, ArrayList<String> arrayList) 
        this.context = context;
        this.arrayList = arrayList;
        layoutInflater = LayoutInflater.from(context);

    

    @Override
    public int getCount() 

        return arrayList.size();
    

    @Override
    public Object getItem(int position) 

        return arrayList.get(position);
    

    @Override
    public long getItemId(int position) 

        return position;
    


    @Override
    public int getViewTypeCount() 
        return VIEW_TYPE;
    

    @Override
    public int getItemViewType(int position) 


        if (position == 0) 
            return TYPE_1;
         else 
            if (position == arrayList.size() - 1) 
                return TYPE_3;
             else 
                return TYPE_2;
            

        

    


    @Override
    public View getView(int position, View convertView, ViewGroup parent) 

        ViewHolderFirst viewHolderFirst = null;
        ViewHolderSecond viewHolderSecond = null;
        ViewHolderThird viewHolderThird = null;
        int type = getItemViewType(position);

        if (convertView == null) 
            switch (type) 
                case TYPE_1:
                    convertView = layoutInflater.inflate(R.layout.adapter_item_first, null);
                    viewHolderFirst = new ViewHolderFirst();
                    viewHolderFirst.serialNumber = (TextView) convertView.findViewById(R.id.numberTextview);
                    viewHolderFirst.addressName = (TextView) convertView.findViewById(R.id.addressTextview);
                    convertView.setTag(viewHolderFirst);
                    break;
                case TYPE_2:
                    convertView = layoutInflater.inflate(R.layout.adapter_item_second, null);
                    viewHolderSecond = new ViewHolderSecond();
                    viewHolderSecond.serialNumber = (TextView) convertView.findViewById(R.id.numberTextview);
                    viewHolderSecond.addressName = (TextView) convertView.findViewById(R.id.addressTextview);
                    convertView.setTag(viewHolderSecond);
                    break;
                case TYPE_3:
                    convertView = layoutInflater.inflate(R.layout.adapter_item_third, null);
                    viewHolderThird = new ViewHolderThird();
                    viewHolderThird.serialNumber = (TextView) convertView.findViewById(R.id.numberTextview);
                    viewHolderThird.addressName = (TextView) convertView.findViewById(R.id.addressTextview);
                    convertView.setTag(viewHolderThird);

                    break;
                default:
                    break;
            

         else 
            switch (type) 
                case TYPE_1:
                    viewHolderFirst = (ViewHolderFirst) convertView.getTag();
                    break;
                case TYPE_2:
                    viewHolderSecond = (ViewHolderSecond) convertView.getTag();
                    break;
                case TYPE_3:
                    viewHolderThird = (ViewHolderThird) convertView.getTag();
                    break;
                default:
                    break;
            
        

        switch (type) 
            case TYPE_1:
                if (position + 1 < 10) 
                    // 设置值得时候,当值小于10,即是1位数的时候,  前面设置空,使数字正好和下面的车站名称对齐
                    viewHolderFirst.serialNumber.setText(" " + String.valueOf(position + 1));

                 else 
                    viewHolderFirst.serialNumber.setText(String.valueOf(position + 1));

                
                viewHolderFirst.addressName.setText(arrayList.get(position));
                break;
            case TYPE_2:
                if (position + 1 < 10) 
                    viewHolderSecond.serialNumber.setText(" " + String.valueOf(position + 1));

                 else 
                    viewHolderSecond.serialNumber.setText(String.valueOf(position + 1));

                
                viewHolderSecond.addressName.setText(arrayList.get(position));
                break;
            case TYPE_3:
                if (position + 1 < 10) 
                    viewHolderThird.serialNumber.setText(" " + String.valueOf(position + 1));

                 else 
                    viewHolderThird.serialNumber.setText(String.valueOf(position + 1));

                
                viewHolderThird.addressName.setText(arrayList.get(position));
                break;
            default:
                break;

        

        return convertView;
    


    class ViewHolderFirst 
        TextView serialNumber;
        TextView addressName;
    

    class ViewHolderSecond 
        TextView serialNumber;
        TextView addressName;
    

    class ViewHolderThird 
        TextView serialNumber;
        TextView addressName;
    

item的第一种格式布局 adapter_item_first:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="100dp"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/pointImageview"
        android:layout_width="20dp"
        android:layout_height="15dp"
        android:src="@drawable/point_bus"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="42dp" />

    <ImageView
        android:layout_width="40dp"
        android:layout_height="15dp"
        android:layout_marginTop="42dp"
        android:layout_toRightOf="@+id/pointImageview"
        android:background="@drawable/line_bus_right"/>

    <TextView
        android:id="@+id/numberTextview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="1"
        android:text=" 1"
        android:layout_marginTop="20dp"
        android:layout_below="@+id/pointImageview"
        android:layout_centerHorizontal="true" />
    <TextView
        android:id="@+id/addressTextview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="1"
        android:text="三森美居广场"
        android:layout_marginTop="20dp"
        android:textSize="15sp"
        android:layout_below="@+id/numberTextview"
        android:layout_centerHorizontal="true"/>


</RelativeLayout>


item的第二中格式,adapter_item_second:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="100dp"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/leftImageview"
        android:layout_width="40dp"
        android:layout_height="15dp"
        android:background="@drawable/line_bus_left"
        android:layout_marginTop="42dp" />

    <ImageView
        android:id="@+id/pointImageview"
        android:layout_width="20dp"
        android:layout_height="15dp"
        android:src="@drawable/point_bus"
        android:layout_toRightOf="@+id/leftImageview"
        android:layout_marginTop="42dp" />

    <ImageView
        android:layout_width="40dp"
        android:layout_height="15dp"
        android:layout_marginTop="42dp"
        android:layout_toRightOf="@+id/pointImageview"
        android:background="@drawable/line_bus_right"/>

    <TextView
        android:id="@+id/numberTextview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="1"
        android:text=" 1"
        android:layout_marginTop="20dp"
        android:layout_below="@+id/pointImageview"
        android:layout_centerHorizontal="true" />
    <TextView
        android:id="@+id/addressTextview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="1"
        android:text="三森美居广场"
        android:layout_marginTop="20dp"
        android:textSize="15sp"
        android:layout_below="@+id/numberTextview"
        android:layout_centerHorizontal="true"/>


</RelativeLayout>


item 的第三种样式 adapter_item_third:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="100dp"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/leftImageview"
        android:layout_width="40dp"
        android:layout_height="15dp"
        android:background="@drawable/line_bus_left"
        android:layout_marginTop="42dp"
        />

    <ImageView
        android:id="@+id/pointImageview"
        android:layout_width="20dp"
        android:layout_height="15dp"
        android:src="@drawable/point_bus"
        android:layout_toRightOf="@+id/leftImageview"
        android:layout_marginTop="42dp" />

    <TextView
        android:id="@+id/numberTextview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="1"
        android:text=" 1"
        android:layout_marginTop="20dp"
        android:layout_below="@+id/pointImageview"
        android:layout_centerHorizontal="true" />
    <TextView
        android:id="@+id/addressTextview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="1"
        android:text="三森美居广场"
        android:layout_marginTop="20dp"
        android:textSize="15sp"
        android:layout_below="@+id/numberTextview"
        android:layout_centerHorizontal="true"/>


</RelativeLayout>

项目代码

以上是关于使用HorizontalListView仿车来了公交时刻表的主要内容,如果未能解决你的问题,请参考以下文章

公交神奇——“车来了”

燃爆了!北京亦庄这条路上,自动驾驶车来了!

LIO-SAM:从零开始的自车实时数据复现

横向滑动的HorizontalListView滑动指定位置的解决方法

梦笔记1221

梦笔记1221