【问题标题】:Android button not clickable when turning visible变为可见时无法单击Android按钮
【发布时间】:2020-08-10 02:29:49
【问题描述】:

您好,我在这里没有找到任何可能的答案后发布此内容。

我在片段中有一个不可见的 LinearLayout LL1,当 recylerView 数据为空时,它变为可见,LL1 上有一个按钮。问题只是按钮不可点击!我尝试以不同的方式设置侦听器,但仍然无法正常工作。下面是代码:

这是 xml 文件:

 <android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.main.MainActivity"
    android:layout_marginTop="3dp">

    <LinearLayout
        android:id="@+id/msg_emty_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:visibility="gone"
        android:orientation="vertical">

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/ic_empty_box" />
        <TextView
            android:layout_marginTop="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Aucune vente n'a été trouvée !"
            android:textSize="15dp"
            android:layout_gravity="center_horizontal"
            android:textColor="@color/greydark"/>
        <Button
            android:id="@+id/btnAddSale"
            android:layout_marginTop="5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Créer une vente"
            android:textColor="@color/whiteTextColor"
            android:background="@drawable/centre_button">

        </Button>

    </LinearLayout>

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_refresh_db"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view_sale_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    </android.support.v4.widget.SwipeRefreshLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_add_db"
        android:layout_margin="20dp"
        android:layout_gravity="bottom|end"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorAccesGreen"
        android:src="@drawable/ic_edit"/>


</android.support.design.widget.CoordinatorLayout>

这是java:

   private void generateSaleList(ArrayList<Sale> saleArrayList, View view) {
    if(saleArrayList.isEmpty()){
        getActivity().setTitle("Ventes sur portable");
        msgLayout.setVisibility(View.VISIBLE);
        creatSaleBtn = (Button) view.findViewById(R.id.btnAddSale);
        creatSaleBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getActivity(), "Button clicked !", Toast.LENGTH_LONG).show();
            }
        });
        creatSaleBtn.setClickable(true);

    }else{
        msgLayout.setVisibility(View.GONE);
        recyclerView_db = view.findViewById(R.id.recycler_view_sale_list);
        adapter_db = new SaleAdapter((ArrayList<Sale>) Utils.sortArray(saleArrayList),this,1,getContext(),this);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recyclerView_db.setLayoutManager(layoutManager);
        recyclerView_db.setAdapter(adapter_db);
        adapter_db.notifyDataSetChanged();
    }

}

你们知道是什么导致了这个奇怪的问题吗?

【问题讨论】:

  • 为什么要添加creatSaleBtn.setClickable(true); 你是否在任何地方禁用它?我的意思是在代码的其他地方使用creatSaleBtn.setClickable(false);?
  • 不,我只是想确保它是可点击的,是的,我在没有 setClickable 的情况下尝试过
  • 请检查我的答案

标签: java android xml android-fragments android-button


【解决方案1】:

你可以试试requestFocusFromTouch()

调用它以尝试将焦点放在特定视图或其一个 后代。

 msgLayout.setVisibility(View.VISIBLE);
 creatSaleBtn = (Button) view.findViewById(R.id.btnAddSale);
 creatSaleBtn.requestFocusFromTouch();

【讨论】:

  • @noah_abou 添加msgLayout.invalidate();
  • 链接中提到的 android:duplicateParentState="true" 不起作用
  • 关于 msgLayout.invalidate() 我试过了,也没有用.. 哪里有问题我找不到它..
  • @noah_abou 似乎草率的错误会让你付出代价
【解决方案2】:

当没有项目时隐藏您的 RecyclerView(将可见性设置为消失)。问题是它正在捕获点击,因为它被放置在按钮的顶部。

【讨论】:

    【解决方案3】:

    正如你在 cmets 中所说,你没有在任何地方使用 creatSaleBtn.setClickable(false);,那么你必须使用 recyclerView_db.setVisibility(View.GONE); 隐藏 recyclerView

    为什么?因为默认情况下它也适合全屏 match_parent 的宽度和高度。

    请在saleArrayList.isEmpty()检查中添加recyclerView_db.setVisibility(View.GONE);

    所以你的新代码将是:

    if(saleArrayList.isEmpty()){
        recyclerView_db.setVisibility(View.GONE);
        getActivity().setTitle("Ventes sur portable");
        msgLayout.setVisibility(View.VISIBLE);
        creatSaleBtn = (Button) view.findViewById(R.id.btnAddSale);
        creatSaleBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getActivity(), "Button clicked !", Toast.LENGTH_LONG).show();
            }
        });
        creatSaleBtn.setClickable(true);
    
    }
    

    【讨论】:

    • 您是否尝试隐藏滑动刷新视图?
    【解决方案4】:

    在 recyclerview 为空后,SwipeRefreshLayout 的可见性应该消失。
    粗略的 recyclcerview 是空的,但它仍然可见,因此您的点击事件被 SwipeRefreshLayout 捕获,女巫是在 LL1 之上定义的。
    编辑
    而不是 recyclerview,将 swipe_refresh_db 可见性设置为消失,因为我怀疑 recyclerview 是 swipe_refresh_dd 的子级,所以如果你将 recyclerview 的可见性设置为消失,swipe_refresh_db 仍然可见并获得点击事件。
    试试看

    【讨论】:

    • AddSaleButton 和 Fab 有不同的侦听器,所以不是这样,只是为了确保我删除了 FAB 并且仍然无法正常工作。
    猜你喜欢
    • 2016-12-08
    • 2014-06-13
    • 1970-01-01
    • 2017-10-15
    • 2017-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-21
    相关资源
    最近更新 更多