ListItemLayout


public class ListItemLayout


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 SwipeableListItem and up to 2 RevealableListItems. The SwipeableListItem and RevealableListItems must be direct children of the ListItemLayout. RevealableListItems can be set to the start or end of the SwipeableListItem via android:layout_gravity. There cannot be more than 1 RevealableListItem with the same layout_gravity.

Summary

Nested types

Positions for list items.

Constants

static final int

Position for the first item in a list.

static final int

Position for the last item in a list.

static final int

Position for an item in the middle of a list.

static final int

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

Public fields

int

Public constructors

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

Public methods

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

Returns the current SwipeState of the ListItemLayout.

boolean
onInterceptTouchEvent(MotionEvent ev)
boolean
onTouchEvent(MotionEvent ev)
void
onViewRemoved(View child)
void
setSwipeState(int swipeState, int revealGravity)

Sets the state to swipe the SwipeableListItem child to.

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

Sets the state to swipe the SwipeableListItem child to.

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

Sets the state to swipe the SwipeableListItem child to.

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

Sets the state to swipe the SwipeableListItem child to.

void

Helper method that sets the drawable state of the ListItemLayout according to its position in the list.

void
updateAppearance(int position, int itemCount)

Helper method that sets the drawable state of the ListItemLayout according to its position in the list.

Protected methods

int[]
onCreateDrawableState(int extraSpace)
void
onLayout(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 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 SwipeableListItem child to.

Parameters
int swipeState

The state to swipe to. This must be one of STATE_CLOSED, STATE_OPEN, or STATE_SWIPE_PRIMARY_ACTION

int revealGravity

The gravity of the 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 SwipeableListItem child to.

Parameters
int swipeState

The state to swipe to. This must be one of STATE_CLOSED, STATE_OPEN, or STATE_SWIPE_PRIMARY_ACTION

T revealView

The 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 SwipeableListItem child to.

Parameters
int swipeState

The state to swipe to. This must be one of STATE_CLOSED, STATE_OPEN, or STATE_SWIPE_PRIMARY_ACTION

int revealGravity

The gravity of the 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 SwipeableListItem child to.

Parameters
int swipeState

The state to swipe to. This must be one of STATE_CLOSED, STATE_OPEN, or STATE_SWIPE_PRIMARY_ACTION

T revealView

The 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 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 bind if the ListItemLayout is inside of a 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)