Java Android按钮在变为可见时不可单击
嗨,我在这里找不到任何可能的答案后发布了这篇文章 我在片段中有一个不可见的线性布局LL1,当recylerView数据为空时,我将其变为可见,LL1上有一个按钮。问题只是按钮不可点击!我尝试用不同的方式设置侦听器,但仍然不起作用。下面是代码: 以下是xml文件:Java Android按钮在变为可见时不可单击,java,android,xml,android-fragments,android-button,Java,Android,Xml,Android Fragments,Android Button,嗨,我在这里找不到任何可能的答案后发布了这篇文章 我在片段中有一个不可见的线性布局LL1,当recylerView数据为空时,我将其变为可见,LL1上有一个按钮。问题只是按钮不可点击!我尝试用不同的方式设置侦听器,但仍然不起作用。下面是代码: 以下是xml文件: <android.support.design.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height
<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();
}
}
private void generatesalList(ArrayList saleArrayList,View){
if(saleArrayList.isEmpty()){
getActivity().setTitle(“便携式通风设备”);
msgLayout.setVisibility(View.VISIBLE);
createsalebtn=(按钮)view.findviewbyd(R.id.btnAddSale);
createSaleBtn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
Toast.makeText(getActivity(),“单击按钮!”,Toast.LENGTH_LONG.show();
}
});
creatSaleBtn.setClickable(真);
}否则{
msgLayout.setVisibility(View.GONE);
recyclerView\u db=view.findViewById(R.id.recyclerView\u sale\u list);
adapter_db=new SaleAdapter((ArrayList)Utils.sortArray(saleArrayList),this,1,getContext(),this);
RecyclerView.LayoutManager LayoutManager=新的LinearLayoutManager(getActivity());
回收视图数据库setLayoutManager(layoutManager);
recyclerView_db.setAdapter(适配器数据库);
适配器\u db.notifyDataSetChanged();
}
}
你们知道是什么导致了这个奇怪的问题吗?你们可以试试
调用此函数可尝试将焦点放在特定视图或其中一个视图上
后代
recyclerview为空后,SwipeRefreshLayout的可见性应消失。
of粗略回收视图为空,但仍然可见,因此您的单击事件会被定义在LL1顶部的SwiperFreshLayout捕获。
编辑
代替recyclerview,将swipe\u refresh\u db visibility设置为gone,因为我怀疑recyclerview是swipe\u refresh\u dd的子项,因此如果将recyclerview的visibility设置为gone,则swipe\u refresh\u db仍然可见并获得单击事件。
尝试一下在没有项目时隐藏回收视图(将可见性设置为“已消失”)。问题是,它正在捕获点击,因为它被放置在按钮的顶部 正如你在评论中所说,你没有使用
createsalebtn.setClickable(false)
anywhere,然后必须使用recyclerView\u db.setVisibility(View.GONE)隐藏recyclerView
代码>
为什么??因为默认情况下,它也适合全屏,因为宽度和高度也与父项匹配
请添加recyclerView\u db.setVisibility(View.GONE)查看saleArrayList.isEmpty()中的code>
因此,您的新代码将是:
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);
}
为什么要添加createsalebtn.setClickable(true)代码>您是否在任何地方禁用它?我的意思是使用createsalebtn.setClickable(false)代码>在代码的其他位置?不,我只是想确保它是可点击的,是的,我在没有设置可点击的情况下尝试了。请检查我的答案AddSale按钮和Fab有不同的侦听器,所以不是这样,只是为了确保我删除了Fab并且仍然不工作。@noah_abou@noah_abou addmsgLayout.invalidate()
链接中提到的android:duplicateParentState=“true”不适用于msgLayout.invalidate(),我试过了,也不适用。。我找不到的地方出了点问题。@noah_abou似乎是个草率的错误,你有没有试图隐藏滑动刷新视图?
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);
}