# ListItemLayout

[View Source](https://github.com/material-components/material-components-android/blob/1.14.0/lib/java/com/google/android/material/listitem/ListItemLayout.java)

*** ** * ** ***


```
public class ListItemLayout
```

<br />

*** ** * ** ***

A container layout for a List item.

This layout applies the following states according to its position in a list:

- android.R.attr.state_first
- android.R.attr.state_last
- android.R.attr.state_middle
- android.R.attr.state_single

Children of ListItemLayout that wish to be affected by the ListItemLayout's position state should duplicate the state through setting android.R.attr.duplicateParentState to true.

MaterialCardView is recommended as a ListItemLayout child, as it supports updating its shape / corners based on states.

ListItemLayout supports swipe-to-reveal via a single `https://developer.android.com/reference/com/google/android/material/listitem/SwipeableListItem` and up to 2 `https://developer.android.com/reference/com/google/android/material/listitem/RevealableListItem`s. The `https://developer.android.com/reference/com/google/android/material/listitem/SwipeableListItem` and `https://developer.android.com/reference/com/google/android/material/listitem/RevealableListItem`s must be direct children of the ListItemLayout. `https://developer.android.com/reference/com/google/android/material/listitem/RevealableListItem`s can be set to the start or end of the `https://developer.android.com/reference/com/google/android/material/listitem/SwipeableListItem` via android:layout_gravity. There cannot be more than 1 `https://developer.android.com/reference/com/google/android/material/listitem/RevealableListItem` with the same layout_gravity.

## Summary

| ### Nested types |
|---|
| `@https://developer.android.com/reference/java/lang/annotation/Retention(value = https://developer.android.com/reference/java/lang/annotation/RetentionPolicy.SOURCE) public annotation https://developer.android.com/reference/com/google/android/material/listitem/ListItemLayout.Position` Positions for list items. |

| ### Constants |
|---|---|
| `static final int` | `https://developer.android.com/reference/com/google/android/material/listitem/ListItemLayout#POSITION_FIRST() = 0` Position for the first item in a list. |
| `static final int` | `https://developer.android.com/reference/com/google/android/material/listitem/ListItemLayout#POSITION_LAST() = 2` Position for the last item in a list. |
| `static final int` | `https://developer.android.com/reference/com/google/android/material/listitem/ListItemLayout#POSITION_MIDDLE() = 1` Position for an item in the middle of a list. |
| `static final int` | `https://developer.android.com/reference/com/google/android/material/listitem/ListItemLayout#POSITION_SINGLE() = 3` Position for an item that is the only item in a list. |

| ### Public fields |
|---|---|
| `int` | `https://developer.android.com/reference/com/google/android/material/listitem/ListItemLayout#swipeState()` |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/com/google/android/material/listitem/ListItemLayout#ListItemLayout(Context)(Context context)` |
| `https://developer.android.com/reference/com/google/android/material/listitem/ListItemLayout#ListItemLayout(Context,AttributeSet)(Context context, AttributeSet attrs)` |
| `https://developer.android.com/reference/com/google/android/material/listitem/ListItemLayout#ListItemLayout(Context,AttributeSet,int)(Context context, AttributeSet attrs, int defStyleAttr)` |
| `https://developer.android.com/reference/com/google/android/material/listitem/ListItemLayout#ListItemLayout(Context,AttributeSet,int,int)( Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes )` |

| ### Public methods |
|---|---|
| `void` | `https://developer.android.com/reference/com/google/android/material/listitem/ListItemLayout#addView(View,int,ViewGroup.LayoutParams)(View child, int index, LayoutParams params)` |
| `int` | `https://developer.android.com/reference/com/google/android/material/listitem/ListItemLayout#getSwipeState()()` Returns the current `https://developer.android.com/reference/com/google/android/material/listitem/SwipeableListItem.SwipeState` of the ListItemLayout. |
| `boolean` | `https://developer.android.com/reference/com/google/android/material/listitem/ListItemLayout#onInterceptTouchEvent(MotionEvent)(MotionEvent ev)` |
| `boolean` | `https://developer.android.com/reference/com/google/android/material/listitem/ListItemLayout#onTouchEvent(MotionEvent)(MotionEvent ev)` |
| `void` | `https://developer.android.com/reference/com/google/android/material/listitem/ListItemLayout#onViewRemoved(View)(View child)` |
| `void` | `https://developer.android.com/reference/com/google/android/material/listitem/ListItemLayout#setSwipeState(int,int)(int swipeState, int revealGravity)` Sets the state to swipe the `https://developer.android.com/reference/com/google/android/material/listitem/SwipeableListItem` child to. |
| `void` | `<T extends View & https://developer.android.com/reference/com/google/android/material/listitem/RevealableListItem> https://developer.android.com/reference/com/google/android/material/listitem/ListItemLayout#setSwipeState(int,T)(int swipeState, T revealView)` Sets the state to swipe the `https://developer.android.com/reference/com/google/android/material/listitem/SwipeableListItem` child to. |
| `void` | `https://developer.android.com/reference/com/google/android/material/listitem/ListItemLayout#setSwipeState(int,int,boolean)(int swipeState, int revealGravity, boolean animate)` Sets the state to swipe the `https://developer.android.com/reference/com/google/android/material/listitem/SwipeableListItem` child to. |
| `void` | `<T extends View & https://developer.android.com/reference/com/google/android/material/listitem/RevealableListItem> https://developer.android.com/reference/com/google/android/material/listitem/ListItemLayout#setSwipeState(int,T,boolean)( int swipeState, T revealView, boolean animate )` Sets the state to swipe the `https://developer.android.com/reference/com/google/android/material/listitem/SwipeableListItem` child to. |
| `void` | `https://developer.android.com/reference/com/google/android/material/listitem/ListItemLayout#updateAppearance(int)(@https://developer.android.com/reference/com/google/android/material/listitem/ListItemLayout.Position int position)` Helper method that sets the drawable state of the ListItemLayout according to its position in the list. |
| `void` | `https://developer.android.com/reference/com/google/android/material/listitem/ListItemLayout#updateAppearance(int,int)(int position, int itemCount)` Helper method that sets the drawable state of the ListItemLayout according to its position in the list. |

| ### Protected methods |
|---|---|
| `int[]` | `https://developer.android.com/reference/com/google/android/material/listitem/ListItemLayout#onCreateDrawableState(int)(int extraSpace)` |
| `void` | `https://developer.android.com/reference/com/google/android/material/listitem/ListItemLayout#onLayout(boolean,int,int,int,int)(boolean changed, int left, int top, int right, int bottom)` |

## Constants

### POSITION_FIRST

```
public static final int POSITION_FIRST = 0
```

Position for the first item in a list.

### POSITION_LAST

```
public static final int POSITION_LAST = 2
```

Position for the last item in a list.

### POSITION_MIDDLE

```
public static final int POSITION_MIDDLE = 1
```

Position for an item in the middle of a list.

### POSITION_SINGLE

```
public static final int POSITION_SINGLE = 3
```

Position for an item that is the only item in a list.

## Public fields

### swipeState

```
public int swipeState
```

## Public constructors

### ListItemLayout

```
public ListItemLayout(Context context)
```

### ListItemLayout

```
public ListItemLayout(Context context, AttributeSet attrs)
```

### ListItemLayout

```
public ListItemLayout(Context context, AttributeSet attrs, int defStyleAttr)
```

### ListItemLayout

```
public ListItemLayout(
    Context context,
    AttributeSet attrs,
    int defStyleAttr,
    int defStyleRes
)
```

## Public methods

### addView

```
public void addView(View child, int index, LayoutParams params)
```

### getSwipeState

```
public int getSwipeState()
```

Returns the current `https://developer.android.com/reference/com/google/android/material/listitem/SwipeableListItem.SwipeState` of the ListItemLayout.

### onInterceptTouchEvent

```
public boolean onInterceptTouchEvent(MotionEvent ev)
```

### onTouchEvent

```
public boolean onTouchEvent(MotionEvent ev)
```

### onViewRemoved

```
public void onViewRemoved(View child)
```

### setSwipeState

```
public void setSwipeState(int swipeState, int revealGravity)
```

Sets the state to swipe the `https://developer.android.com/reference/com/google/android/material/listitem/SwipeableListItem` child to.

| Parameters |
|---|---|
| `int swipeState` | The state to swipe to. This must be one of `https://developer.android.com/reference/com/google/android/material/listitem/SwipeableListItem#STATE_CLOSED()`, `https://developer.android.com/reference/com/google/android/material/listitem/SwipeableListItem#STATE_OPEN()`, or `https://developer.android.com/reference/com/google/android/material/listitem/SwipeableListItem#STATE_SWIPE_PRIMARY_ACTION()` |
| `int revealGravity` | The gravity of the `https://developer.android.com/reference/com/google/android/material/listitem/RevealableListItem` to affect when setting the swipe state. |

### setSwipeState

```
public void <T extends View & RevealableListItem> setSwipeState(int swipeState, T revealView)
```

Sets the state to swipe the `https://developer.android.com/reference/com/google/android/material/listitem/SwipeableListItem` child to.

| Parameters |
|---|---|
| `int swipeState` | The state to swipe to. This must be one of `https://developer.android.com/reference/com/google/android/material/listitem/SwipeableListItem#STATE_CLOSED()`, `https://developer.android.com/reference/com/google/android/material/listitem/SwipeableListItem#STATE_OPEN()`, or `https://developer.android.com/reference/com/google/android/material/listitem/SwipeableListItem#STATE_SWIPE_PRIMARY_ACTION()` |
| `T revealView` | The `https://developer.android.com/reference/com/google/android/material/listitem/RevealableListItem` view to affect when setting the swipe state. |

### setSwipeState

```
public void setSwipeState(int swipeState, int revealGravity, boolean animate)
```

Sets the state to swipe the `https://developer.android.com/reference/com/google/android/material/listitem/SwipeableListItem` child to.

| Parameters |
|---|---|
| `int swipeState` | The state to swipe to. This must be one of `https://developer.android.com/reference/com/google/android/material/listitem/SwipeableListItem#STATE_CLOSED()`, `https://developer.android.com/reference/com/google/android/material/listitem/SwipeableListItem#STATE_OPEN()`, or `https://developer.android.com/reference/com/google/android/material/listitem/SwipeableListItem#STATE_SWIPE_PRIMARY_ACTION()` |
| `int revealGravity` | The gravity of the `https://developer.android.com/reference/com/google/android/material/listitem/RevealableListItem` to reveal when swiping. |
| `boolean animate` | Whether to animate to the given swipe state. |

### setSwipeState

```
public void <T extends View & RevealableListItem> setSwipeState(
    int swipeState,
    T revealView,
    boolean animate
)
```

Sets the state to swipe the `https://developer.android.com/reference/com/google/android/material/listitem/SwipeableListItem` child to.

| Parameters |
|---|---|
| `int swipeState` | The state to swipe to. This must be one of `https://developer.android.com/reference/com/google/android/material/listitem/SwipeableListItem#STATE_CLOSED()`, `https://developer.android.com/reference/com/google/android/material/listitem/SwipeableListItem#STATE_OPEN()`, or `https://developer.android.com/reference/com/google/android/material/listitem/SwipeableListItem#STATE_SWIPE_PRIMARY_ACTION()` |
| `T revealView` | The `https://developer.android.com/reference/com/google/android/material/listitem/RevealableListItem` view to reveal when swiping. |
| `boolean animate` | Whether to animate to the given swipe state. |

### updateAppearance

```
public void updateAppearance(@ListItemLayout.Position int position)
```

Helper method that sets the drawable state of the ListItemLayout according to its position in the list. This is already called by bind if the ListItemLayout is inside of a `https://developer.android.com/reference/com/google/android/material/listitem/ListItemViewHolder`.

Children of ListItemLayout that wish to be affected by this state should duplicate its parent's state.

### updateAppearance

```
public void updateAppearance(int position, int itemCount)
```

Helper method that sets the drawable state of the ListItemLayout according to its position in the list. This can be called via `https://developer.android.com/reference/com/google/android/material/listitem/ListItemViewHolder#bind(int,int)` if the ListItemLayout is inside of a `https://developer.android.com/reference/com/google/android/material/listitem/ListItemViewHolder`.

Children of ListItemLayout that wish to be affected by this state should duplicate its parent's state.

## Protected methods

### onCreateDrawableState

```
protected int[] onCreateDrawableState(int extraSpace)
```

### onLayout

```
protected void onLayout(boolean changed, int left, int top, int right, int bottom)
```