0

I'm trying to replace a LinearLayout in a RecyclerView row but only the last row of all the items is being replaced.

In my RecyclerView there are two types of items - Payment (blue dollar icon) and Session (yellow casino chips icon) - therefore if the Type is PlayerBalanceItemTypes.Session I instantiate a new BalancePlayerSessionDetailed fragment and replaces a LinearLayout with the ID layoutBalancePlayerSessionDetailed that exists in the Layout. In OnCreateViewHolder that Layout is being assigned to a property called SessionDetailed. However only the top row is being replaced.

public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
        {
            PlayerBalanceRecycleView view = holder as PlayerBalanceRecycleView;
            var item = mItems[position];

            //view.Type.Text = item.Type.Substring(0, 1);
            view.Date.Text = item.CreationDate.ToString("dd/MM");
            view.Result.Text = item.Amount.ToString("N0", new CultureInfo("en-US"));
            view.Total.Text = item.Balance.ToString("N0", new CultureInfo("en-US"));

            //var orange = "#FF7300";
            if (item.Type == PlayerBalanceItemTypes.Payment)
            {
                view.Icon.SetImageDrawable(mActivity.Resources.GetDrawable(Resource.Drawable.ic_money_medium_blue));
                view.Result.SetTextColor(mActivity.Resources.GetColor(Resource.Color.orange));
            }
            else if(item.Type == PlayerBalanceItemTypes.Session)
            {
                view.Icon.SetImageDrawable(mActivity.Resources.GetDrawable(Resource.Drawable.ic_chips_medium));

                var frag = new BalancePlayerSessionDetailed(mActivity, -1);
                var trans = mFragmentManager.BeginTransaction();
                trans.Replace(view.SessionDetailed.Id, frag);
                //trans.Replace(Resource.Id.layoutBalancePlayerSessionDetailed, frag);
                trans.Commit();
            }
            if (item.Type == PlayerBalanceItemTypes.InitialDebt) view.Date.Visibility = ViewStates.Invisible;

            //view.SessionDetailed.Visibility = ViewStates.Gone;
        }

The Layout that the RecyclerView uses:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minWidth="25px"
    android:minHeight="25px">
    <LinearLayout
        android:orientation="vertical"
        android:minWidth="46px"
        android:minHeight="46px"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout2"
        android:layout_margin="5dp"
        android:background="#FFFFFF">
        <LinearLayout
            android:orientation="horizontal"
            android:minHeight="46dp"
            android:weightSum="100"
            android:background="?android:attr/selectableItemBackground"
            android:clickable="true"
            android:id="@+id/layoutPlayerBalance"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <LinearLayout
                android:orientation="vertical"
                android:minWidth="25px"
                android:minHeight="25px"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/linearLayout3"
                android:layout_weight="5"
                android:gravity="center">
                <ImageView
                    android:src="@android:drawable/ic_menu_gallery"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/imageViewPlayerBalanceType" />
            </LinearLayout>
            <TextView
                android:text="06/07 23:25"
                android:gravity="center"
                android:id="@+id/textBalancePlayerDate"
                android:layout_width="0dp"
                android:layout_weight="25"
                android:layout_height="match_parent"
                android:textSize="20dp"
                android:singleLine="true"
                android:textColor="@color/grey" />
            <TextView
                android:text="Result"
                android:gravity="right|center_vertical"
                android:id="@+id/textBalancePlayerResult"
                android:layout_width="0dp"
                android:layout_weight="35"
                android:layout_height="match_parent"
                android:textSize="28dp"
                android:singleLine="true"
                android:textColor="@color/blue"
                android:paddingLeft="5dp" />
            <TextView
                android:text="Total"
                android:gravity="right|center_vertical"
                android:id="@+id/textBalancePlayerTotal"
                android:layout_width="0dp"
                android:layout_weight="35"
                android:layout_height="match_parent"
                android:textSize="20dp"
                android:textColor="@color/grey"
                android:singleLine="true"
                android:paddingRight="10dp" />
        </LinearLayout>
    </LinearLayout>
    <!-- I'M REPLACING THIS LINEAR LAYOUT -->
    <LinearLayout
        android:orientation="horizontal"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_margin="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/layoutBalancePlayerSessionDetailed" />
</LinearLayout>

Current result: enter image description here

Expected result: enter image description here

Why is this row only visible/replaced for the top now and not for the Session rows?

Westerlund.io
  • 2,743
  • 5
  • 30
  • 37

1 Answers1

0

Solved it by using the methods in this post: Fragment replacing in RecyclerView item

In my OnBindViewHolder the code now looks like this:

int containerId = view.SessionDetailed.Id;
                var oldFrag = mActivity.FragmentManager.FindFragmentById(containerId);
                if (oldFrag != null) mActivity.FragmentManager.BeginTransaction().Remove(oldFrag).Commit();

                int newContainerId = DateTime.Now.Millisecond;
                view.SessionDetailed.Id = newContainerId;

                var frag = new BalancePlayerSessionDetailed(mActivity, -1);
                var trans = mFragmentManager.BeginTransaction();
                trans.Replace(view.SessionDetailed.Id, frag);
                //trans.Replace(Resource.Id.layoutBalancePlayerSessionDetailed, frag);
                trans.Commit();
Community
  • 1
  • 1
Westerlund.io
  • 2,743
  • 5
  • 30
  • 37