0

在第一次二维码扫描后,我调用 API 并获取数据。它的工作正常。但后来当我扫描其他二维码时,数据被检索到,但它没有显示在内部 recyclerview 中,这意味着没有展开和折叠功能起作用。

在实例化 recyclerview、适配器和布局管理器之后。我扫描 qrcode,结果我进行了 api 调用。然后观察它并发送数据进行第一次绑定。

下面是观察者。

  //ViewModel Observer
private void observeVIewModel() {
    qrTrac_viewModel.getQRTrackListLiveData().observe(this, qrTrackingList -> {
        if (qrTrac_viewModel.getSerResponseCodeVModel() != null) {
            if (qrTrac_viewModel.getSerResponseCodeVModel().equals(Constants.OK)){
                if (qrTrackingList != null) {
                    Util.Logd(qrTrackingList.getLstQRL0().toString());
                    progressActivity.dismiss();
                    Util.Logd("qrtrack viewmodel call");

                    mLstQRL0Adapter.setLstQRL0List((ArrayList<LstQRL0>) qrTrackingList.getLstQRL0(),(ArrayList<LstQRL1Info>) qrTrackingList.getLstQRL1Info(),
                            (ArrayList<LstQRTrackingSummary>) qrTrackingList.getLstQRTrackingSummary());
                }
            } else if(qrTrac_viewModel.getSerResponseCodeVModel().equals(Constants.INTERNAL_SERVER_ERROR)){
                progressActivity.dismiss();
                Util.Loge("in not found");
                Snackbar snackbar = Snackbar.make(view, "Internal Server Error", BaseTransientBottomBar.LENGTH_INDEFINITE).setAction(android.R.string.ok, view -> {

                });
                snackbar.show();
            }
            else  {
                progressActivity.dismiss();
                Snackbar snackbar = Snackbar.make(view, qrTrac_viewModel.getSerVModelResponseMsg(), BaseTransientBottomBar.LENGTH_INDEFINITE).setAction(android.R.string.ok, v -> {});
                snackbar.show();
            }
        }
        else {
            progressActivity.dismiss();
            Snackbar snackbar = Snackbar.make(view, "Some Unknown Error occured", BaseTransientBottomBar.LENGTH_INDEFINITE).setAction(android.R.string.ok, v -> {});
            snackbar.show();
        }
    });
}

这是第一个适配器。

 public class LstQRL0Adapter extends RecyclerView.Adapter<LstQRL0Adapter.LstQRL0ViewHolder> {

private Context context;
private List<LstQRL0> listQRL0 = new ArrayList<>();
private List<LstQRL1Info> listQRl1Info = new ArrayList<>();
private List<LstQRTrackingSummary> listQRTracSumm = new ArrayList<>();
private View view;
private LayoutInflater inflater;
private ArrayList<LstQRL1Info> templistQRl1Info = new ArrayList<>();
private int oldL0ClickPosition = -1;

//lstQRLInfo
private RecyclerView mLstQRL1InfoRecView;
private LstQRL1InfoAdapter mLstQRL1InfoAdapter;
private LinearLayoutManager mLst1Manager;

public LstQRL0Adapter(Context context) {
    inflater = LayoutInflater.from(context);
    this.context = context;
}

public void setLstQRL0List(ArrayList<LstQRL0> list, ArrayList<LstQRL1Info> listQRl1Info, ArrayList<LstQRTrackingSummary> listQRTracSumm) {
    this.listQRL0 = list;
    this.listQRl1Info = listQRl1Info;
    this.listQRTracSumm = listQRTracSumm;
    oldL0ClickPosition = -1 ;
    notifyDataSetChanged();

@NonNull
@Override
public LstQRL0Adapter.LstQRL0ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    view = inflater.inflate(R.layout.lstqrl0_baseview, parent, false);
    return new LstQRL0ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull LstQRL0Adapter.LstQRL0ViewHolder holder, int position) {
    LstQRL0 myL0List = listQRL0.get(position);

    holder.mSeason.setText(myL0List.getNvGDSeason());
    holder.mStyle.setText(myL0List.getStyleBriefDescr().replace("<br/>", "\n"));
    holder.mVendor.setText(myL0List.getNvPartnerVendor());

    mLstQRL1InfoAdapter = new LstQRL1InfoAdapter(context);
    mLstQRL1InfoRecView.setAdapter(mLstQRL1InfoAdapter);
    mLstQRL1InfoRecView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));

    templistQRl1Info.clear();

    for (int i = 0; i < listQRl1Info.size(); i++) {
        if (listQRl1Info.get(position).getId().equals(listQRl1Info.get(i).getId()) && !listQRl1Info.get(i).getId().isEmpty()) {
            Util.Logd(listQRl1Info.get(i).getId() + " is l0 adapter");
            templistQRl1Info.add(listQRl1Info.get(i));
        }
    }

    Util.Logd(templistQRl1Info.size() +" is temp l0 size");
    mLstQRL1InfoAdapter.setLstQRL1infoList(templistQRl1Info, (ArrayList<LstQRTrackingSummary>) listQRTracSumm);

    if (listQRL0.get(position).isL0Expanded()) {
        holder.mLstQrL0ImageView.setImageResource(R.drawable.ic_arrow_drop_down);
        mLstQRL1InfoRecView.setVisibility(View.VISIBLE);
    } else {
        holder.mLstQrL0ImageView.setImageResource(R.drawable.ic_arrow_right);
        mLstQRL1InfoRecView.setVisibility(View.GONE);
    }

    view.setOnClickListener(view -> {
        if (oldL0ClickPosition >= 0){
            if (listQRL0.get(oldL0ClickPosition).isL0Expanded()) {
                listQRL0.get(oldL0ClickPosition).setL0Expanded(false);
                mLstQRL1InfoRecView.setVisibility(View.GONE);
                notifyItemChanged(oldL0ClickPosition);
            }
        }

        if (oldL0ClickPosition == holder.getAdapterPosition()){
            oldL0ClickPosition = -1 ;
        }else {
            oldL0ClickPosition = holder.getAdapterPosition() ;
        }

        Util.Logd("Old clickc is "+oldL0ClickPosition);
        if (oldL0ClickPosition >= 0){
            new Handler(Looper.myLooper()).postDelayed(() -> {
                listQRL0.get(oldL0ClickPosition).setL0Expanded(!listQRL0.get(oldL0ClickPosition).isL0Expanded());
                notifyItemChanged(oldL0ClickPosition);
            }, 500);
        }
    });
}

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

public class LstQRL0ViewHolder extends RecyclerView.ViewHolder {

    private TextView mSeason, mStyle, mVendor;
    private ImageView mLstQrL0ImageView;

    public LstQRL0ViewHolder(@NonNull View itemView) {
        super(itemView);
        Util.Logd("just check is called");
        mSeason = itemView.findViewById(R.id.lstqrl0_season);
        mStyle = itemView.findViewById(R.id.lstqrl0_style);
        mVendor = itemView.findViewById(R.id.lstqrl0_vendor);
        mLstQrL0ImageView = itemView.findViewById(R.id.lstqr10_arrow);
        mLstQRL1InfoRecView = itemView.findViewById(R.id.lstqrl1info_rv);

    }
}

}

这是第二个适配器

  public class LstQRL1InfoAdapter extends RecyclerView.Adapter<LstQRL1InfoAdapter.LstQRL1InfoViewHolder> {

private Context context;
private List<LstQRL1Info> lstQRL1Infos = new ArrayList<>();
private List<LstQRTrackingSummary> listQRTracSumm = new ArrayList<>();
private ArrayList<LstQRTrackingSummary> tempListQRTracSumm = new ArrayList<>();
private View view;
private LayoutInflater inflater;
private int oldL1ClickPosition = 0;   // not used in this case

//LstQRTracingSummary
private RecyclerView mQRLTracSummRV;
private LstQRTracSummAdapter mQrTracSummAdapter;
private LinearLayoutManager mQrTrackSummManager;

public LstQRL1InfoAdapter(Context context) {
    this.context = context;
    inflater = LayoutInflater.from(context);
}

public void setLstQRL1infoList(ArrayList<LstQRL1Info> list, ArrayList<LstQRTrackingSummary> listQRTracSumm) {
    this.lstQRL1Infos = list;
    this.listQRTracSumm = listQRTracSumm;
    notifyDataSetChanged();
}

@NonNull
@Override
public LstQRL1InfoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    view = inflater.inflate(R.layout.lstqrl1info_baseview, parent, false);
    return new LstQRL1InfoViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull LstQRL1InfoViewHolder holder, int position) {
    holder.bind(lstQRL1Infos.get(position),position);
}

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

public class LstQRL1InfoViewHolder extends RecyclerView.ViewHolder {

    private TextView quantity, info, add_info;
    private ImageView mlstqrl1info_arrow;

    public LstQRL1InfoViewHolder(@NonNull View itemView) {
        super(itemView);
        quantity = itemView.findViewById(R.id.lstqr1info_quantity);
        info = itemView.findViewById(R.id.lstqr1info_info);
        add_info = itemView.findViewById(R.id.lstqrl1info_add_info);
        mQRLTracSummRV = itemView.findViewById(R.id.lstqrtracsumm_rv);
        mlstqrl1info_arrow = itemView.findViewById(R.id.lstqr1info_arrow);

            itemView.setOnClickListener(view -> {
            if (lstQRL1Infos.get(oldL1ClickPosition).isL1Expanded()) {
                Util.Logd("in expanded check");
                lstQRL1Infos.get(oldL1ClickPosition).setL1Expanded(false);
                mQRLTracSummRV.setVisibility(View.GONE);
                notifyItemChanged(oldL1ClickPosition);
            }

            oldL1ClickPosition = getAdapterPosition() ;

            new Handler(Looper.myLooper()).postDelayed(() -> {
            }, 500);
            lstQRL1Infos.get(getAdapterPosition()).setL1Expanded(!lstQRL1Infos.get(getAdapterPosition()).isL1Expanded());
            notifyItemChanged(getAdapterPosition());
        });
    }

    public void bind(LstQRL1Info lstQRL1Info,int position) {
        quantity.setText(Math.round(lstQRL1Info.getmQty())+"");
        info.setText(lstQRL1Info.getNvGDActivity());
        add_info.setText(lstQRL1Info.getNvAdditionalInfo1());

        mQrTracSummAdapter = new LstQRTracSummAdapter(context);
        mQRLTracSummRV.setAdapter(mQrTracSummAdapter);
        mQRLTracSummRV.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));

        tempListQRTracSumm.clear() ;

        for (int i = 0; i < listQRTracSumm.size(); i++) {
            if (lstQRL1Info.getId().equals(listQRTracSumm.get(i).getIdQRTrackingList()) && !listQRTracSumm.get(i).getIdQRTrackingList().isEmpty()) {
                tempListQRTracSumm.add(listQRTracSumm.get(i));
                Util.Logd(listQRTracSumm.get(i).getIdQRTrackingList() + " is info adapter");
            }
        }
        Util.Logd("in view binding and size is " + lstQRL1Infos.size() + "   "+ tempListQRTracSumm.size());
        mQrTracSummAdapter.setListQRTracSumm(tempListQRTracSumm);

        if (lstQRL1Infos.get(position).isL1Expanded()) {
            mlstqrl1info_arrow.setImageResource(R.drawable.ic_arrow_drop_down);
            mQRLTracSummRV.setVisibility(View.VISIBLE);
        } else {
            mlstqrl1info_arrow.setImageResource(R.drawable.ic_arrow_right);
            mQRLTracSummRV.setVisibility(View.GONE);
        }
    }
}

}

第三个适配器也与第二个相同。第一个适配器与第二个和第三个适配器有点不同,因为我正在努力实现所需的功能。

问题:当我扫描第二次和后续时间时,会收到来自 api 调用的数据,我认为设置也正确,但不知何故它不显示 recyclerview 2 数据。(第一个 recyclerview 数据显示正确)。

帮助我是否使用不同的方法或更新相同的代码。

提前致谢。

4

1 回答 1

0

更新 - 上面的代码是正确的,我做的错误是 -> 我在第一次运行 Activity 时提前创建了 RecyclerView 适配器实例(这是适配器的提前初始化)并且稍后在扫描 QrCode 和 Api 调用时更新数据制成 。

所以现在我在观察者收到数据时创建实例。如下所示

    private void observeVIewModel() {

    qrTrac_viewModel.getQRTrackListLiveData().observe(this, qrTrackingList -> {
        if (qrTrac_viewModel.getSerResponseCodeVModel() != null) {
            if (qrTrac_viewModel.getSerResponseCodeVModel().equals(Constants.OK)){
                if (qrTrackingList != null) {
                    Util.Logd(qrTrackingList.getLstQRL0().toString());
                    progressActivity.dismiss();
                    Util.Logd("qrtrack viewmodel call");

                    // result sent to adapters for binding
                    mLstQRL0Adapter = new LstQRL0Adapter(this,(ArrayList<LstQRL0>) qrTrackingList.getLstQRL0(),(ArrayList<LstQRL1Info>) qrTrackingList.getLstQRL1Info(),
                            (ArrayList<LstQRTrackingSummary>) qrTrackingList.getLstQRTrackingSummary());
                    mLst0Manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
                    mLst0RecView.setLayoutManager(mLst0Manager);
                    mLst0RecView.setNestedScrollingEnabled(false);
                    mLst0RecView.setRecycledViewPool(sharedPool);
                    mLst0RecView.setAdapter(mLstQRL0Adapter);
                    mLstQRL0Adapter.notifyDataSetChanged();
                }
            } else if(qrTrac_viewModel.getSerResponseCodeVModel().equals(Constants.INTERNAL_SERVER_ERROR)){
                progressActivity.dismiss();
                Util.Loge("in not found");
                Snackbar snackbar = Snackbar.make(view, "Internal Server Error", BaseTransientBottomBar.LENGTH_INDEFINITE).setAction(android.R.string.ok, view -> {

                });
                snackbar.show();
            }
            else  {
                progressActivity.dismiss();
                Snackbar snackbar = Snackbar.make(view, qrTrac_viewModel.getSerVModelResponseMsg(), BaseTransientBottomBar.LENGTH_INDEFINITE).setAction(android.R.string.ok, v -> {});
                snackbar.show();
            }
        }
        else {
            progressActivity.dismiss();
            Snackbar snackbar = Snackbar.make(view, "Some Unknown Error occured", BaseTransientBottomBar.LENGTH_INDEFINITE).setAction(android.R.string.ok, v -> {});
            snackbar.show();
        }
    });
}

与我为嵌套列表的进一步适配器做的相同的方式更好地工作。

第 1 节。

public class LstQRL0Adapter extends RecyclerView.Adapter<LstQRL0Adapter.LstQRL0ViewHolder> {

private Context context;
private List<LstQRL0>  listQRL0 = new ArrayList<>() ;
private List<LstQRL1Info>  listQRl1Info = new ArrayList<>(); ;
private List<LstQRTrackingSummary> listQRTracSumm = new ArrayList<>();
private View view;
private LayoutInflater inflater;
private ArrayList<LstQRL1Info> templistQRl1Info = new ArrayList<>();
private int oldL0ClickPosition = -1;
private Boolean isFromClose = false ;

//lstQRLInfo
private RecyclerView mLstQRL1InfoRecView;
public LstQRL1InfoAdapter mLstQRL1InfoAdapter;

public LstQRL0Adapter(Context context,ArrayList<LstQRL0> list, ArrayList<LstQRL1Info> listQRl1Info, ArrayList<LstQRTrackingSummary> listQRTracSumm) {
    inflater = LayoutInflater.from(context);
    this.context = context;
    this.listQRL0 = list;
    this.listQRl1Info = listQRl1Info;
    this.listQRTracSumm = listQRTracSumm;
    oldL0ClickPosition = -1;
}


@NonNull
@Override
public LstQRL0Adapter.LstQRL0ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    view = inflater.inflate(R.layout.lstqrl0_baseview, parent, false);
    return new LstQRL0ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull LstQRL0Adapter.LstQRL0ViewHolder holder, int position) {
    LstQRL0 myL0List = listQRL0.get(position);

    holder.mSeason.setText(myL0List.getNvGDSeason());
    holder.mStyle.setText(myL0List.getStyleBriefDescr().replace("<br/>", "\n"));
    holder.mVendor.setText(myL0List.getNvPartnerVendor());



    Util.Logd( "temp l0 size is "+templistQRl1Info.size());
    if (!isFromClose){
        templistQRl1Info.clear();

        if (oldL0ClickPosition >=0){
            for (int i = 0; i < listQRl1Info.size(); i++) {
                if (listQRL0.get(oldL0ClickPosition).getId().equals(listQRl1Info.get(i).getId())) {
                    Util.Logd( "temp ids are "+listQRl1Info.get(i).getId());
                    templistQRl1Info.add(listQRl1Info.get(i));
                }
            }
        }
        mLstQRL1InfoAdapter = new LstQRL1InfoAdapter(context,templistQRl1Info, (ArrayList<LstQRTrackingSummary>) listQRTracSumm);
        mLstQRL1InfoRecView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        mLstQRL1InfoRecView.setNestedScrollingEnabled(false);
        mLstQRL1InfoRecView.setAdapter(mLstQRL1InfoAdapter);
        mLstQRL1InfoAdapter.notifyDataSetChanged();
    }

    boolean isL0Expanded = myL0List.isL0Expanded();
    Util.Logd("Expanded status is "+ position + " " + listQRL0.get(position).isL0Expanded());
    if (isL0Expanded) {
        holder.mLstQrL0ImageView.setImageResource(R.drawable.ic_arrow_drop_down);
        holder.hideShowL0Layout.setVisibility(View.VISIBLE);
    } else {
        holder.mLstQrL0ImageView.setImageResource(R.drawable.ic_arrow_right);
        holder.hideShowL0Layout.setVisibility(View.GONE);
    }

    holder.itemView.setOnClickListener(view -> {

        Util.Logd("////////////////////////");
        if (oldL0ClickPosition >= 0) {
            if (listQRL0.get(oldL0ClickPosition).isL0Expanded()) {
                listQRL0.get(oldL0ClickPosition).setL0Expanded(false);
                holder.hideShowL0Layout.setVisibility(View.GONE);
                Util.Logd("Id to close is "+listQRL0.get(oldL0ClickPosition).getId());
                isFromClose = true ;
                notifyItemChanged(oldL0ClickPosition);
            }
        }

        if (oldL0ClickPosition == holder.getAdapterPosition()) {
            oldL0ClickPosition = -1;
        } else {
            oldL0ClickPosition = holder.getAdapterPosition();
        }

        Util.Logd("New click is " + oldL0ClickPosition);
        if (oldL0ClickPosition >= 0 && oldL0ClickPosition != -1) {
            new Handler(Looper.myLooper()).postDelayed(() -> {
                listQRL0.get(oldL0ClickPosition).setL0Expanded(!listQRL0.get(oldL0ClickPosition).isL0Expanded());
                Util.Logd("new data modify is "+listQRL0.get(oldL0ClickPosition).getId());
                isFromClose = false ;
                notifyItemChanged(oldL0ClickPosition);
            },500);
        }
    });
}

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

public class LstQRL0ViewHolder extends RecyclerView.ViewHolder {

    private TextView mSeason, mStyle, mVendor;
    private ImageView mLstQrL0ImageView;
    private LinearLayout hideShowL0Layout;

    public LstQRL0ViewHolder(@NonNull View itemView) {
        super(itemView);
        mSeason = itemView.findViewById(R.id.lstqrl0_season);
        mStyle = itemView.findViewById(R.id.lstqrl0_style);
        mVendor = itemView.findViewById(R.id.lstqrl0_vendor);
        mLstQrL0ImageView = itemView.findViewById(R.id.lstqr10_arrow);
        mLstQRL1InfoRecView = itemView.findViewById(R.id.lstqrl1info_rv);
        hideShowL0Layout = itemView.findViewById(R.id.hideShowL0Layout);
    }
}

}

还有一个嵌套适配器,但也一样,所以我不在这里发布。

如果需要任何帮助,请发表评论。

于 2021-05-22T17:00:24.290 回答