回收器视图项,它未显示波纹效果/触摸突出显示,我通过使用手势吸引器应用了侦听器 [英] recycler View Item it is not showing Ripple effect/touch highlight, i applied Listener by using gesture Detractor

查看:85
本文介绍了回收器视图项,它未显示波纹效果/触摸突出显示,我通过使用手势吸引器应用了侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

recyclerView.addOnItemTouchListener(新的MyListClickListener(getContext(),新的MyListClickListener.ClickListener(){ @Override public void onClick(查看v,int位置){

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);

        }
    }));

公共类MyListClickListener实现RecyclerView.OnItemTouchListener {

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);
}

}

推荐答案

我认为您正在寻找涟漪效应.只需在drawable&中创建一个xml文件(ripple.xml).将此代码写在

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>

&只需在您已声明recyclerview元素的另一个xml文件中写两行.

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

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

就我而言,我只有TextView,而在父标记中,我声明了这两行

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>

最终结果:

观看此视频:

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

这篇关于回收器视图项,它未显示波纹效果/触摸突出显示,我通过使用手势吸引器应用了侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆