anta40
anta40

Reputation: 6743

How to "persist" data/view while scrolling RecyclerView

I'm implementing an Excel-like table with RecyclerView like this: enter image description here

The RecyclerView is wrapped with a ScrollView, because the requirement is only the RecyclerView is scrollable, not the entire activity. There are 2 issues, though:

  1. Say I just input values on Kalbe's activity and remarks. If the recycler view is scrolled to bottom end and goes up again, those values are gone.

  2. When some of the rows are not visible due to scrolling, say the first 2 rows, this code doesn't work as expected:

      for (int x = 0; x < uiBinding.rvCompetitor.getChildCount(); x++){
     View vv = uiBinding.rvCompetitor.getChildAt(x);
     EditText edtActivity = (EditText) vv.findViewById(R.id.edtActivity);
     EditText edtRemarks = (EditText) vv.findViewById(R.id.edtRemarks);
     EditText edtCompany = (EditText) vv.findViewById(R.id.edtCompany);
    
     //
     // save the inputs from those EditText to ArrayList and submit
     allData.add(new PromoItem(edtCompany.getText().toString(), edtActivitygetText().toString(), edtRemarks.getText().toString());
     // 
     //
    

    }

My expectation is uiBinding.rvCompetitor.getChildCount() returns 10 (the entire row count). Yet it returns 8. That means I cannot update allData correctly

This is the ViewHolder's layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/black_5"
    android:orientation="horizontal"
    android:gravity="center_vertical"
    android:weightSum="3.2"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:id="@+id/tv_no"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.1"
        android:gravity="center"
        android:text="No"
        android:maxLines="1"
        android:textAllCaps="true"
        android:textColor="@color/black"
        android:textSize="@dimen/_6sdp"
        android:textStyle="normal" />

    <EditText
        android:id="@+id/edtCompany"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.6"
        android:gravity="left"
        android:maxLines="1"
        android:text="Code"
        android:textAllCaps="true"
        android:textColor="@color/black"
        android:textSize="@dimen/_6sdp"/>

    <EditText
        android:id="@+id/edtActivity"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1.25"
        android:gravity="left"
        android:maxLines="1"
        android:inputType="text"
        android:text="Activity"
        android:textAllCaps="true"
        android:textColor="@color/black"
        android:textSize="@dimen/_6sdp"/>

    <EditText
        android:id="@+id/edtRemarks"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1.25"
        android:gravity="left"
        android:maxLines="1"
        android:inputType="text"
        android:text="Remarks"
        android:textAllCaps="false"
        android:textColor="@color/black"
        android:textSize="@dimen/_6sdp" />


</LinearLayout>

The data model:

import com.google.gson.annotations.SerializedName;

public class PromoCompetitorItem {
    @SerializedName("company_id")
    public int companyId;

    @SerializedName("company_name")
    public String companyName;

    @SerializedName("activity")
    public String activity;

    @SerializedName("remarks")
    public String remarks;

    public boolean readOnly = false;

    public PromoCompetitorItem(int id, String name, String activity, String remarks){
        this.companyId = id;
        this.companyName = name;
        this.activity = activity;
        this.remarks = remarks;
    }
}

and the adapter:

public class CompetitorAdapter extends RecyclerView.Adapter<CompetitorAdapter.ViewHolder> {

    private List<PromoCompetitorItem> data;
    private Context ctxt;

    public CompetitorAdapter(Context ctxt){
        this.ctxt = ctxt;
        this.data = new ArrayList<PromoCompetitorItem>();
    }

    public void updateData(List<PromoCompetitorItem> newData){
        data.clear();
        data.addAll(newData);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View viewItem = inflater.inflate(R.layout.item_promo_competitor, parent, false);
        ViewHolder holder = new ViewHolder(viewItem);
        return holder;
    }

    @Override
    public int getItemViewType(int position) {
       return position;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        PromoCompetitorItem item = data.get(position);

        holder.uiBinding.tvNo.setText(""+(position+1));
        holder.uiBinding.edtCompany.setText(item.companyName);

        if (item.readOnly) holder.uiBinding.edtCompany.setEnabled(false);
        else holder.uiBinding.edtCompany.setEnabled(true);

        if (item.activity == null) holder.uiBinding.edtActivity.setText("");
        else holder.uiBinding.edtActivity.setText(item.activity);

        if (item.remarks == null) holder.uiBinding.edtRemarks.setText("");
        else holder.uiBinding.edtRemarks.setText(item.remarks);
    }

    @Override
    public int getItemCount() {
        if (data == null) return 0;
        else return data.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        ItemPromoCompetitorBinding uiBinding;

        public ViewHolder(View view){
            super(view);
            uiBinding = ItemPromoCompetitorBinding.bind(view);
        }
    }

}

If I change the activity layout so ScrollView wraps the entire layout (not the RecyclerView), then no issue at all. How to fix this?

Upvotes: 0

Views: 14

Answers (0)

Related Questions