单击时按钮不会变为可见

时间:2016-08-02 20:05:53

标签: android xml visibility

我有一个按钮,我希望在每次点击图像视图时都可见。 我试过设置Invisible / Visible,但它不起作用。

这是我的代码:

public class FullScreenImageAdapter extends PagerAdapter {

    Button btnClose;
    private Activity _activity;
    private ArrayList<String> _imagePaths;
    private LayoutInflater inflater;

    // constructor
    public FullScreenImageAdapter(Activity activity,
                                  ArrayList<String> imagePaths) {
        this._activity = activity;
        this._imagePaths = imagePaths;
    }

    @Override
    public int getCount() {
        return this._imagePaths.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((RelativeLayout) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        TouchImageView imgDisplay;

        inflater = (LayoutInflater) _activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View viewLayout = inflater.inflate(R.layout.layout_fullscreen_image, container,
                false);
        hideBar();
        imgDisplay = (TouchImageView) viewLayout.findViewById(R.id.imgDisplay);
        btnClose = (Button) viewLayout.findViewById(R.id.btnClose);
        Picasso.with(_activity)
                .load(_imagePaths.get(position))
                .placeholder(R.drawable.loader)
                .fit()
                .centerCrop().into(imgDisplay);

        // close button click event
        btnClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                _activity.finish();
            }
        });

        imgDisplay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideBar();
                hideButton();
            }
        });

        ((ViewPager) container).addView(viewLayout);

        return viewLayout;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((RelativeLayout) object);
    }

    public void hideBar() {
        View decorView = _activity.getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);
    }

    public void hideButton() {
        if (btnClose.isShown()){
            btnClose.setVisibility(View.INVISIBLE);
        }else{
            btnClose.setVisibility(View.VISIBLE);
        }
    }
    }

这是我的xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.example.shaka.we.ImageHelper.helper.TouchImageView
    android:id="@+id/imgDisplay"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"
    android:scaleType="fitCenter" />

<Button
    android:id="@+id/btnClose"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="15dp"
    android:layout_marginTop="15dp"
    android:background="@drawable/redx"
    android:paddingBottom="2dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="2dp"
    android:visibility="invisible"/>

</RelativeLayout>

有人可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

public void hideButton() {
    if (btnClose.isShown()){
        btnClose.setVisibility(View.INVISIBLE);
    }else{
        btnClose.setVisibility(View.VISIBLE);
    }
}

调用isShown不是检查视图是否显示或可见的好方法。检查其实际可见性,如下所示

public void hideButton() {
    if (btnClose.getVisibility() != View.VISIBLE){
        btnClose.setVisibility(View.VISIBLE);
    }else{
        btnClose.setVisibility(View.INVISIBLE);
    }
}

答案 1 :(得分:1)

您可以使用view.getVisibility()view.isShow()检查视图是否可见。但请记住:isShow() returns the visibility of this view and all of its ancestors. True if this view and all of its ancestors are visiblegetVisibility() returns the visibility status for this view, which are VISIBLE, INVISIBLE, GONE. True if this view is visible。在您的情况下,该按钮的父级始终可见,因此btnClose.isShown为真,因此您在单击imageview时始终执行btnClose.setVisibility(View.INVISIBLE);