RecyclerView内容显示在BottomNavBar

时间:2018-11-09 17:12:30

标签: android android-studio android-recyclerview navigationbar

我有一个带有RecyclerView和BottomNavBar的布局:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true">
    </android.support.v7.widget.RecyclerView>

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

当我开始活动时,最后一个卡片视图显示在导航栏:https://drive.google.com/open?id=1vPwKTwswDSTrf5IJga54fTahUB1j9hni后面。 当我在RecyclerView中添加“ android:layout_above =“ @ + id / mainNav”“行时,屏幕上出现了一些奇怪的现象,我的RecyclerView出现了很多错误:https://drive.google.com/open?id=1vI6eOBIf6tQe0rw6wFDDO2phYNvrxRwX。你能帮我吗 ? <3

cardview.xml:

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

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/text_nom"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10dp"
        android:textStyle="bold" />
    <TextView
        android:id="@+id/text_lieu"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10dp"/>
</LinearLayout>
<TextView
    android:id="@+id/text_day"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:textStyle="bold"
    android:textSize="20sp"
    android:padding="10dp" />
<TextView
    android:id="@+id/text_heure"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end"
    android:padding="10dp" />
</android.support.v7.widget.CardView>

Planning.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_planning);
    db = FirebaseFirestore.getInstance();
    db.collection("Planning").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            ListNom.clear();
            ListHeure.clear();
            ListLieu.clear();
            Day.clear();
            if (task.isSuccessful()) {
                for (DocumentSnapshot document : task.getResult()) {
                    ListNom.add(document.getString("Nom"));
                    ListHeure.add(document.getString("Heure"));
                    ListLieu.add(document.getString("Lieu"));
                    Day.add(document.getString("Day"));
                    RecyclerViewAdapter();
                }
            }
        }
    });
    mainNav = (BottomNavigationView)findViewById(R.id.mainNav);
    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;
            }
        }
    });
}

public void RecyclerViewAdapter() {
    RecyclerViewAdapter adapter = new RecyclerViewAdapter(ListNom, ListHeure, ListLieu, Day);
    RecyclerView myView = (RecyclerView) findViewById(R.id.recyclerview);
    myView.setHasFixedSize(true);
    myView.setAdapter(adapter);
    LinearLayoutManager llm = new LinearLayoutManager(Planning.this);
    llm.setOrientation(LinearLayoutManager.VERTICAL);
    myView.setLayoutManager(llm);
}
}

RecyclerViewAdapter.java:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {

public ArrayList<String> ListNom;
public ArrayList<String> ListHeure;
public ArrayList<String> ListLieu;
public ArrayList<String> Day;
public RecyclerViewAdapter (ArrayList<String> ListNom, ArrayList<String> ListHeure, ArrayList<String> ListLieu, ArrayList<String> Day){
    this.ListNom= ListNom;
    this.ListHeure= ListHeure;
    this.ListLieu= ListLieu;
    this.Day= Day;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View listItem = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview, parent, false);
    return new MyViewHolder(listItem);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    if(Day.get(position) == null){ holder.cardView.setCardBackgroundColor(Color.parseColor("#ffffff")); holder.myTextView.setText(ListNom.get(position)); }else {
        holder.cardView.setCardBackgroundColor(Color.parseColor("#cac8a0"));
        holder.myTextView4.setText(ListNom.get(position)); }
    holder.myTextView2.setText(ListHeure.get(position));
    holder.myTextView3.setText(ListLieu.get(position));
    if(holder.myTextView3.getText().toString().trim().isEmpty()){ holder.myTextView3.setVisibility(View.GONE); }
}


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

public static class MyViewHolder extends RecyclerView.ViewHolder {
    private TextView myTextView;
    private TextView myTextView2;
    private TextView myTextView3;
    private TextView myTextView4;
    private CardView cardView;
    public MyViewHolder(View itemView) {
        super(itemView);
        myTextView = (TextView)itemView.findViewById(R.id.text_nom);
        myTextView2 = (TextView)itemView.findViewById(R.id.text_heure);
        myTextView3 = (TextView)itemView.findViewById(R.id.text_lieu);
        myTextView4 = (TextView)itemView.findViewById(R.id.text_day);
        cardView = (CardView)itemView.findViewById(R.id.CardVieww);
    }
}
}

2 个答案:

答案 0 :(得分:0)

如果要使用“约束”布局,则可以通过将以下行添加到xml中来将列表视图约束为在栏的顶部结束:

app:layout_constraintBottom_toTopOf="@+id/mainNav" 

编辑:您可以通过在设计模式下进入布局文件,然后在组件树上右键单击相对布局,然后选择将布局转换为约束布局,在Android Studio中轻松地在一个约束中转换布局。剩下的事。

Here有关约束布局的更多信息

答案 1 :(得分:0)

将此属性添加到RecyclerView

android:layout_above="@+id/mainNav"

因此它将保持在BottomNavigationView内部RelativeLayout上方
编辑

public void onComplete(@NonNull Task<QuerySnapshot> task) {
    ListNom.clear();
    ListHeure.clear();
    ListLieu.clear();
    Day.clear();
    if (task.isSuccessful()) {
        for (DocumentSnapshot document : task.getResult()) {
            ListNom.add(document.getString("Nom"));
            ListHeure.add(document.getString("Heure"));
            ListLieu.add(document.getString("Lieu"));
            Day.add(document.getString("Day"));
        }
        setRecyclerViewAdapter();
    }
}