我想在play store安装的应用程序片段中创建一个listview

时间:2017-03-29 08:10:47

标签: android listview

我想创建一个列表视图,它在一些listview项目的顶部显示一些详细信息(在我的情况下为批处理名称),如play store的屏幕截图。在这里,它显示了几个项目和更新之上的下载或安装在其他项目上。我怎么能这样做?

2 个答案:

答案 0 :(得分:2)

您创建RecyclerView的官方参考:

Creating Lists and Cards

另外,您可以按照以下步骤操作:

Using lists and grids in Android with RecylerView - Tutorial

考虑到设计模式,您将充分利用构建RecyclerView

以下是使用标题创建RecyclerView的示例:

    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);
        }
    }
}

答案 1 :(得分:1)

您可以使用CardView - 设计库。

在应用级Gradle中添加库 -

compile 'com.android.support:cardview-v7:25.0.+'

这里是卡片视图指南

https://developer.android.com/reference/android/support/v7/widget/CardView.html