【问题标题】:OnClick Listener is not triggered inside a fragment with View PagerOnClick 侦听器未在带有 View Pager 的片段内触发
【发布时间】:2017-09-08 15:24:37
【问题描述】:

我希望能够在我的ViewPager 的页面(片段)内将View.OnclickListener 设置为ImageView,我不想点击我的ViewPager 的整个页面,我只是希望能够点击一个规范ImageView。我关注了这些问题(onClick not triggered on LinearLayout with childHow to set OnClickListener in ViewPager ),但它们并没有帮助我。这是我的实际代码:

MyPageFragment.xml

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="240dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:focusableInTouchMode="true"
        android:id="@+id/container_linear"
        android:gravity="center"
        android:orientation="vertical">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleType="centerCrop" />
    </LinearLayout>

</LinearLayout>

MyActivity.xml

<android.support.v4.view.ViewPager
                    android:id="@+id/product_view_pager"
                    android:layout_width="match_parent"
                    android:layout_height="240dp"
                    android:layout_marginLeft="4dp"
                    android:layout_marginRight="4dp"
                    android:gravity="center"
                    android:overScrollMode="never" />

MyFragment.class(我使用 Butterknife 绑定视图)

@BindView(R.id.container_linear)
LinearLayout mContainer;

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View mRootView = inflater.inflate(R.layout.my_layout, container, false);
    ButterKnife.bind(this, mRootView);

    mContainer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("Test", "Clicked!");
        }
    });
}

感谢您的支持。

【问题讨论】:

    标签: android android-layout android-fragments android-viewpager onclicklistener


    【解决方案1】:

    要考虑的一件事是将您的 OnClickListener 移动到您设置为可点击的父 LinearLayout 并查看是否有帮助。

    另一件事是将android:focusableInTouchMode="true" 添加到您将 OnClickListener 附加到的视图中。

    除此之外,您可能还想分享注册 OnClickListener 的代码,以便我们调查其他可能的错误。

    【讨论】:

    • 谢谢,我刚刚实现了您的建议并更新了我的描述
    【解决方案2】:

    如果我理解正确你想点击MyPageFragment.xml里面的图片视图。这很简单,但您需要将 ID 提供给 ImageView 并在您的 onCreateView 方法中引用它,就像您对 LinearLayout 所做的那样,然后在该图像上使用 onClickListener

    所以最近的谈话似乎在线性布局中你需要删除clickable="false"

    基本上它已经阻止了对其子级的所有点击,因此线性布局以及 imageview 点击对您不起作用。请参阅此代码,我删除了该行。希望这有效

    遵循此代码。

    MyPageFragment.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:duplicateParentState="true"
    android:orientation="vertical">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="240dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:focusableInTouchMode="true"
        android:id="@+id/container_linear"
        android:gravity="center"
        android:orientation="vertical">
      
        <--you need to give ID to the view to be able to 
                             perform events on them specifically.-->
        <ImageView
            android:id="@+id/my_awesome_image"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleType="centerCrop" />
    </LinearLayout>
    

    现在在你的 MyFragment.class 中

    @BindView(R.id.container_linear)
    LinearLayout mContainer;
    //declare with butterknife
    @BindView(R.id.my_awesome_image)
    ImageView myAwesomeImage;
    
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View mRootView = inflater.inflate(R.layout.my_layout, container, false);
        ButterKnife.bind(this, mRootView);
        
         //you set click listener previously on entire linear layout instead of imageview 
         //that's why it was not reflecting on imageview click.
    
    
        myAwesomeImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG , "Awesome Imageview clicked!");
            }
        });
    }
    

    【讨论】:

    • 我按照@Bjelis 的建议更改了我的代码。这种使用带有 OnClickListener 的 ImageView 的方法是我的首选。
    • 其实不然,我还是无法使用那些 LinearLayout 或 ImageView 触发 onClickListener
    • 你是否给 imageview 提供了 ID 并在片段中引用了正确的 ID ?
    • 是的,我很确定,因为我使用的是 Butterknife,如果这个库没有找到声明的视图,它会引发错误并且应用程序崩溃。
    • 我在我的代码中实现了这些更改,但它不起作用。我实现了 OnTouchListener,它在 ImageView 中工作。但我不想使用这种方法,因为我在 ViewPager 中使用这个片段。
    【解决方案3】:

    我使用以下代码块解决了这个问题:

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState){
    
        view.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
    
                /*  Do as you please here. */
            }
        });
    }
    

    这个答案的参考可以在这里查看:How to detect click on ImageView in ViewPager's PagerAdapter (android)?

    【讨论】:

      猜你喜欢
      • 2017-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多