recycler View Item it is not showing Ripple effect/touch highlight, i applied Listener by using gesture Detractor

走远了吗. 提交于 2020-08-24 03:54:32

问题


recyclerView.addOnItemTouchListener(new MyListClickListener(getContext(), new MyListClickListener.ClickListener() { @Override public void onClick(View v, int position) {

            TextView textView = (TextView) v.findViewById(R.id.title);

            Fragment fragment = ((MyPagerAdapter) viewPager.getAdapter()).getFragment(1);
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.detach(fragment);
            fragment.onDestroy();
            ft.attach(fragment);
            ft.commit();
            comm.sendData(String.valueOf(textView.getText()));
            viewPager.setCurrentItem(1);

        }
    }));

public class MyListClickListener implements RecyclerView.OnItemTouchListener {

private GestureDetector gestureDetector;
ClickListener clickListener;
public MyListClickListener(Context context, ClickListener clickListener){

    this.clickListener = clickListener;
    gestureDetector = new GestureDetector(context,new  GestureDetector.SimpleOnGestureListener(){

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            return true;
        }
    });

}


@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {

    View child = rv.findChildViewUnder(e.getX(),e.getY());
    if(child!= null && clickListener!= null && gestureDetector.onTouchEvent(e)){
        clickListener.onClick(child,rv.getChildPosition(child));
    }
    return true;
}

@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {

}

public interface ClickListener {
    void onClick(View v, int position);
}

}


回答1:


I think you are looking for ripple effect. Just create one xml file (ripple.xml) in drawable & write this code inside

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?attr/colorControlHighlight">
    <item
        android:id="@android:id/mask"
        android:drawable="@android:color/white"
        >

    </item>
</ripple>

& just write two line in your another xml file in which you have declared recyclerview elements.

android:background="@drawable/ripple"
android:clickable="true"

Like in my case i am having only TextView and in parent tag i am declaring these two lines

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/ripple"
    android:clickable="true"
   >

<TextView
    android:id="@+id/textView_Username"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="30sp"
    android:textColor="@color/colorPrimary"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp"
    />
</LinearLayout>

Finally the result:

See this video:

https://www.youtube.com/watch?v=qvVkDb7DqCk




回答2:


you have just add two Lines in Recyclar item layout root view

android:clickable="true"
android:foreground="?android:selectableItemBackground"


<LinearLayout
  android:id="@+id/root"
  android:layout_width="match_parent"
  android:layout_height="30dp"
  android:clickable="true"
  android:foreground="?android:selectableItemBackground">
<TextView
  android:layout_width="match_parent"
  android:layout_height="20dp" />
</LinearLayout>


来源:https://stackoverflow.com/questions/34243426/recycler-view-item-it-is-not-showing-ripple-effect-touch-highlight-i-applied-li

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!