java Android ListView头文件
我有一个ListView,上面有一些事件。事件是按天排序的,我希望每天都有一个带有日期的标题,然后听下面的事件
下面是我如何填充该列表的:
ArrayList<TwoText> crs = new ArrayList<TwoText>();
crs.add(new TwoText("This will be header", event.getDate()));
for (Event event : events) {
crs.add(new TwoText(event.getStartString() + "-" + event.getEndString(), event.getSubject()));
}
arrayAdapter = new TwoTextArrayAdapter(this, R.layout.my_list_item, crs);
lv1.setAdapter(arrayAdapter);
这就是我的课文的样子:
public class TwoText {
public String classID;
public String state;
public TwoText(String classID, String state) {
this.classID = classID;
this.state = state;
}
}
这就是我的TwoTextArrayAdapter类的外观:
import java.util.ArrayList;
import 安卓.app.Activity;
import 安卓.content.Context;
import 安卓.view.LayoutInflater;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓.widget.ArrayAdapter;
import 安卓.widget.TextView;
public class TwoTextArrayAdapter extends ArrayAdapter<TwoText> {
private ArrayList<TwoText> classes;
private Activity con;
TextView seperator;
public TwoTextArrayAdapter(Activity context, int textViewResourceId, ArrayList<TwoText> classes) {
super(context, textViewResourceId, classes);
this.con = context;
this.classes = classes;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.my_list_item, null);
}
TwoText user = classes.get(position);
if (user != null) {
TextView content1 = (TextView) v.findViewById(R.id.list_content1);
TextView content2 = (TextView) v.findViewById(R.id.list_content2);
if (content1 != null) {
content1.setText(user.classID);
}
if(content2 != null) {
content2.setText(user.state);
}
}
return v;
}
}
这是我的清单。xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:orientation="vertical" >
<TextView
style="?安卓:attr/listSeparatorTextViewStyle"
安卓:id="@+id/separator"
安卓:text="Header"
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:background="#757678"
安卓:textColor="#f5c227" />
<LinearLayout
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:orientation="horizontal" >
<TextView
安卓:id="@+id/list_content1"
安卓:layout_width="wrap_content"
安卓:layout_height="match_parent"
安卓:layout_margin="5dip"
安卓:clickable="false"
安卓:gravity="center"
安卓:longClickable="false"
安卓:paddingBottom="1dip"
安卓:paddingTop="1dip"
安卓:text="sample"
安卓:textColor="#ff7f1d"
安卓:textSize="17dip"
安卓:textStyle="bold" />
<TextView
安卓:id="@+id/list_content2"
安卓:layout_width="wrap_content"
安卓:layout_height="match_parent"
安卓:layout_margin="5dip"
安卓:clickable="false"
安卓:gravity="center"
安卓:linksClickable="false"
安卓:longClickable="false"
安卓:paddingBottom="1dip"
安卓:paddingTop="1dip"
安卓:text="sample"
安卓:textColor="#6d6d6d"
安卓:textSize="17dip" />
</LinearLayout>
</LinearLayout>
我现在所做的是,我添加的标题只是作为常规列表对象,但我希望它作为标题,在我的情况下,它有一个日期
我的xml文件头中有以下代码:
<TextView
style="?安卓:attr/listSeparatorTextViewStyle"
安卓:id="@+id/separator"
安卓:text="Header"
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:background="#757678"
安卓:textColor="#f5c227" />
我试着在不必要的时候隐藏它,在必要的时候显示它,但是我把剩下的代码搞乱了。我尝试了更多的教程,但它们也有同样的效果
有谁能指导我如何用这种简单的方法吗
# 1 楼答案
Here is a sample project,基于antew详细而有用的回答,它实现了一个带有多个标题的
ListView
,其中包含视图持有者,以提高滚动性能在这个项目中,
ListView
中表示的对象是类HeaderItem
或类RowItem
的实例,两者都是抽象类Item
的子类。Item
的每个子类对应于自定义适配器ItemAdapter
中的不同视图类型。ItemAdapter
上的方法getView()
将每个列表项的视图的创建委托给HeaderItem
或RowItem
上的个体化getView()
方法,具体取决于在传递给适配器上getView()
方法的位置使用的Item
子类。每个Item
子类都提供自己的视图持有者视图持有者的实现如下所示。
Item
子类上的getView()
方法检查传递给ItemAdapter
上的getView()
方法的View
对象是否为空。如果是这样的话,适当的布局会膨胀,视图持有者对象会被实例化,并通过View.setTag()
与膨胀的视图关联。如果View
对象不为空,则视图持有者对象已经与视图关联,并且通过View.getTag()
检索视图持有者。视图持有者的使用方式可以在HeaderItem
的以下代码片段中看到:下面是ListView的完整实现。以下是Java代码:
还有两个列表项布局,每个项子类一个。以下是HeaderItem使用的布局
header
:下面是RowItem使用的布局
row
:下面是生成的ListView的一部分的图像:
# 2 楼答案
您可能正在寻找一个ExpandableListView,它有头(组)来分隔项目(child)
关于这个主题的不错的教程:here
# 3 楼答案
我是如何将日期(如2016年12月1日)作为标题的。 我使用了StickyHeaderListView库
https://github.com/emilsjolander/StickyListHeaders
将日期转换为长毫秒[不包括时间],并将其作为标题Id
# 4 楼答案
我是这样做的,键是
Adapter
类中的getItemViewType和getViewTypeCountgetViewTypeCount
返回列表中有多少类型的项,在本例中,我们有一个标题项和一个事件项,所以有两个getItemViewType
应该返回我们在输入position
中的View
类型然后,Android会自动在
convertView
中向您传递正确类型的View
下面是下面代码的结果:
首先,我们有一个接口,我们的两个列表项类型将实现该接口
然后我们有一个适配器,它接受
Item
编辑 性能更好。。滚动时可以注意到
然后我们让类实现
Item
并膨胀正确的布局。在你的例子中,你会有Header
类和ListItem
类然后是
ListItem
类和一个简单的
Activity
来显示它用于
R.layout.header
的布局用于
R.layout.my_list_item
的布局用于
R.layout.activity_main.xml
的布局你也可以变得更花哨,使用
ViewHolders
,异步加载,或者你喜欢的任何东西# 5 楼答案
作为替代方案,有一个很好的3rd party library专门为这个用例设计。因此,您需要根据存储在适配器中的数据生成标题。它们被称为Rolodex适配器,与
ExpandableListViews
一起使用。它们可以很容易地进行定制,使其行为类似于带有标题的普通列表使用OP的
Event
对象并知道头是基于与之关联的Date
。。。代码如下所示:活动
适配器