按照建议,我用RecyclerView替换了我的ListView。但现在我无法使项目点击动画正常工作。长按动画是有效的,但如果我只轻触项目,则无法获得轻触动画。
我尝试了这里找到的不同解决方案,但没有帮助。我的代码有什么问题?
包含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>
这是列表项的布局:(我还尝试过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>
在我的 Activity 的 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
}
})
);