陳昇昇
陳昇昇

Reputation: 35

Layout setVisibility() in java no effect

this is the floagingview.It can drag...
I need to set the layout to hide or show in the code.
problem : listview_left、listview_left no way to hide or show

FloatingView.java
This code has hidden settings but has no effect.
listview_left.setVisibility(View.GONE) and listview_right.setVisibility(View.GONE)

public FloatingView(Context context, FloatingViewConfig config) {
    this.mContext = context;
    mWindowManager = (WindowManager) context.getSystemService(WINDOW_SERVICE);
    rootView = LayoutInflater.from(context).inflate(R.layout.playground_float_view, null, false);

    this.config = config;
    if (config.displayWidth == Integer.MAX_VALUE) {
        DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
        config.displayWidth = metrics.widthPixels;
    }
    if (config.displayHeight == Integer.MAX_VALUE) {
        DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
        config.displayHeight = (int) (metrics.heightPixels - 25 * metrics.density);
    }
    config.paddingLeft = dp2px(config.paddingLeft);
    config.paddingTop = dp2px(config.paddingTop);
    config.paddingRight = dp2px(config.paddingRight);
    config.paddingBottom = dp2px(config.paddingBottom);

    rootView.measure(0, 0);
    width = rootView.getMeasuredWidth();
    height = rootView.getMeasuredHeight();

    screen_widht = mWindowManager.getDefaultDisplay().getWidth();
    screen_height = mWindowManager.getDefaultDisplay().getHeight();
    mFloatLayout = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.playground_float_view, null);
    listview_left =  mFloatLayout.findViewById(R.id.listview_left);
    listview_right =  mFloatLayout.findViewById(R.id.listview_right);
    x = 0;
    y = 0;
    listview_left.setVisibility(View.GONE);
    listview_right.setVisibility(View.GONE);
    Log.d(TAG,"初始化(left,right) : " + listview_left.getVisibility() + ", " + listview_right.getVisibility());
}

float_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/float_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/floating_layout_shape"
    android:orientation="horizontal">

    <LinearLayout
        android:id="@+id/listview_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="visible">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/floating_view_list2" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/floating_view_list3" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/floating_view_list1" />
    </LinearLayout>


    <ImageView
        android:id="@+id/float_image"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/floating_view_menu"/>

    <LinearLayout
        android:id="@+id/listview_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/float_image"
        android:visibility="visible">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/floating_view_list1" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/floating_view_list3" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/floating_view_list2" />
    </LinearLayout>


</LinearLayout>



This is the complete code and it is run!!!
link: https://github.com/sheng855174/Test
How can i solve?

Upvotes: 1

Views: 80

Answers (2)

Dandre Allison
Dandre Allison

Reputation: 6035

The answer here is good, but maybe it was misinterpreted. I will try to explain it differently here https://stackoverflow.com/a/60161868/910718

mFloatLayout should be completely replaceable with rootView, inflating the layout twice means you have 2 completely separate versions of that layout, one in mFloatLayout and another in rootView. Changes to mFloatLayout would not change anything to the Views in rootView.

I see that the view is added here:

    public void showOverlayActivity() {
    if (isShowing) {
        return;
    }
    type = TYPE.OVERLAY_ACTIVITY;
    initParams();
    initPosition();
    initWindowView();
    isShowing = true;
    mWindowManager.addView(rootView, mParamsWindowManager);
}

So the View that is rendered on the screen is not the View where you have extracted the listview_left and listview_right from, because as stated, the second inflate is creating a completely new instance of the layout with no association with the layout that is being added to the screen in this code.

I would encourage that FloatingView extend LinearLayout, that the outer LinearLayout is replaced with a merge tag, and that View.inflate like this: inflate(context, R.layout.playground_float_view, this). This makes the FloatingView equal the float_view layout and the mFloatLayout and rootView become unnecessary.

Upvotes: 2

Dipankar Baghel
Dipankar Baghel

Reputation: 2039

you don't need to inflate playground_float_view 2 times.

just remove below line from your code -

mFloatLayout = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.playground_float_view, null);

and get listview_left and listview_left like below -

listview_left =  (LinearLayout)rootView.findViewById(R.id.listview_left);
listview_right =  (LinearLayout)rootView.findViewById(R.id.listview_right);

hope this will help!!

Upvotes: 1

Related Questions