RecyclerView问题

时间:2018-11-13 14:41:44

标签: android firebase layout android-recyclerview android-cardview

我遇到了问题,我有一个recyclerview,它可以将firebase-firestore数据显示到cardviews中。一切正常:https://drive.google.com/file/d/1_IE9afCX3A4lPZn_VLRpBV4YtC-9XMWd/view?usp=sharinghttps://drive.google.com/file/d/10sYU6v4Wb94gbKvuOuNf8-hsGpkNJatZ/view?usp=sharing

但是当我在我的Planning布局中添加BottomNavigationBar时,发生了一个非常奇怪的问题:https://drive.google.com/open?id=1SOCTsqLCcQdD431MdBWY5zZo5mUzPxG2https://drive.google.com/open?id=1-xRlPsvuADNYEB9kHqAz45txiwnkR8x_

有人可以帮助我解决此问题吗? <3

这是我的Planning.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1"
xmlns:app="http://schemas.android.com/apk/res-auto">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>
</RelativeLayout>

<android.support.design.widget.BottomNavigationView
    android:id="@+id/mainNav"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    app:itemBackground="@color/colorPrimary"
    app:itemTextColor="@color/white"
    app:itemIconTint="@color/white"
    app:menu="@menu/nav_items">
</android.support.design.widget.BottomNavigationView>

</LinearLayout>

List_menu.xml:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_margin="10dp"
app:cardCornerRadius="10dp"
android:elevation="10dp"
android:id="@+id/cardView"
xmlns:tools="http://schemas.android.com/tools">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/nom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:fontFamily="sans-serif-condensed"
            android:textSize="20sp"/>

        <TextView
            android:id="@+id/lieu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif-condensed"/>
        <TextView
            android:id="@+id/date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="italic"
            android:fontFamily="sans-serif-condensed"/>

    </LinearLayout>

</RelativeLayout>
</android.support.v7.widget.CardView>

Planning.java:

public class Planning extends AppCompatActivity {

FirebaseFirestore db = FirebaseFirestore.getInstance();
RecyclerView recyclerView;
ArrayList<Events> eventsArrayList;
MyRecyclerViewAdapter adapter;
BottomNavigationView mainNav;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_planning);
    mainNav = (BottomNavigationView)findViewById(R.id.mainNav);
    eventsArrayList = new ArrayList<>();
    setUpBottomNav();
    setUpRecyclerView();
    setUpFirebase();
    loadDataFromFirebase();
}

private void setUpRecyclerView(){
    recyclerView = findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
}

private void setUpFirebase(){
    db = FirebaseFirestore.getInstance();
}

private void loadDataFromFirebase(){
    if(eventsArrayList.size()>0){
        eventsArrayList.clear();}
    db.collection("Planning").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            for(DocumentSnapshot querySnapshot: task.getResult()){
                Events Nom = new Events(querySnapshot.getString("Nom"),querySnapshot.getString("Lieu"),querySnapshot.getString("Date"));
                eventsArrayList.add(Nom);
            }
            adapter = new MyRecyclerViewAdapter(Planning.this, eventsArrayList);
            recyclerView.setAdapter(adapter);
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Toast.makeText(Planning.this, "Erreur(s) :", Toast.LENGTH_SHORT).show();
            Log.v("---]---", e.getMessage());
        }
    });
}

private void setUpBottomNav(){
    mainNav.getMenu().getItem(3).setChecked(true);
    mainNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            switch (menuItem.getItemId()){
                case R.id.nav_home :
                    Intent intent = new Intent(Planning.this, Home.class);
                    startActivity(intent);
                    return true;
                case R.id.nav_map :
                    Intent intent2 = new Intent(Planning.this, Localisation.class);
                    startActivity(intent2);
                    return true;
                case R.id.nav_search :
                    Intent intent3 = new Intent(Planning.this, Search.class);
                    startActivity(intent3);
                    return true;
                case R.id.nav_planning :
                    return true;
                case R.id.nav_contact :
                    Intent intent4 = new Intent(Planning.this, Contact.class);
                    startActivity(intent4);
                    return true;
                default:
                    return false;
            }
        }
    });
}
}

MyRecyclerViewHolder.java:

public class MyRecyclerViewHolder extends RecyclerView.ViewHolder {

public TextView tvNom, tvLieu, tvDate;
public CardView cardView;

public MyRecyclerViewHolder(View itemView){
    super(itemView);
    cardView = itemView.findViewById(R.id.cardView);
    tvNom = itemView.findViewById(R.id.nom);
    tvLieu = itemView.findViewById(R.id.lieu);
    tvDate = itemView.findViewById(R.id.date);
}
}

MyRecyclerViewAdapter.java:

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewHolder> {

Planning planning;
ArrayList<Events> eventsArrayList;

public MyRecyclerViewAdapter(Planning planning, ArrayList<Events> eventsArrayList) {
    this.planning = planning;
    this.eventsArrayList = eventsArrayList;
}

@NonNull
@Override
public MyRecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

    LayoutInflater layoutInflater = LayoutInflater.from(planning.getBaseContext());
    View view = layoutInflater.inflate(R.layout.list_item, viewGroup, false);
    return new MyRecyclerViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull MyRecyclerViewHolder holder, int position) {
    holder.tvNom.setText(eventsArrayList.get(position).getNom());
    holder.tvLieu.setText(eventsArrayList.get(position).getLieu());
    if(eventsArrayList.get(position).getLieu() == ""){ holder.tvLieu.setVisibility(View.GONE);}
    if(eventsArrayList.get(position).getDate() == ""){ holder.tvDate.setVisibility(View.GONE);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) holder.tvNom.getLayoutParams();
        params.gravity = Gravity.CENTER;
        holder.tvNom.setLayoutParams(params);
        holder.cardView.setCardBackgroundColor(Color.GRAY);
    }else{holder.tvDate.setText(eventsArrayList.get(position).getDate());}
}

@Override
public int getItemCount() {
    return eventsArrayList.size();
}
}

Events.java:

public class Events {

String Nom;
String Lieu;
String Date;

public Events(String Nom, String Lieu, String Date){
    this.Nom = Nom;
    this.Lieu = Lieu;
    this.Date = Date;
}

public String getNom() {
    return Nom;
}

public void setNom(String nom) {
    Nom = nom;
}

public String getLieu() {
    return Lieu;
}

public void setLieu(String lieu) {
    Lieu = lieu;
}

public String getDate() {
    return Date;
}

public void setDate(String date) {
    Date = date;
}
}

1 个答案:

答案 0 :(得分:0)

在片段中设置recyclerview并将其替换为framelayout 这是一个带有recyclerview片段的容器,并在单击底部工作表按钮时将该片段充气