首页 文章

recycleler查看项目它没有显示Ripple效果/触摸高亮,我使用手势Detractor应用了Listener

提问于
浏览
-1

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 {

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

}

2 回答

  • 2

    我认为你正在寻找涟漪效应 . 只需在drawable中创建一个xml文件(ripple.xml)并在里面写下这段代码

    <?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文件中写两行 .

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

    就像在我的情况下我只有TextView和父标签我声明这两行

    <?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>
    

    最后结果:
    enter image description here

    观看此视频:

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

  • 1

    您只需在Recyclar项目布局根视图中添加两行

    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>
    

相关问题