4343import android .view .animation .Interpolator ;
4444import android .view .animation .PathInterpolator ;
4545import android .widget .FrameLayout ;
46+ import androidx .annotation .IntDef ;
4647import androidx .annotation .NonNull ;
4748import androidx .annotation .Nullable ;
4849import androidx .core .view .GravityCompat ;
5354import com .google .android .material .listitem .RevealableListItem .RevealGravity ;
5455import com .google .android .material .listitem .SwipeableListItem .StableSwipeState ;
5556import 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 );
0 commit comments