0

Я первый раз попробовал имплементировать CardView в RecyclerView и вроде как все работает, но между карточками огромные пробелы, не пойму в чем ошибка

введите сюда описание изображения

Вот код

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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">

<android.support.v7.widget.CardView
    android:id="@+id/cv"
    app:cardElevation="15dp"
    app:cardCornerRadius="5dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardBackgroundColor="@color/standard_white">

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

        <ImageView
            android:id="@+id/ivPersonPhoto"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginEnd="16dp" />

        <TextView
            android:id="@+id/tvPersonName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toEndOf="@+id/ivPersonPhoto"
            android:textColor="@color/black_color"
            android:textSize="30sp" />

        <TextView
            android:id="@+id/tvPersonNumber"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tvPersonName"
            android:layout_toEndOf="@+id/ivPersonPhoto"
            android:textColor="@color/black_color" />
    </RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>

Вот адаптер

public class FriendsAdapter extends RecyclerView.Adapter<FriendsAdapter.FriendViewHolder> {

List<CardFriend> friends;

public FriendsAdapter(List<CardFriend> friends) {
    this.friends = friends;
}

public static class FriendViewHolder extends RecyclerView.ViewHolder {
    CardView cv;
    TextView friendName;
    TextView friendNumber;
    ImageView friendPhoto;

    FriendViewHolder(View itemView) {
        super(itemView);
        cv = (CardView) itemView.findViewById(R.id.cv);
        friendName = (TextView) itemView.findViewById(R.id.tvPersonName);
        friendNumber = (TextView) itemView.findViewById(R.id.tvPersonNumber);
        friendPhoto = (ImageView) itemView.findViewById(R.id.ivPersonPhoto);
    }
}

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

@Override
public FriendViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_friend, viewGroup, false);
    FriendViewHolder pvh = new FriendViewHolder(v);
    return pvh;
}

@Override
public void onBindViewHolder(FriendViewHolder friendViewHolder, int i) {
    friendViewHolder.friendName.setText(friends.get(i).getFriendName());
    friendViewHolder.friendNumber.setText(friends.get(i).getFriendNumber());
    friendViewHolder.friendPhoto.setImageResource(friends.get(i).getPhotoId());
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}
}

вот фрагмент

public class FriendsFragment extends Fragment {

private static List<CardFriend> friends;

static {
    friends = new ArrayList<>();
    friends.add(new CardFriend("Emma Wilson", "0505343434", R.drawable.ic_add_white_24dp));
    friends.add(new CardFriend("Lavery Maiss", "0505333232", R.drawable.ic_add_white_24dp));
    friends.add(new CardFriend("Lillie Watts", "0504444444", R.drawable.ic_add_white_24dp));
}

public FriendsFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_friends, container, false);

    RecyclerView rvFriends = (RecyclerView) rootView.findViewById(R.id.rvFriends);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
    rvFriends.setLayoutManager(layoutManager);

    FriendsAdapter friendsAdapter = new FriendsAdapter(friends);
    rvFriends.setAdapter(friendsAdapter);

    return rootView;
}

}

Разметка Recycler

<FrameLayout   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:id="@+id/contentFrame"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activities.FriendsActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <include layout="@layout/tool_bar_left_padding" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways"
            app:tabIndicatorColor="@android:color/background_light"
            app:tabSelectedTextColor="@android:color/background_light"
            app:tabTextColor="@android:color/background_light" />

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_add_white_24dp"
        app:layout_anchor="@id/pager"
        app:layout_anchorGravity="bottom|right|end" />

</android.support.design.widget.CoordinatorLayout>
</FrameLayout>

Что делаю не так?

4
  • 1
    а разметка RecyclerView какая?
    – pavel
    27 июл 2016 в 9:27
  • Не видно разметки Recycler'a, но выглядит будто marginBottom 80dp выставлен
    – Silento
    27 июл 2016 в 9:42
  • @pavel добавил разметку
    – Sirop4ik
    27 июл 2016 в 10:09
  • @Asgard добавил разметку
    – Sirop4ik
    27 июл 2016 в 10:09

2 ответа 2

3

У корневого LinearLayout выставьте android:layout_height="wrap_content"

1
  • Да точно, вот где ошибка. Спасибо!
    – Sirop4ik
    27 июл 2016 в 10:13
0

Если мне не изменяет память, то это все из за FloatingButton. То есть он привязан не совсем корректно из-за чего выходит такой пробел. У меня раньше была такая же проблема. Найди в интернете как правильно привязать кнопку и проблема исчезнет

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.