0

Je souhaite créer une activité de tiroir contenant une image. Maintenant, je veux que le parent de la vue de l'image, c'est-à-dire content_main2, qui est la mise en page relative, devrait écouter les événements tactiles comme glisser et lancer. mais il ne reçoit que le toucher.NavDrawer ne donne pas de contact avec d'autres vues

Mon activité principale est une activité de tiroir Nav.

Mon activité principale

import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.util.Log; 
import android.view.GestureDetector; 
import android.view.MotionEvent; 
import android.view.View; 
import android.support.design.widget.NavigationView; 
import android.support.v4.view.GravityCompat; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.RelativeLayout; 
import android.widget.Toast; 

public class Main2Activity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 
    RelativeLayout content_main2; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main2); 
     content_main2=(RelativeLayout)findViewById(R.id.content_main2); 

     final GestureDetector gdt=new GestureDetector(this,onGestureListener); 

     content_main2.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View view, MotionEvent motionEvent) { 
       gdt.onTouchEvent(motionEvent); 
       Toast.makeText(getApplicationContext(),"Touched",Toast.LENGTH_SHORT).show(); 
       return false; 
      } 
     });  

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
       this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
     drawer.setDrawerListener(toggle); 
     toggle.syncState(); 

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(this); 
    } 

    @Override 
    public void onBackPressed() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 
     } else { 
      super.onBackPressed(); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main2, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     int id = item.getItemId(); 

     if (id == R.id.nav_camera) { 
      // Handle the camera action 
     } else if (id == R.id.nav_gallery) {  
     } else if (id == R.id.nav_slideshow) {  
     } else if (id == R.id.nav_manage) {  
     } else if (id == R.id.nav_share) {  
     } else if (id == R.id.nav_send) {  
     } 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    }  

    private final GestureDetector.SimpleOnGestureListener onGestureListener = new GestureDetector.SimpleOnGestureListener() { 
     @Override 
     public boolean onDoubleTap(MotionEvent e) { 
      Log.i("gestureDebug333", "doubleTapped:" + e); 
      return super.onDoubleTap(e); 
     } 

     @Override 
     public boolean onDoubleTapEvent(MotionEvent e) { 
      Log.i("gestureDebug333", "doubleTappedEvent:" + e); 

      return super.onDoubleTapEvent(e); 
     } 

     @Override 
     public boolean onDown(MotionEvent e) { 
      Log.i("gestureDebug333", "onDown:" + e); 
      return super.onDown(e);  
     } 

     @Override 
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {  

      Log.i("gestureDebug333", "flinged:" + e1 + "---" + e2); 
      Log.i("gestureDebug333", "fling velocity:" + velocityX + "---" + velocityY); 
      if (e1.getAction() == MotionEvent.ACTION_DOWN && e1.getX() > (e2.getX() + 300)){ 
       Toast.makeText(getApplicationContext(), "flinged right to left", Toast.LENGTH_SHORT).show(); 
      } 
      if (e1.getAction() == MotionEvent.ACTION_DOWN && e2.getX() > (e1.getX() + 300)){ 
       Toast.makeText(getApplicationContext(), "flinged left to right", Toast.LENGTH_SHORT).show(); 

      } 
      return super.onFling(e1, e2, velocityX, velocityY); 
     } 

     @Override 
     public void onLongPress(MotionEvent e) { 
      super.onLongPress(e); 
     } 

     @Override 
     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { 
      return super.onScroll(e1, e2, distanceX, distanceY); 
     } 

     @Override 
     public void onShowPress(MotionEvent e) { 
      super.onShowPress(e); 
     } 

     @Override 
     public boolean onSingleTapConfirmed(MotionEvent e) { 
      return super.onSingleTapConfirmed(e); 
     } 

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

Mon xml d'activité (activity_main2.xml):

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="start"> 

    <include 
     layout="@layout/app_bar_main2" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true" 
     app:headerLayout="@layout/nav_header_main2" 
     app:menu="@menu/activity_main2_drawer" /> 

</android.support.v4.widget.DrawerLayout> 

app_bar_main2.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="com.example.my_workstation.fabbystore1.Main2Activity"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

    </android.support.design.widget.AppBarLayout> 

    <include layout="@layout/content_main2" /> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|end" 
     android:layout_margin="@dimen/fab_margin" 
     app:srcCompat="@android:drawable/ic_dialog_email" /> 

</android.support.design.widget.CoordinatorLayout> 

content_main2.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/content_main2" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.example.my_workstation.fabbystore1.Main2Activity" 
    tools:showIn="@layout/app_bar_main2"> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:srcCompat="@mipmap/ic_launcher" 
     android:adjustViewBounds="true" 
     android:id="@+id/imageView2" /> 
</RelativeLayout> 

Répondre

1

Ajouté juste,
android:focusableInTouchMode="true"
android:focusable="true"
android:clickable="true"

dans content_main2 (RelativeLayout).
Tout fonctionne maintenant.