Skip to content
This repository has been archived by the owner on Mar 24, 2023. It is now read-only.

Minimal default RecyclerView animations support #75

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,14 @@ private Rect getDefaultHeaderOffset(RecyclerView recyclerView, View header, View
int translationX, translationY;
Rect headerMargins = mDimensionCalculator.getMargins(header);
if (orientation == LinearLayoutManager.VERTICAL) {
translationX = firstView.getLeft() + headerMargins.left;
translationX = (int) firstView.getX() + headerMargins.left;
translationY = Math.max(
firstView.getTop() - header.getHeight() - headerMargins.bottom,
(int) firstView.getY() - header.getHeight() - headerMargins.bottom,
getListTop(recyclerView) + headerMargins.top);
} else {
translationY = firstView.getTop() + headerMargins.top;
translationY = (int) firstView.getY() + headerMargins.top;
translationX = Math.max(
firstView.getLeft() - header.getWidth() - headerMargins.right,
(int) firstView.getX() - header.getWidth() - headerMargins.right,
getListLeft(recyclerView) + headerMargins.left);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void drawVertical(Canvas c, RecyclerView parent) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int top = Math.max(recyclerViewTop, child.getBottom() + params.bottomMargin);
final int top = Math.max(recyclerViewTop, child.getBottom() + (int) child.getTranslationY() + params.bottomMargin);
final int bottom = Math.min(recyclerViewBottom, top + mDivider.getIntrinsicHeight());
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
Expand All @@ -77,7 +77,7 @@ public void drawHorizontal(Canvas c, RecyclerView parent) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int left = Math.max(recyclerViewLeft, child.getRight() + params.rightMargin);
final int left = Math.max(recyclerViewLeft, child.getRight() + (int) child.getTranslationX() + params.rightMargin);
final int right = Math.min(recyclerViewRight, left + mDivider.getIntrinsicHeight());
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@

public class MainActivity extends Activity {

public static final String ANIMALS_BELOW = "Animals below!";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
Button button = (Button) findViewById(R.id.button_update);
Button buttonAdd = (Button) findViewById(R.id.button_add);
final ToggleButton isReverseButton = (ToggleButton) findViewById(R.id.button_is_reverse);

// Set adapter populated with example dummy data
Expand All @@ -57,6 +60,18 @@ public void run() {
}
});

// Add/remove item to the top of list (Test animations)
buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ANIMALS_BELOW.equals(mAdapter.getItem(0))) {
mAdapter.remove(ANIMALS_BELOW);
} else {
mAdapter.add(0, ANIMALS_BELOW);
}
}
});

// Set layout manager
int orientation = getLayoutManagerOrientation(getResources().getConfiguration().orientation);
final LinearLayoutManager layoutManager = new LinearLayoutManager(this, orientation, isReverseButton.isChecked());
Expand Down Expand Up @@ -122,7 +137,7 @@ private class SampleArrayHeadersAdapter extends RecyclerArrayAdapter<String, Rec
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.view_item, parent, false);
.inflate(R.layout.view_item, parent, false);
return new RecyclerView.ViewHolder(view) {
};
}
Expand All @@ -145,7 +160,7 @@ public long getHeaderId(int position) {
@Override
public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.view_header, parent, false);
.inflate(R.layout.view_header, parent, false);
return new RecyclerView.ViewHolder(view) {
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public M getItem(int position) {

@Override
public long getItemId(int position) {
return position;
return getItem(position).hashCode();
}

@Override
Expand Down
8 changes: 8 additions & 0 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
android:layout_weight="1"
android:text="@string/button_update"/>

<Button
android:id="@+id/button_add"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="14sp"
android:text="@string/button_add"/>

<ToggleButton
android:id="@+id/button_is_reverse"
android:layout_width="0dp"
Expand Down
3 changes: 2 additions & 1 deletion sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
<resources>

<string name="app_name">StickyHeadersRecyclerView</string>
<string name="button_update">Start Update</string>
<string name="button_update">Update</string>
<string name="button_reverse">Is reverse</string>
<string name="button_add">Add/Remove</string>

</resources>