繁体   English   中英

android 可见对于线性布局不起作用

[英]android visible for linearlayout does not work

我创建了一个带有地址的回收站视图。 当这个 recyclerview 为空时,想要一个特殊的 LinearLayout 出现,而当 recyclerView 有一个或多个项目时,这个 LinearLayout 消失。但它不起作用。 我使用函数 setVisibility()。 请帮忙!!!
我的 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) {

    }
}

我的适配器代码是:

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

您的代码中似乎有一个错误,我已修复它。

错误是您首先检查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) {
    
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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