Issue with SearchBar and RecyclerView and CardView

2019-08-26 21:01发布

问题:

I'm using the SearchBar following the tutorial Android Development Tutorial - Search on SQLite data. I have a problem with the layout. Please see attached image. I'm using CardView and RecyclerView. Can you please tell me what is wrong in my layout?

check image

main_activity.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MainActivity">

    <com.mancj.materialsearchbar.MaterialSearchBar
        android:id="@+id/search_bar"
        android:layout_alignParentTop="true"
        app:mt_speechMode="false"
        app:mt_hint="Search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyler_search"
        android:layout_below="@+id/search_bar"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v7.widget.RecyclerView>

</RelativeLayout>

layout_item.xml

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:cardElevation="0dp"
    android:layout_margin="9dp">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_margin="8dp"
        android:background="@android:color/white"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:src="@drawable/ic_account_circle_black_24dp"
            android:layout_width="70dp"
            android:layout_height="70dp" />

        <LinearLayout
            android:orientation="vertical"
            android:layout_weight="9"
            android:layout_width="0dp"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/name"
                android:layout_marginLeft="10dp"
                android:gravity="center_vertical|start"
                android:textAllCaps="true"
                android:textStyle="bold"
                android:text="Eddy Lee"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/email"
                android:layout_marginLeft="10dp"
                android:gravity="center_vertical|start"
                android:textStyle="italic"
                android:text="eddylee@aol.com"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/phone"
                android:layout_marginLeft="10dp"
                android:gravity="center_vertical|start"
                android:textAllCaps="true"
                android:textStyle="bold"
                android:text="(222)888-888"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/address"
                android:layout_marginLeft="10dp"
                android:gravity="center_vertical|start"
                android:textAllCaps="true"
                android:textStyle="normal"
                android:text="new york"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

            </TextView>


        </LinearLayout>
    </LinearLayout>

Thank you for attention

回答1:

update your view and take parent height wrap_content

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardElevation="0dp"
    android:layout_margin="9dp">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_margin="8dp"
        android:background="@android:color/white"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:src="@drawable/ic_account_circle_black_24dp"
            android:layout_width="70dp"
            android:layout_height="70dp" />

        <LinearLayout
            android:orientation="vertical"
            android:layout_weight="9"
            android:layout_width="0dp"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/name"
                android:layout_marginLeft="10dp"
                android:gravity="center_vertical|start"
                android:textAllCaps="true"
                android:textStyle="bold"
                android:text="Eddy Lee"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/email"
                android:layout_marginLeft="10dp"
                android:gravity="center_vertical|start"
                android:textStyle="italic"
                android:text="eddylee@aol.com"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/phone"
                android:layout_marginLeft="10dp"
                android:gravity="center_vertical|start"
                android:textAllCaps="true"
                android:textStyle="bold"
                android:text="(222)888-888"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/address"
                android:layout_marginLeft="10dp"
                android:gravity="center_vertical|start"
                android:textAllCaps="true"
                android:textStyle="normal"
                android:text="new york"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

            </TextView>


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


回答2:

Change your layout_item.xml CardView height="wrap_content"

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardElevation="0dp"
    android:layout_margin="9dp">

    ...........................

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


回答3:

Use wrap_content for cardView in layout_item

<android.support.v7.widget.CardView
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:layout_width="match_parent"
   android:layout_height="wrap_content "
   app:cardElevation="0dp"
   android:layout_margin="9dp">