RecyclerView Inside Adapter刷新后不可见

时间:2018-11-12 01:38:41

标签: java android android-fragments android-recyclerview android-adapter

在RV中看不见的东西似乎是一个普遍的问题,但是我已经进行了大量的搜索,无法弄清楚出了什么问题。

我有一个片段,其中包含适配器的RecyclerView。每个适配器内都有另一个RecyclerView,用于显示标签或类别(适配器)。

当我滑动以刷新主片段时,适配器的顶层显示正常。但是里面的RecyclerView消失了。我正在通知数据集更改,并且已经检查以确保在刷新(确实是)时确实将数据发送到了适配器。

Before refresh

After refresh

顶级片段:

public class AdminReportCardsFragment extends Fragment {
RecyclerView rv;
AdminReportCardAdapter adapter;
SwipeRefreshLayout swipeContainer;

//Filter Option Data
private List<String> newCategories;

public AdminReportCardsFragment(){

}

@Override
public View onCreateView(@NonNull LayoutInflater flater, ViewGroup tainer, Bundle savedInstanceState){
    View v = flater.inflate(R.layout.fragment_admin_reportcards, tainer, false);

    adapter = new AdminReportCardAdapter(getContext());

    rv = v.findViewById(R.id.admin_reports_rv);

    adapter.updateReports(Client.reportMap.values());

    rv.setAdapter(adapter);
    LinearLayoutManager llm = new LinearLayoutManager(getContext());
    llm.setOrientation(LinearLayoutManager.VERTICAL);
    rv.setLayoutManager(llm);

    swipeContainer = v.findViewById(R.id.admin_reports_sr);
    swipeContainer.setOnRefreshListener(this::actionSwipeRefresh);

    return v;
}

public void refreshReports() {
    //network stuff, callback is this line
        adapter.updateReports(Client.reportMap.values());
}

private void actionSwipeRefresh() {
    swipeContainer.setRefreshing(true);
    {
        refreshReports();
    }
    if(swipeContainer.isRefreshing())
        swipeContainer.setRefreshing(false);
}

顶级RV适配器:

public class AdminReportCardAdapter extends RecyclerView.Adapter<AdminReportCardAdapter.ReportViewHolder>{
Context ctx;
List<Report> data;

public AdminReportCardAdapter(Context c){
    ctx = c;
    data = new ArrayList<>();
}

public void updateReports(Collection<Report> c){
    data.clear();
    data.addAll(c);
    notifyDataSetChanged();
}

@NonNull
@Override
public AdminReportCardAdapter.ReportViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(ctx).inflate(R.layout.adapter_reportcard, parent, false);
    ReportViewHolder vh = new ReportViewHolder(v);
    LinearLayoutManager llm = new LinearLayoutManager(ctx);
    llm.setOrientation(LinearLayoutManager.HORIZONTAL);
    vh.catAdapter = new CategoryTagAdapter(false);
    vh.rv.setAdapter(vh.catAdapter);
    vh.rv.setLayoutManager(llm);

    return vh;
}

@Override
public void onBindViewHolder(@NonNull ReportViewHolder holder, int position) {
    Report r = data.get(position);
    holder.tvReportID.setText(r.reportId);
    holder.tvStatus.setText(r.status);
    holder.tvSubmitted.setText(Util.formatTimestamp(r.creationTimestamp));

    holder.catAdapter.updateCategories(r.categories);

    holder.cardContainer.setOnClickListener(v -> {
        Client.activeReport = data.get(holder.getAdapterPosition());
        ctx.startActivity(new Intent(ctx, AdminReportDetailsActivity.class));
    });
}

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

class ReportViewHolder extends RecyclerView.ViewHolder {
    RecyclerView rv;
    CategoryTagAdapter catAdapter;
    CardView cardContainer;
    TextView tvReportID, tvSubmitted, tvStatus;

    public ReportViewHolder(View v) {
        super(v);
        cardContainer = itemView.findViewById(R.id.reportcard_cv);
        tvReportID = itemView.findViewById(R.id.reportcard_alt_id);
        tvStatus = itemView.findViewById(R.id.reportcard_alt_status);
        tvSubmitted = itemView.findViewById(R.id.reportcard_alt_action);
        rv = itemView.findViewById(R.id.reportcard_rv_categories);
    }
}
}

最后,神秘地消失的内部适配器...

public class CategoryTagAdapter extends RecyclerView.Adapter<CategoryTagAdapter.CategoryTagViewHolder>{
int layoutID;
private List<String> categoryList;

public CategoryTagAdapter(Boolean isTag){
    categoryList = new ArrayList<>();
    if(!isTag) layoutID = R.layout.adapter_category;
    else layoutID = R.layout.adapter_tag;
}

@NonNull
@Override
public CategoryTagViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(layoutID, parent, false);
    return new CategoryTagViewHolder(v, layoutID);
}

@Override
public void onBindViewHolder(@NonNull CategoryTagViewHolder holder, int position) {
    System.out.println("CAT BIND");
    holder.tv.setText(categoryList.get(position));
}

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

public void updateCategories(Collection<String> c){
    categoryList.clear();
    categoryList.addAll(c);
    notifyDataSetChanged();
}

class CategoryTagViewHolder extends RecyclerView.ViewHolder{
    TextView tv;

    public CategoryTagViewHolder(View itemView, int layoutID){
        super(itemView);
        if(layoutID == R.layout.adapter_category)
            tv = itemView.findViewById(R.id.adapter_alt_category);
        else
            tv = itemView.findViewById(R.id.adapter_alt_tag);
    }
}

我的布局通过ConstraintLayout格式化,并且确保Recyclerview被绑定。辅助适配器应该是其父代内部的变量吗?还是将其放置在视口中的适当位置?

0 个答案:

没有答案