我希望能够将View.OnclickListener
设置为我ViewPager
的页面(片段)内的ImageView
,我不想单击我的ViewPager
的整个页面我只想能够单击specifig ImageView
。我遵循了这些问题(onClick在Linearlayout上没有与Child触发,如何在ViewPager中设置OnClickListener)但是他们没有帮助我。这是我的实际代码:
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(我使用黄油刀约束视图)
@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!");
}
});
}
感谢您的支持。
要考虑的一件事是将您的onclicklistener移至您的parent linearlayout,您正在设置为可单击,看看是否有帮助。
另一件事是将android:focusableInTouchMode="true"
添加到您将onclicklistener附加到的视图中。
除此之外,您可能需要共享注册OnClickListener的代码,以便我们可以调查其他可能的错误。
如果我理解正确,您想单击MyPageFragment.xml
内的图像视图。这很简单,但是您需要将ID
给ImageView
并在onCreateView
方法中引用LinearLayout
,然后在该图像上使用onClickListener
。
因此,通过最近的对话,似乎在线性布局中,您需要删除行
clickable="false"
基本上,它阻止了其孩子的所有点击,因此线性布局以及ImageView lights不适合您。请参阅此代码,我删除了该行。希望这有效
遵循此代码。
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!");
}
});
}
我使用以下代码块解决了此问题:
@Override
public void onViewCreated(View view, Bundle savedInstanceState){
view.setOnClickListener(new OnClickListener(){
public void onClick(View v){
/* Do as you please here. */
}
});
}
可以在此处查看此答案的引用:如何在ViewPager的Pageradapter(Android)中查看单击ImageView?