如何在Android中创建Accordion ListView?

时间:2015-08-30 11:30:25

标签: java android listview android-listview accordion

我知道如何在Android中创建ListView,但我需要在Android中使用某种手风琴列表视图。像这样:

enter image description here

手风琴列表应该有一个章节标题,并且在点击标题标题时应该切换。

如何构建这样的手风琴ListView?

4 个答案:

答案 0 :(得分:4)

您可以使用ExpandableListView。文档here

答案 1 :(得分:0)

答案 2 :(得分:0)

我最近通过Constraint layout建立了一个超轻型的手风琴清单。值得一看:

https://github.com/draxdave/ConstraintAccordion

enter image description here

答案 3 :(得分:-1)

为什么要使用ListView? Android现在有了一些名为RecyclerView的新功能,它发生在ListView上:https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html 以下是使用RecyclerView制作标题的方法:

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

 public class HeaderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;
String[] data;

public HeaderAdapter(String[] data) {
    this.data = data;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == TYPE_ITEM) {
        //inflate your layout and pass it to view holder
        return new VHItem(null);
    } else if (viewType == TYPE_HEADER) {
        //inflate your layout and pass it to view holder
        return new VHHeader(null);
    }

    throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    if (holder instanceof VHItem) {
        String dataItem = getItem(position);
        //cast holder to VHItem and set data
    } else if (holder instanceof VHHeader) {
        //cast holder to VHHeader and set data for header.
    }
}

@Override
public int getItemCount() {
    return data.length + 1;
}

@Override
public int getItemViewType(int position) {
    if (isPositionHeader(position))
        return TYPE_HEADER;

    return TYPE_ITEM;
}

private boolean isPositionHeader(int position) {
    return position == 0;
}

private String getItem(int position) {
    return data[position - 1];
}

class VHItem extends RecyclerView.ViewHolder {
    TextView title;

    public VHItem(View itemView) {
        super(itemView);
    }
}

class VHHeader extends RecyclerView.ViewHolder {
    Button button;

    public VHHeader(View itemView) {
        super(itemView);
    }
}

}

git上的链接:https://gist.github.com/hister/d56c00fb5fd2dfaf279b