How can I disable all views inside the layout?

后端 未结 23 1109
一个人的身影
一个人的身影 2020-11-28 08:46

For example I have:



        
相关标签:
23条回答
  • 2020-11-28 09:23

    Actully what work for me is:

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
    

    and to undo it:

    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
    
    0 讨论(0)
  • 2020-11-28 09:23

    The easiest way is creating a <View in your xml, with match_parent for height and width, make sure the view is above every other views, then when you want to prevent clicks, make it visible add an onClickListener to that view with null as parameter.

    Example:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
            android:layout_height="fill_parent">
         <Button 
            android:id="@+id/backbutton"
            android:text="Back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <LinearLayout
            android:id="@+id/my_layout"
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/my_text_view"
                android:text="First Name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <EditText
                android:id="@+id/my_edit_view"
                android:width="100px"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" /> 
            
            <View
                android:id="@+id/disable_layout_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:visibility="gone"/>
        </LinearLayout>
    
    </LinearLayout>
    

    Then in your code:

    val disableLayoutView = rootView.find<View>(R.id.disable_layout_view)
    disableLayoutView.visibility = View.VISIBLE
    disableLayoutView.setOnClickListener(null)
    
    0 讨论(0)
  • 2020-11-28 09:24

    Details

    • Android studio 3.1.4
    • Kotlin 1.2.70
    • checked in minSdkVersion 19

    Solution

    fun View.forEachChildView(closure: (View) -> Unit) {
        closure(this)
        val groupView = this as? ViewGroup ?: return
        val size = groupView.childCount - 1
        for (i in 0..size) {
            groupView.getChildAt(i).forEachChildView(closure)
        }
    }
    

    Usage

    val layout = LinearLayout(context!!)
    layout.forEachChildView {  it.isEnabled = false  }
    
    val view = View(context!!)
    view.forEachChildView {  it.isEnabled = false  }
    
    val fragment = Fragment.instantiate(context, "fragment_id")
    fragment.view?.forEachChildView {  it.isEnabled = false  }
    
    0 讨论(0)
  • 2020-11-28 09:24

    My two cents function without recursion

        package io.chord.ui.utils
    
    import android.view.View
    import android.view.ViewGroup
    import androidx.core.view.forEach
    
    class ViewUtils
    {
        companion object
        {
            fun setViewState(view: View, state: Boolean)
            {
                var depth = 0
                val views: MutableMap<Int, MutableList<View>> = mutableMapOf()
                views[depth] = mutableListOf(view)
    
                while(true)
                {
                    val currentViews = views[depth]
                    val nextViews = mutableListOf<View>()
    
                    currentViews!!.forEach { view ->
                        if(view is ViewGroup)
                        {
                            view.forEach { children ->
                                nextViews.add(children)
                            }
                        }
                    }
    
                    if(nextViews.size == 0)
                    {
                        break
                    }
    
                    depth++
    
                    views[depth] = nextViews
                }
    
                views.flatMap {
                    it.value
                }.forEach {
                    it.isEnabled = state
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-28 09:24

    For me RelativeLayout or any other layout at the end of the xml file with width and height set to match_parent with attribute focusable and clickable set to true.

     <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:clickable="true"
                android:focusable="true">
    
            <ProgressBar
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true" />
    
        </RelativeLayout>
    
    0 讨论(0)
  • 2020-11-28 09:26

    Use below recursive function to make your child views visible or gone. First argument is your parent view and second argument decides if you want childs of parent view visible or gone. true = visible false = gone

    private void layoutElemanlarininGorunumunuDegistir(View view, boolean gorunur_mu_olsun) {
        ViewGroup view_group;
        try {
            view_group = (ViewGroup) view;
            Sabitler.konsolaYazdir(TAG, "View ViewGroup imiş!" + view.getId());
        } catch (ClassCastException e) {
            Sabitler.konsolaYazdir(TAG, "View ViewGroup değilmiş!" + view.getId());
            return;
        }
    
        int view_eleman_sayisi = view_group.getChildCount();
        for (int i = 0; i < view_eleman_sayisi; i++) {
            View view_group_eleman = view_group.getChildAt(i);
            if (gorunur_mu_olsun) {
                view_group_eleman.setVisibility(View.VISIBLE);
            } else {
                view_group_eleman.setVisibility(View.GONE);
            }
            layoutElemanlarininGorunumunuDegistir(view_group_eleman, gorunur_mu_olsun);
        }
    }
    
    0 讨论(0)
提交回复
热议问题