android visible for linearlayout不起作用
我创建了一个带有地址的recyclerview。当此recyclerview为空时,如果出现LinearLayout并当recyclerview有一个或多个项目,则需要一个特定项,但此LinearLayout不起作用。我使用Functions setVisibility()。请帮忙android visible for linearlayout不起作用,android,android-studio,Android,Android Studio,我创建了一个带有地址的recyclerview。当此recyclerview为空时,如果出现LinearLayout并当recyclerview有一个或多个项目,则需要一个特定项,但此LinearLayout不起作用。我使用Functions setVisibility()。请帮忙 我的xml代码是: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&q
我的xml代码是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/user_location_bg_color"
android:orientation="vertical"
tools:context=".UserLocationActivity">
<ImageButton
android:id="@+id/user_location_back_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="50dp"
android:background="@color/my_account_bg_color"
android:contentDescription="@null"
android:src="@drawable/ic_back" />
<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="40dp"
android:text="@string/user_location_text"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold" />
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:background="@color/black" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/coffee_recycler_bg_color" />
<LinearLayout
android:id="@+id/empty_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone"
tools:visibility="invisible">
<pl.droidsonroids.gif.GifImageView
android:id="@+id/location_giffy"
android:layout_width="wrap_content"
android:layout_height="300dp"
android:src="@drawable/location_gif" />
<TextView
android:id="@+id/user_location_blank_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="60dp"
android:layout_marginTop="50dp"
android:layout_marginEnd="60dp"
android:gravity="center"
android:text="@string/user_address_blank_text"
android:textColor="@color/black"
android:textSize="15sp" />
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center|bottom"
android:orientation="vertical">
<Button
android:id="@+id/add_new_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="50dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="10dp"
android:backgroundTint="@color/btn_add_new_address"
android:text="@string/btn_add_new_address_text"
android:textAllCaps="false"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
我的活动代码是:public class UserLocationActivity extends AppCompatActivity implements AddressAdapter.OnAddressListener {
//Init Variables
RecyclerView recyclerAddress;
private FirebaseDatabase db = FirebaseDatabase.getInstance();
private DatabaseReference ref = db.getReference().child("UserAddress");
private AddressAdapter adapter;
private ArrayList<UserLocation> list;
public LinearLayout empty_address;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_location);
recyclerAddress = findViewById(R.id.recycler_address);
recyclerAddress.setHasFixedSize(true);
recyclerAddress.setLayoutManager(new LinearLayoutManager(this));
list = new ArrayList<>();
adapter = new AddressAdapter(this, list, this);
recyclerAddress.setAdapter(adapter);
empty_address = findViewById(R.id.empty_address);
if (adapter.getItemCount() == 0) {
//making it semi-transparent
empty_address.setVisibility(LinearLayout.VISIBLE);
}
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
UserLocation userLocation = dataSnapshot.getValue(UserLocation.class);
list.add(userLocation);
}
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
@Override
public void onAddressItemDelete(int position) {
}
}
公共类UserLocationActivity扩展AppCompatActivity实现AddressAdapter.OnAddressListener{
//初始变量
RecyclerView recyclerAddress;
私有FirebaseDatabase db=FirebaseDatabase.getInstance();
私有DatabaseReference=db.getReference().child(“用户地址”);
专用地址适配器;
私有数组列表;
公共线路显示空地址;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u user\u location);
recyclerAddress=findViewById(R.id.RecyclerU地址);
recyclerAddress.setHasFixedSize(true);
recyclerAddress.setLayoutManager(新的LinearLayoutManager(此));
列表=新的ArrayList();
适配器=新地址适配器(此,列表,此);
recyclerAddress.setAdapter(适配器);
empty_address=findviewbyd(R.id.empty_address);
if(adapter.getItemCount()==0){
//使其半透明
空_address.setVisibility(LinearLayout.VISIBLE);
}
ref.addListenerForSingleValueEvent(新的ValueEventListener(){
@凌驾
public void onDataChange(@NonNull DataSnapshot snapshot snapshot){
对于(DataSnapshot DataSnapshot:snapshot.getChildren()){
UserLocation UserLocation=dataSnapshot.getValue(UserLocation.class);
添加(用户位置);
}
adapter.notifyDataSetChanged();
}
@凌驾
已取消公共void(@NonNull DatabaseError){
}
});
}
@凌驾
地址项delete上的公共无效(int位置){
}
}
我的适配器代码是:public class AddressAdapter extends RecyclerView.Adapter<AddressAdapter.AddressViewHolder> {
private List<UserLocation> listData = new ArrayList<>();
UserLocationActivity userLocationActivity;
private OnAddressListener mOnAddressListener;
public AddressAdapter(UserLocationActivity userLocationActivity, List<UserLocation> listData, OnAddressListener onAddressListener){
this.listData = listData;
this.userLocationActivity = userLocationActivity;
this.mOnAddressListener = onAddressListener;
}
@NonNull
@Override
public AddressViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(userLocationActivity).inflate(R.layout.address_item , parent, false);
return new AddressViewHolder(v,mOnAddressListener);
}
@Override
public void onBindViewHolder(@NonNull AddressViewHolder holder, int position) {
UserLocation userLocation = listData.get(position);
holder.addressItem.setText(userLocation.getUserLocation());
}
@Override
public int getItemCount() {
if (listData.size() == 0){
userLocationActivity.empty_address.setVisibility(LinearLayout.VISIBLE);
}
return listData.size();
}
public static class AddressViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView addressItem;
public ImageView btnAddressItemDelete;
OnAddressListener onAddressListener;
public AddressViewHolder(View itemView, OnAddressListener onAddressListener){
super(itemView);
addressItem = itemView.findViewById(R.id.address_item);
btnAddressItemDelete = itemView.findViewById(R.id.btn_address_item_delete);
this.onAddressListener = onAddressListener;
}
@Override
public void onClick(View v) {
onAddressListener.onAddressItemDelete(getAdapterPosition());
}
}
public interface OnAddressListener{
void onAddressItemDelete(int position);
}
}
公共类AddressAdapter扩展了RecyclerView.Adapter{
private List listData=new ArrayList();
UserLocationActivity UserLocationActivity;
专用OnAddressListener mOnAddressListener;
公共地址适配器(UserLocationActivity UserLocationActivity,List listData,OnAddressListener OnAddressListener){
this.listData=listData;
this.userLocationActivity=userLocationActivity;
this.mOnAddressListener=onAddressListener;
}
@非空
@凌驾
公共地址ViewHolder onCreateViewHolder(@NonNull ViewGroup父级,int-viewType){
视图v=LayoutFlater.from(userLocationActivity)。充气(R.layout.address\u项,父项,false);
返回新的AddressViewHolder(v,mOnAddressListener);
}
@凌驾
公共无效onBindViewHolder(@NonNull AddressViewHolder holder,int位置){
UserLocation UserLocation=listData.get(位置);
holder.addressItem.setText(userLocation.getUserLocation());
}
@凌驾
public int getItemCount(){
if(listData.size()==0){
userLocationActivity.empty_address.setVisibility(LinearLayout.VISIBLE);
}
返回listData.size();
}
公共静态类AddressViewHolder扩展了RecyclerView.ViewHolder实现了View.OnClickListener{
公共文本视图地址项;
公共图像视图btnAddressItemDelete;
OnAddressListener OnAddressListener;
公共地址视图持有者(查看项目视图,OnAddressListener OnAddressListener){
超级(项目视图);
addressItem=itemView.findViewById(R.id.address\u项);
btnAddressItemDelete=itemView.findviewbyd(R.id.btn\u address\u item\u delete);
this.onAddressListener=onAddressListener;
}
@凌驾
公共void onClick(视图v){
onAddressListener.onAddressItemDelete(getAdapterPosition());
}
}
地址侦听器的公共接口{
地址项删除无效(整数位置);
}
}
您的代码中似乎有个bug,我已经修复了 错误在于,首先检查
recyclerView
是否为空,然后将数据添加到recyclerView
public class UserLocationActivity extends AppCompatActivity implements AddressAdapter.OnAddressListener {
//Init Variables
RecyclerView recyclerAddress;
private FirebaseDatabase db = FirebaseDatabase.getInstance();
private DatabaseReference ref = db.getReference().child("UserAddress");
private AddressAdapter adapter;
private ArrayList<UserLocation> list;
public LinearLayout empty_address;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_location);
recyclerAddress = findViewById(R.id.recycler_address);
recyclerAddress.setHasFixedSize(true);
recyclerAddress.setLayoutManager(new LinearLayoutManager(this));
list = new ArrayList<>();
adapter = new AddressAdapter(this, list, this);
recyclerAddress.setAdapter(adapter);
empty_address = findViewById(R.id.empty_address);
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
UserLocation userLocation =
dataSnapshot.getValue(UserLocation.class);
list.add(userLocation);
}
adapter.notifyDataSetChanged();
if (list.size() == 0) {
empty_address.setVisibility(View.VISIBLE);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
@Override
public void onAddressItemDelete(int position) {
}
}
公共类UserLocationActivity扩展AppCompatActivity实现AddressAdapter.OnAddressListener{
//初始变量
RecyclerView recyclerAddress;
私有FirebaseDatabase db=FirebaseDatabase.getInstance();
私有DatabaseReference=db.getReference().child(“用户地址”);
专用地址适配器;
私有数组列表;
公共线路显示空地址;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u user\u location);
recyclerAddress=findViewById(R.id.RecyclerU地址);
recyclerAddress.setHasFixedSize(true);
recyclerAddress.setLayoutManager(新的LinearLayoutManager(此));
列表=新的ArrayList();
适配器=新地址适配器(此,列表,此);
recyclerAddress.setAdapter(适配器);
empty_address=findviewbyd(R.id.empty_address);
ref.addListenerForSingleValueEvent(新的ValueEventListener(){
@凌驾
public void onDataChange(@NonNull DataSnapshot snapshot snapshot){
对于(DataSnapshot DataSnapshot:snapshot.getChildren()){
用户位置用户位置=
getValue(UserLocation.class);
添加(使用)