最近公司的项目中的一个模块做了一个和车来了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<