java - 带标题的 Android 多行 ListView

标签 java android android-listview

我需要在 Android 中创建一个具有标题且多行的 ListView。我用它来显示作业标题和它下面的截止日期。作业在两个标题下排序:“即将到来的作业”和“过去的作业”(当然是根据日期)。

我的标题工作正常,作业标题显示正确,但我对如何将第二行合并到列表中感到困惑。

这是我的代码:

ListCourseAssignments.java

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    final SeparatedListAdapter adapter;
    setContentView(R.layout.courseassignmentslistview);
    adapter = new SeparatedListAdapter(getContext());
    ArrayAdapter<String> upcomingList = new ArrayAdapter<String>(getContext(), R.layout.list_item, Homework.getUpcomingDates()); // Homework.getUpcomingDates() Returns string array
    ArrayAdapter<String> pastList = new ArrayAdapter<String>(getContext(), R.layout.list_item, Homework.getPastDates()); // Homework.getPastDates() Returns string array

    adapter.addSection("Upcoming Assignments", upcomingList); 
    adapter.addSection("Past Assignments", pastList);

    ListView lv = (ListView) findViewById(android.R.id.list);
    lv.setAdapter(adapter);

    lv.setOnItemClickListener(new OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long duration){
            String item = (String) adapter.getItem(position);
            Toast.makeText(getApplicationContext(), item, Toast.LENGTH_SHORT).show();
        }
    });
}

SeparatedListAdapter.java

public class SeparatedListAdapter extends BaseAdapter {
public final Map<String, Adapter> sections = new LinkedHashMap<String, Adapter>();
public final ArrayAdapter<String> headers;

public SeparatedListAdapter(Context context) {
    headers = new ArrayAdapter<String>(context, R.layout.courseassignmentslistview_header);
}

public void addSection(String section, Adapter adapter) {
    this.headers.add(section);
    this.sections.put(section, adapter);
}

public Object getItem(int position) {
    for (Object section : this.sections.keySet()) {
        Adapter adapter = sections.get(section);
        int size = adapter.getCount() + 1;

        if (position == 0) return section;
        if (position < size) return adapter.getItem(position - 1);

        position -= size;
    }
    return null;
}

public int getCount() {
    int total = 0;
    for (Adapter adapter : this.sections.values()) total += adapter.getCount() + 1;
    return total;
}

@Override
public int getViewTypeCount() {
    int total = 1;
    for (Adapter adapter : this.sections.values()) total += adapter.getViewTypeCount();
    return total;
}

@Override
public int getItemViewType(int position) {
    int type = 1;
    for (Object section : this.sections.keySet()) {
        Adapter adapter = sections.get(section);
        int size = adapter.getCount() + 1;

        if (position == 0) return 0;
        if (position < size) return type + adapter.getItemViewType(position - 1);

        position -= size;
        type += adapter.getViewTypeCount();
    }
    return -1;
}

public boolean areAllItemsSelectable() {
    return false;
}

@Override
public boolean isEnabled(int position) {
    return (getItemViewType(position) != 0);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    int sectionnum = 0;
    for (Object section : this.sections.keySet()) {
        Adapter adapter = sections.get(section);
        int size = adapter.getCount() + 1;

        if (position == 0) return headers.getView(sectionnum, convertView, parent);
        if (position < size) return adapter.getView(position - 1, convertView, parent);

        position -= size;
        sectionnum++;
    }
    return null;
}

@Override
public long getItemId(int position) {
    return position;
}
}

类(class)作业列表view_header.xml

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list_header_title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="2dip"
    android:paddingBottom="2dip"
    android:paddingLeft="5dip"
    style="?android:attr/listSeparatorTextViewStyle" />

类(class)作业列表view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ListView
        android:id="@+android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

列表项.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16sp" >
</TextView>

Here's a screenshot of what it currently looks like.

编辑:我知道如何创建多行列表,但我只是不知道如何将它与带有标题的列表(例如这个)放在一起...

最佳答案

我不知道如何用你目前的东西来做,但你可以用 CommonWare 的 MergeAdapter 来做。

引用自述文件:

MergeAdapter accepts a mix of Adapters and Views and presents them as one contiguous 
whole to whatever ListView it is poured into. This is good for cases where you have
multiple data sources, or if you have a handful of ordinary Views to mix in with lists 
of data, or the like.

可以得到here .

我写了一个使用它的简要概述here

使用 MergeAdapter,您可以通过为它们提供 View 来使用它来制作您的节标题,然后设置您的适配器以创建您想要的两行格式。

关于java - 带标题的 Android 多行 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10712891/

相关文章:

java - Proximity Alert Intent 不放置或返回额外的东西

android - 如何获取用户的位置?

android - 我正在尝试在此 ListView 中添加图像

Android - ListView滚动太慢

java - 如何使用 createScreenCapture 仅捕获所需的屏幕部分

java - Action 监听器 : for each button in GUI a seperate listener or one listener for all buttons?

java - 如何防止在事务提交时保存到 Hibernate 中的数据库?

android - Eclipse 未检测到索尼爱立信 Xperia 进行调试?

android - ListView layout_weight 不工作,高度设置为 0dp

java - Firebase JSON 到 Arraylist 内的 Arraylist 存在对象问题