为了账号安全,请及时绑定邮箱和手机立即绑定

Android ListView标头

Android ListView标头

呼唤远方 2019-08-09 15:18:14
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);这就是我的类TwoText看起来的样子: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 android.app.Activity;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ArrayAdapter;import android.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);         }我现在所做的是我正在添加标题,就像常规列表对象一样,但我喜欢它作为标题,在我的情况下有一个日期。我尝试在必要时隐藏它并在必要时显示它但我只是弄乱了我的其余代码。我尝试了更多的教程,但他们也有同样的效果。任何人都可以指导我如何做到这一点吗?
查看完整描述

3 回答

?
慕后森

TA贡献1802条经验 获得超5个赞

作为替代方案,有一个很好的第三方库专为此用例而设计。因此,您需要根据存储在适配器中的数据生成标头。它们被称为Rolodex适配器并与之一起使用ExpandableListViews。它们可以很容易地进行自定义,就像带有标题的普通列表一样。

使用OP的Event对象并知道标题是基于Date与之关联的...代码看起来像这样:

活动

    //There's no need to pre-compute what the headers are. Just pass in your List of objects. 
    EventDateAdapter adapter = new EventDateAdapter(this, mEvents);
    mExpandableListView.setAdapter(adapter);

适配器

private class EventDateAdapter extends NFRolodexArrayAdapter<Date, Event> {

    public EventDateAdapter(Context activity, Collection<Event> items) {
        super(activity, items);
    }

    @Override
    public Date createGroupFor(Event childItem) {
        //This is how the adapter determines what the headers are and what child items belong to it
        return (Date) childItem.getDate().clone();
    }

    @Override
    public View getChildView(LayoutInflater inflater, int groupPosition, int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        //Inflate your view

        //Gets the Event data for this view
        Event event = getChild(groupPosition, childPosition);

        //Fill view with event data
    }

    @Override
    public View getGroupView(LayoutInflater inflater, int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        //Inflate your header view

        //Gets the Date for this view
        Date date = getGroup(groupPosition);

        //Fill view with date data
    }

    @Override
    public boolean hasAutoExpandingGroups() {
        //This forces our group views (headers) to always render expanded.
        //Even attempting to programmatically collapse a group will not work.
        return true;
    }

    @Override
    public boolean isGroupSelectable(int groupPosition) {
        //This prevents a user from seeing any touch feedback when a group (header) is clicked.
        return false;
    }}


查看完整回答
反对 回复 2019-08-09
  • 3 回答
  • 0 关注
  • 429 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号