gpt4 book ai didi

android - 我想像在 Play 商店安装的应用程序 fragment 中创建一个 ListView

转载 作者:行者123 更新时间:2023-11-29 17:00:51 27 4
gpt4 key购买 nike

我想创建一个 ListView ,它在一些 ListView 项目的顶部显示一些细节(在我的例子中是批处理名称),例如 play store 的屏幕截图.这里它显示了在一些项目和更新之上的下载或安装在其他项目上。我该怎么做?

最佳答案

创建RecyclerView的官方引用:

Creating Lists and Cards

另外,你可以按照这个:

Using lists and grids in Android with RecylerView - Tutorial

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

下面是一个使用header创建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);
}
}
}

关于android - 我想像在 Play 商店安装的应用程序 fragment 中创建一个 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43087892/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com