Reputation: 2942
As recommended I replaced my ListView by RecyclerView. But now I can't get working the item click animation. Long press animation is working but if I only tap the item I get no tap animation.
I tried different solutions found around here but nothing helped. What is wrong with my code?
The layout that contains the RecyclerView:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/frame_main" tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/recycler_view" />
</RelativeLayout>
This is the list item layout: (I also tried FrameLayout)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="72dp"
android:clickable="true"
android:focusable="true"
android:background="?android:attr/selectableItemBackground">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:id="@+id/cover"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00:00"
android:id="@+id/time"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="false"
android:layout_marginRight="16dp"
android:layout_marginTop="20dp"
style="@style/Widget.AppCompat.ListView"
android:singleLine="true" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="72dp"
android:layout_marginRight="56dp"
android:id="@+id/linearLayout"
android:gravity="center_vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
android:id="@+id/title"
android:textSize="16dp"
style="@style/Widget.AppCompat.ListView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:singleLine="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Artist"
android:id="@+id/artist"
android:textSize="14dp"
style="@style/Widget.AppCompat.ListView"
android:layout_below="@+id/title"
android:layout_alignLeft="@+id/title"
android:layout_alignStart="@+id/title"
android:singleLine="true" />
</LinearLayout>
</RelativeLayout>
Code in my activity's onCreate():
recyclerview = (RecyclerView) findViewById(R.id.recycler_view);
mLayoutManager = new LinearLayoutManager(this);
recyclerview.setLayoutManager(mLayoutManager);
recyclerview.setHasFixedSize(true);
recyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
// enable swipelayout if scrolled to top
try {
int firstPos = mLayoutManager.findFirstCompletelyVisibleItemPosition();
if (firstPos > 0) {
swipeLayout.setEnabled(false);
} else {
swipeLayout.setEnabled(true);
if (recyclerview.getScrollState() == 1 && swipeLayout.isRefreshing()) {
recyclerview.stopScroll();
}
}
} catch (Exception e) {
}
}
});
recyclerview.addOnItemTouchListener(
new RecyclerItemClickListener(MainActivity.this, new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
// do something works
}
})
);
Upvotes: 3
Views: 379
Reputation: 799
I have read through your question, To achieve tap animation you only need to replace the code below found in your list items.
from
android:background="?android:attr/selectableItemBackground"
to
android:foreground="?android:attr/selectableItemBackground"
Hope it helps solve your problem
Upvotes: 1