[英]Hide/Show on Button/Layout on click without affecting imageview zoom
[英]Show hide layout on imageview
我有一個viewpager,我在其中顯示圖像。 它支持捏縮放和雙擊縮放。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linearLayoutViewpager">
<com.bogdwellers.pinchtozoom.view.ImageViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black" />
</LinearLayout>
<RelativeLayout
android:id="@+id/relativelayoutforbuttons"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/save_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Save" />
</RelativeLayout>
// Sudo layout, working as button
<RelativeLayout
android:id="@+id/relativelayoutforvisibility"
android:layout_width="match_parent"
android:layout_height="match_parent">
我希望當用戶打開viewpager時,它顯示完整圖像,如果用戶單擊圖像,則應該從另一個(RelativeLayout)布局中出現一個按鈕。 我創建了一個額外的布局,並在其上設置了OnClickListener。
viewPager = (ImageViewPager) v.findViewById(R.id.viewpager);
final RelativeLayout relativeLayout = (RelativeLayout)
v.findViewById(R.id.relativelayoutforbuttons);
relativeLayout.setVisibility(View.GONE);
RelativeLayout relativelayoutforvisibility = (RelativeLayout)
v.findViewById(R.id.relativelayoutforvisibility);
relativelayoutforvisibility.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(relativeLayout.getVisibility() == View.VISIBLE)
{
relativeLayout.setVisibility(View.GONE);
}
// Show layouts if they're not VISIBLE
else
{
relativeLayout.setVisibility(View.VISIBLE);
}
}
});
myViewPagerAdapter = new MyViewPagerAdapter();
viewPager.setAdapter(myViewPagerAdapter);
viewPager.addOnPageChangeListener(viewPagerPageChangeListener);
setCurrentItem(selectedPosition);
現在我的問題是這段代碼可以正常工作並顯示/隱藏按鈕containig布局。 但這也會影響viewpager。 我無法向左或向右滑動,也無法縮放圖像。 我想要類似全圖預覽的whatsapp,當觸摸時顯示前進按鈕。
輕松解決:將您的
RelativeLayout
高度設置為wrap_content
一旦使您的RelativeLayout
可見,您的觸摸手勢就無法工作的原因是因為您使用的是FrameLayout
,並且“ RelativeLayout
”現在位於ImageView
上方,這invalidate
Touch gestures/events
invalidate
,或者換句話說,它將“ consume
”該觸摸手勢,並且不允許您的ViewPager
使用它。
固定? 使用ConstraintLayout
並將各個視圖(位於RelativeLayout
)與圖像視圖一起放置。
這是一個隨機示例,在您觸摸ImageView
時,您將隱藏兩個按鈕。 我不會發布顯示onClickListeners
的Java代碼。
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView android:layout_width="0dp" android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<Button
android:id="@+id/shareButton"
android:text="Share Button"
android:layout_width="wrap_content" android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
tools:ignore="HardcodedText"/>
<Button
android:id="@+id/sendButton"
android:text="Send Button Button"
android:layout_width="wrap_content" android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:ignore="HardcodedText"/>
<android.support.constraint.Group
android:id="@+id/group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:constraint_referenced_ids="sendButton,shareButton"/>
</android.support.constraint.ConstraintLayout>
然后在您的onClickListener中
yourGroup.setVisiblility(View.GONE);
等等..
更新(用戶要求)
Groups
是ConstraintLayout
的一部分
<android.support.constraint.Group
android:id="@+id/group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
app:constraint_referenced_ids="button4,button9" />
在您的Java代碼中
import android.support.constraint.Group;
然后為了初始化它們
Group group = findViewById(R.id.yourGroupId);
然后設置可見性
group.setVisibility(View.VISIBLE);
這是因為您的relativelayoutforvisibility
占據了所有位置(寬度和高度= match_parent),並且它位於ViewPager的前面,因此此布局具有所有touchs事件,而不會將它們分派到您的ViewPager(位於布局的后面)。 您可以將android:clickable="false"
和android:focusable="false"
到您的relativelayoutforvisibility
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.