Android:包含层次数据的Recyclerview

时间:2018-03-13 03:52:45

标签: android android-recyclerview

我有这个POJO,它有点递归,因为它有一个自己的列表对象(子),如下所示。

public class Volume implements Serializable, Parcelable {

    @JsonProperty("avail")
    private Long avail;

    @JsonProperty("children")
    private List<Volume> children = null;

    @JsonProperty("id")
    private Long id;

    @JsonProperty("is_decrypted")
    private Boolean isDecrypted;

    @JsonProperty("is_upgraded")
    private Boolean isUpgraded;

    @JsonProperty("mountpoint")
    private String mountpoint;

    @JsonProperty("name")
    private String name;

    @JsonProperty("path")
    private String path;

    @JsonProperty("status")
    private String status;

    @JsonProperty("type")
    private String type;

    @JsonProperty("used")
    private Long used;

    @JsonProperty("used_pct")
    private String usedPct;

    @JsonProperty("vol_encrypt")
    private Long volEncrypt;

    @JsonProperty("vol_encryptkey")
    private String volEncryptkey;

    @JsonProperty("vol_guid")
    private String volGuid;

    @JsonProperty("vol_name")
    private String volName;
}

是否可以在与父母相同的列表/级别中显示其子级,无论其深度/级别如何?如果是这样,我该怎么办呢。

我已经使用普通适配器构建了代码,如此。

private class VolumeAdapter extends RecyclerView.Adapter<VolumeAdapter.VolumeHolder> {

    private List<Volume> volumeList = new ArrayList<>();

    @Override
    public VolumeHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new VolumeHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.list_volume, parent, false));
    }

    @Override
    public void onBindViewHolder(VolumeHolder holder, int position) {
        holder.volume = volumeList.get(position);
        holder.name.setText(holder.volume.getName());
        holder.mountpoint.setText(holder.volume.getMountpoint());
        holder.status.setText(holder.volume.getStatus());

        if (bytesManager.getLengthInGB(holder.volume.getUsed()).compareTo(BigDecimal.ZERO) == 0) {
            if (bytesManager.getLengthInMB(holder.volume.getUsed()).compareTo(BigDecimal.ZERO) == 0) {
                if (bytesManager.getLengthInKB(holder.volume.getUsed()).compareTo(BigDecimal.ZERO) == 0) {
                    holder.used.setText(String.format(Locale.US, "%.1f %s (%s)", holder.volume.getUsed(), "B", holder.volume.getUsedPct()));
                } else {
                    holder.used.setText(String.format(Locale.US, "%.1f %s (%s)", bytesManager.getLengthInKB(holder.volume.getUsed()), "KB", holder.volume.getUsedPct()));
                }
            } else {
                holder.used.setText(String.format(Locale.US, "%.1f %s (%s)", bytesManager.getLengthInMB(holder.volume.getUsed()), "MB", holder.volume.getUsedPct()));
            }
        } else {
            holder.used.setText(String.format(Locale.US, "%.1f %s (%s)", bytesManager.getLengthInGB(holder.volume.getUsed()), "GB", holder.volume.getUsedPct()));
        }

        if (bytesManager.getLengthInGB(holder.volume.getAvail()).compareTo(BigDecimal.ZERO) == 0) {
            if (bytesManager.getLengthInMB(holder.volume.getAvail()).compareTo(BigDecimal.ZERO) == 0) {
                if (bytesManager.getLengthInKB(holder.volume.getAvail()).compareTo(BigDecimal.ZERO) == 0) {
                    holder.avail.setText(String.format(Locale.US, "%.1f %s", holder.volume.getAvail(), "B"));
                } else {
                    holder.avail.setText(String.format(Locale.US, "%.1f %s", bytesManager.getLengthInKB(holder.volume.getAvail()), "KB"));
                }
            } else {
                holder.avail.setText(String.format(Locale.US, "%.1f %s", bytesManager.getLengthInMB(holder.volume.getAvail()), "MB"));
            }
        } else {
            holder.avail.setText(String.format(Locale.US, "%.1f %s", bytesManager.getLengthInGB(holder.volume.getAvail()), "GB"));
        }
    }

    @Override
    public int getItemCount() {
        return volumeList.size();
    }

    public class VolumeHolder extends RecyclerView.ViewHolder {
        public final View view;
        public final TextView name, mountpoint, status, used, avail;
        public Volume volume;

        public VolumeHolder(View itemView) {
            super(itemView);
            view = itemView;
            name = itemView.findViewById(R.id.name);
            mountpoint = itemView.findViewById(R.id.mountpoint);
            status = itemView.findViewById(R.id.status);
            used = itemView.findViewById(R.id.used);
            avail = itemView.findViewById(R.id.avail);
        }
    }

}

但我需要更改它以支持子对象。

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我认为您需要的解决方案是一个递归函数,如下所示。

Volume

我在public class Volume implements Serializable { @JsonProperty("avail") public Long avail; @JsonProperty("children") public List<Volume> children = null; @JsonProperty("id") public Long id; @JsonProperty("is_decrypted") public Boolean isDecrypted; @JsonProperty("is_upgraded") public Boolean isUpgraded; @JsonProperty("mountpoint") public String mountpoint; @JsonProperty("name") public String name; @JsonProperty("path") public String path; @JsonProperty("status") public String status; @JsonProperty("type") public String type; @JsonProperty("used") public Long used; @JsonProperty("used_pct") public String usedPct; @JsonProperty("vol_encrypt") public Long volEncrypt; @JsonProperty("vol_encryptkey") public String volEncryptkey; @JsonProperty("vol_guid") public String volGuid; @JsonProperty("vol_name") public String volName; public Volume() { } public Volume(Volume volume) { this.avail = volume.avail; this.id = volume.id; this.isDecrypted = volume.isDecrypted; this.isUpgraded = volume.isUpgraded; this.mountpoint = volume.mountpoint; this.name = volume.name; this.path = volume.path; this.status = volume.status; this.type = volume.type; this.used = volume.used; this.usedPct = volume.usedPct; this.volEncrypt = volume.volEncrypt; this.volEncryptkey = volume.volEncryptkey; this.volGuid = volume.volGuid; this.volName = volume.volName; } } 课程中添加了一个构造函数,以使其正常工作。

select areaID from table1 
where itemID in (3, 4) 
group by areaID
having count(distinct itemID) = 2

希望有所帮助!