2

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

1 回答 1

-3

您可以将 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;
            }
        });     
    }
}
于 2013-07-12T15:45:48.713 回答