2

J'utilise ViewPager du Support Package Android et mon viewpager sur un fragment.Je veux appeler un longlick quand j'appuie longuement sur mon viewpager mais il ne se déclenche pas.Il y a une solution pour ceci ou un autre ?ViewPager setOnLongClickListener ne se déclenche pas

viewPager.setOnLongClickListener(new View.OnLongClickListener() { 

     @Override 
     public boolean onLongClick(View arg0) { 
      Toast.makeText(context, "ViewPager - LongClick!", Toast.LENGTH_SHORT).show(); 
      return false; 
     } 
    }); 

Répondre

-3

Vous pouvez placer ViewPager dans un parent qui prend en charge un événement "Clic long", tel que Linearlayout. Par exemple;

activity_main.xml

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:id="@+id/linearLayout" 
     android:longClickable="true" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
    <android.support.v4.view.ViewPager 
     android:id="@+id/pager" 
     android:layout_width="match_parent" 
     android:layout_height="200dp" 
     android:background="@android:color/darker_gray" 
     /> 
</LinearLayout> 
</LinearLayout> 

MainActivity.java

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     LinearLayout linearLayout=(LinearLayout)findViewById(R.id.linearLayout); 
     linearLayout.setOnLongClickListener(new OnLongClickListener() { 

      @Override 
      public boolean onLongClick(View v) { 
       Toast.makeText(MainActivity.this, "Long Click...", Toast.LENGTH_SHORT).show(); 
       return false; 
      } 
     });  
    } 
} 
+0

Cela ne fonctionne pas. ViewPager consomme l'événement de clic long avant que le parent le voit. – ehartwell