8

RecyclerView のアイテム レイアウトを clickable="true" に設定し、一部のタッチ イベントを完全に消費するように見えますMotionEvent.ACTION_DOWN(その後の ACTION_MOVE と ACTION_UP は機能しています)。

item.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/demo_item_container"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:background="?android:attr/selectableItemBackground"
    android:clickable="true"> <-- this what breaks touch event ACTION_DOWN

....    
</LinearLayout>

onCreate() で非常に基本的な RecyclerView を設定する:

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);    
... //Standard recyclerView init stuff

//Please note that this is NOT recyclerView.addOnItemTouchListener()
recyclerView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                Log.d("", "TOUCH ---  " + motionEvent.getActionMasked());
                //Will never get here ACTION_DOWN when item set to android:clickable="true" 
                return false;
            }
      });

RecyclerView のこれは意図された動作またはバグですか? まだプレビュー段階なのでしょうか?

PS。これをドキュメントに従ってクリック可能にして、押された状態に反応し、クリック時に波及効果を持たせたいです。false に設定すると、ACTION_DOWN は正常に動作しますが、押された状態はトリガーされず、selectableBackground は効果がありません。

4

1 に答える 1

0

これは意図された動作であり、バグではありません。

アイテムのクリッカブルを true に設定すると、ACTION_DOWN が消費され、リサイクラー ビューは ACTION_DOWN を取得しません。

リサイクラー ビューの onTouch() で ACTION_DOWN が必要なのはなぜですか? 必要ですか?ACTION_DOWN で lastY を設定したい場合は、これはどうですか

    case MotionEvent.ACTION_MOVE:
        if (linearLayoutManager.findFirstCompletelyVisibleItemPosition() == 0) {
        // initial
        if (lastY == -1)
            lastY = y;

        float dy = y - lastY;
        // use dy to do your work

        lastY = y;
        break;
    case:MotionEvent.ACTION_UP:
        // reset
        lastY = -1;
        break;

したいですか?それでも ACTION_DOWN が必要な場合は、次のようにアクティビティで取得してみてください。

 public boolean dispatchTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN)
    lastY = ev.getRawY();
    return super.dispatchTouchEvent(ev);
于 2016-05-10T09:50:44.203 に答える