ViewPager setOnLongClickListener不会触发

时间:2013-07-12 07:16:41

标签: android-viewpager android-support-library onlongclicklistener

我在Android支持包中使用ViewPager,在片段上使用我的viewpager。我想在长时间按下我的viewpager时调用longlick但是它不会触发。是否有针对此或其他方式的解决方案?

viewPager.setOnLongClickListener(new View.OnLongClickListener() {

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

1 个答案:

答案 0 :(得分:-3)

您可以将ViewPager放在支持“Long Click”事件(如Linearlayout)的父级中。例如;

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