【发布时间】:2017-09-08 15:24:37
【问题描述】:
我希望能够在我的ViewPager 的页面(片段)内将View.OnclickListener 设置为ImageView,我不想点击我的ViewPager 的整个页面,我只是希望能够点击一个规范ImageView。我关注了这些问题(onClick not triggered on LinearLayout with child,How 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