woxblom / DragListView

Drag and drop to reorder items in a list, grid or board for Android. Based on RecyclerView. Also supports swiping items in a list.
Apache License 2.0
693 stars 177 forks source link

Editing any Item Programatically #183

Closed Pantoflarz closed 3 years ago

Pantoflarz commented 3 years ago

I am using the Board Fragment for this.

Essentially, each one of my items has the following xml:

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout android:id="@+id/item_layout" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:card_view="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingBottom="2dp" android:paddingLeft="6dp" android:paddingRight="6dp" android:paddingTop="2dp">

<androidx.cardview.widget.CardView
    android:id="@+id/card"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:duplicateParentState="true"
    android:foreground="@drawable/card_view_selector"
    card_view:cardBackgroundColor="#ffffff"
    card_view:cardCornerRadius="4dp"
    card_view:cardElevation="2dp"
    card_view:cardPreventCornerOverlap="true"
    card_view:cardUseCompatPadding="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:padding="12dp">

        <ImageView
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginRight="12dp"
            android:src="@drawable/ic_help_black_24dp"/>

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="test test test"
            android:textSize="16sp"/>

        <Spinner
            android:id="@+id/activityType"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:entries="@array/spinnerIntervalTypeItems"/>

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="number"/>

        <Spinner
            android:id="@+id/activityTime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:entries="@array/spinnerTimeTypeItems"/>

    </LinearLayout>
</androidx.cardview.widget.CardView>

As you can see, there are two spinners there.

Therefore, I want to be able to programatically control the spinners and do stuff when each option is selected (for example, to change the image file displayed next to the item too).

Basically put, I want to be able to register two spinnerarrayadapters for each item.

How would I do this?

This is my adapter:

ArrayList<String> spinnerArray = new ArrayList<>(Arrays.asList(getResources().getStringArray(R.array.spinnerIntervalTypeItems)));
        final ArrayAdapter spinnerArrayAdapter = new ArrayAdapter<String>(
                getContext(), R.layout.spinner_item, spinnerArray) {
            //do not show the first element as enabled
            @Override
            public boolean isEnabled(int position) {
                if (position == 0) {
                    return false;
                } else {
                    return true;
                }
            }

            //grey out the first element
            @Override
            public View getDropDownView(int position, View convertView,
                                        ViewGroup parent) {
                View view = super.getDropDownView(position, convertView, parent);
                TextView tv = (TextView) view;
                if (position == 0) {
                    tv.setTextColor(Color.GRAY);
                } else {
                    tv.setTextColor(Color.BLACK);
                }
                return view;
            }
        };

Thanks

Pantoflarz commented 3 years ago

No worries, managed to solve this.

In the ItemAdapter I put this code in the ViewHolder and it seems to work.