![](/img/trans.png)
[英]Android studio OnTouchListener for RecyclerView item, change item color
[英]Android Recyclerview: onTouchListener on recyclerview to change viewholder color onTouch
在Facebook中,如果您觸摸活動供稿中的某個項目,則該項目會立即變暗以表明您的手指在上面。 如果然后用手指滾動提要,則視口將再次變白。
您還可以在自己的回收站視圖中按住某個項目,並且視圖保持器將變暗。 如果您松開手指,則說明您已“單擊”該項目,Facebook會向您顯示有關單擊內容的更多信息。
這似乎很容易實現,但是我一直在努力。
我認為Facebook使用了onTouchListener來實現效果...
我寫了下面的代碼,但似乎用橙色調使recyclerview的itemView變暗,但是當我拖動或抬起手指時,橙色並沒有變成我想要的透明顏色。
同樣因為recyclerview正在“回收” /重復使用其視圖,所以如果我觸摸列表中的第一項並滾動,則從recyclerview底部出現的其中一個項也將變為橙色,因為該視圖現已移動視線(我單擊的第一個視圖)現在已被底部視圖重用。
這是我的代碼:
((ProfileFeedViewHolder) holder).itemView.setOnTouchListener(new View.OnTouchListener() {
public final static int FINGER_RELEASED = 0;
public final static int FINGER_TOUCHED = 1;
public final static int FINGER_DRAGGING = 2;
public final static int FINGER_UNDEFINED = 3;
private int fingerState = FINGER_RELEASED;
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
Log.e("motionEvent", "motion "+motionEvent.getAction());
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
if (fingerState == FINGER_RELEASED) fingerState = FINGER_TOUCHED;
else fingerState = FINGER_UNDEFINED;
view.setBackgroundColor(ContextCompat.getColor(context, R.color.deep_orange_500));
break;
case MotionEvent.ACTION_UP:
if(fingerState != FINGER_DRAGGING) {
fingerState = FINGER_RELEASED;
view.setBackgroundColor(ContextCompat.getColor(context, R.color.transparent));
// Your onClick codes
}
else if (fingerState == FINGER_DRAGGING) fingerState = FINGER_RELEASED;
else fingerState = FINGER_UNDEFINED;
view.setBackgroundColor(ContextCompat.getColor(context, R.color.transparent));
break;
case MotionEvent.ACTION_MOVE:
if (fingerState == FINGER_TOUCHED || fingerState == FINGER_DRAGGING) {
fingerState = FINGER_DRAGGING;
view.setBackgroundColor(ContextCompat.getColor(context, R.color.transparent));
}
else fingerState = FINGER_UNDEFINED;
break;
default:
fingerState = FINGER_UNDEFINED;
view.setBackgroundColor(ContextCompat.getColor(context, R.color.transparent));
}
return false;
}
});
如何實現這種簡單效果,這種效果在當今大多數應用程序中似乎很流行?
我用谷歌搜索了一下,您實際上可以在xml上執行此操作。 感謝Lawrr和這篇文章: RecyclerView Item中的Background Selector只需將以下內容設置為背景:
android:background="?android:attr/selectableItemBackground"
注意,我下面有兩個relativeLayouts,內容將放入內部的relativeLayout中。 原因是android:background設置itemView的背景色。 我的itemViews都有白色背景,因此我需要將外部relativeLayout設置為白色。 然后,當我觸摸itemView時,將內部relativeLayout設置為?android:attr/selectableItemBackground
背景將導致灰色突出顯示和android漣漪效果。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="@color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/container">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground">
//put in your content here for each itemView
</RelativeLayout>
</RelativeLayout>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.