【问题标题】:ViewPager setOnLongClickListener doesn't fireViewPager setOnLongClickListener 不会触发
【发布时间】:2013-07-12 07:16:41
【问题描述】:

我在片段上使用 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;
        }
    });

【问题讨论】:

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


    【解决方案1】:

    您可以将 ViewPager 放在支持“Long Click”事件的父级中,例如线性布局。例如;

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

    【讨论】:

    • 这不起作用。 ViewPager 在父级看到它之前消耗长点击事件。
    猜你喜欢
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-07
    • 1970-01-01
    • 1970-01-01
    • 2010-11-14
    相关资源
    最近更新 更多