简体   繁体   中英

android visible for linearlayout does not work

I create one recyclerview with address. When this recyclerview is empty, want one spesicif LinearLayout appear and when recyclerView has one or more items this LinearLayout disapear.But it is not working. I use funtions setVisibility(). Please help!!!
My code for xml is:

<?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>

My code for Activity is:

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) {

    }
}

My adapter code is:

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);
    }
}

It seems there's a bug in your code, I have fixed it.

The bug is you're checking recyclerView is empty first then adding the data into 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) {
    
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM