Skip to content

Commit 21060b2

Browse files
imhappipekingme
authored andcommitted
[Lists] Add API to update list shape explicitly
Resolves #5001 PiperOrigin-RevId: 862337925
1 parent db3298e commit 21060b2

2 files changed

Lines changed: 56 additions & 2 deletions

File tree

lib/java/com/google/android/material/listitem/ListItemLayout.java

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import android.view.animation.Interpolator;
4444
import android.view.animation.PathInterpolator;
4545
import android.widget.FrameLayout;
46+
import androidx.annotation.IntDef;
4647
import androidx.annotation.NonNull;
4748
import androidx.annotation.Nullable;
4849
import androidx.core.view.GravityCompat;
@@ -53,6 +54,8 @@
5354
import com.google.android.material.listitem.RevealableListItem.RevealGravity;
5455
import com.google.android.material.listitem.SwipeableListItem.StableSwipeState;
5556
import com.google.android.material.listitem.SwipeableListItem.SwipeState;
57+
import java.lang.annotation.Retention;
58+
import java.lang.annotation.RetentionPolicy;
5659

5760
/**
5861
* A container layout for a List item.
@@ -87,6 +90,23 @@ public class ListItemLayout extends FrameLayout {
8790
private static final int SETTLING_DURATION = 350;
8891
private static final int DEFAULT_SIGNIFICANT_VEL_THRESHOLD = 500;
8992

93+
/** Positions for list items. */
94+
@Retention(RetentionPolicy.SOURCE)
95+
@IntDef({POSITION_FIRST, POSITION_MIDDLE, POSITION_LAST, POSITION_SINGLE})
96+
public @interface Position {}
97+
98+
/** Position for the first item in a list. */
99+
public static final int POSITION_FIRST = 0;
100+
101+
/** Position for an item in the middle of a list. */
102+
public static final int POSITION_MIDDLE = 1;
103+
104+
/** Position for the last item in a list. */
105+
public static final int POSITION_LAST = 2;
106+
107+
/** Position for an item that is the only item in a list. */
108+
public static final int POSITION_SINGLE = 3;
109+
90110
@Nullable private int[] positionState;
91111

92112
@Nullable private ViewDragHelper viewDragHelper;
@@ -168,8 +188,8 @@ protected int[] onCreateDrawableState(int extraSpace) {
168188

169189
/**
170190
* Helper method that sets the drawable state of the ListItemLayout according to its position in
171-
* the list. This is already called by {@link ListItemViewHolder#bind} if the ListItemLayout is
172-
* inside of a {@link ListItemViewHolder}.
191+
* the list. This can be called via {@link ListItemViewHolder#bind(int, int)} if the
192+
* ListItemLayout is inside of a {@link ListItemViewHolder}.
173193
*
174194
* <p>Children of ListItemLayout that wish to be affected by this state should duplicate its
175195
* parent's state.
@@ -189,6 +209,32 @@ public void updateAppearance(int position, int itemCount) {
189209
refreshDrawableState();
190210
}
191211

212+
/**
213+
* Helper method that sets the drawable state of the ListItemLayout according to its position in
214+
* the list. This is already called by {@link ListItemViewHolder#bind} if the ListItemLayout is
215+
* inside of a {@link ListItemViewHolder}.
216+
*
217+
* <p>Children of ListItemLayout that wish to be affected by this state should duplicate its
218+
* parent's state.
219+
*/
220+
public void updateAppearance(@Position int position) {
221+
switch (position) {
222+
case POSITION_SINGLE:
223+
positionState = SINGLE_STATE_SET;
224+
break;
225+
case POSITION_FIRST:
226+
positionState = FIRST_STATE_SET;
227+
break;
228+
case POSITION_LAST:
229+
positionState = LAST_STATE_SET;
230+
break;
231+
case POSITION_MIDDLE:
232+
positionState = MIDDLE_STATE_SET;
233+
break;
234+
}
235+
refreshDrawableState();
236+
}
237+
192238
@Override
193239
public void addView(View child, int index, ViewGroup.LayoutParams params) {
194240
super.addView(child, index, params);

lib/java/com/google/android/material/listitem/ListItemViewHolder.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,12 @@ public void bind(int position, int itemCount) {
7373
}
7474
listItemLayout.updateAppearance(position, itemCount);
7575
}
76+
77+
/**
78+
* Binds the corresponding {@link ListItemLayout} according to the given {@link
79+
* ListItemLayout.Position}.
80+
*/
81+
public void bind(@ListItemLayout.Position int position) {
82+
listItemLayout.updateAppearance(position);
83+
}
7684
}

0 commit comments

Comments
 (0)