意外 space 出现在 recyclerview 项目中

Unexpected space is coming in recyclerview items

这是个愚蠢的问题,但我找不到我做错了什么。

我正在使用 FirebaseRecyclerAdapter 来填充 recyclerView,但意外 space 随项目一起提供。

片段class:

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_tags, container, false);

    mProgressBar = (ProgressBar)view.findViewById(R.id.progressBar);
    mTagsRecyclerView = (RecyclerView) view.findViewById(R.id.tagsRecyclerView);
    mLinearLayoutManager = new LinearLayoutManager(getContext());

    mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference();
    mFirebaseAdapter = new FirebaseRecyclerAdapter<Tags, TagsViewHolder>(
            Tags.class,
            R.layout.item_message,
            TagsViewHolder.class,
            mFirebaseDatabaseReference.child("tags")) {

        @Override
        protected void populateViewHolder(TagsViewHolder viewHolder, Tags tags, int position) {
            mProgressBar.setVisibility(ProgressBar.INVISIBLE);
            viewHolder.tagTextView.setText(tags.getTagname());
            viewHolder.usrTextView.setText(tags.getUserid());
        }
    };


    mFirebaseAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
        @Override
        public void onItemRangeInserted(int positionStart, int itemCount) {
            super.onItemRangeInserted(positionStart, itemCount);
        }
    });

    mTagsRecyclerView.setLayoutManager(mLinearLayoutManager);
    mTagsRecyclerView.setAdapter(mFirebaseAdapter);

    mTagsRecyclerView.addOnItemTouchListener(new RecyclerViewItemClickListener(getContext(),
            new RecyclerViewItemClickListener.OnItemClickListener() {
                @Override public void onItemClick(View v, int position) {
                    //TODO:
                    // do something on item click
                    Log.d("Item clicked at",Integer.toString(position));
                }
            }));
    return view;
}

片段布局:

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.helpplusapp.amit.helpplus.TagsFragment">

<android.support.v7.widget.RecyclerView
    android:id="@+id/tagsRecyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />
<ProgressBar
    style="?android:attr/progressBarStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/progressBar"
    android:layout_gravity="center"
    />

列表项布局:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="@drawable/view_separator"
    android:paddingTop="@dimen/margin_default"
    android:paddingBottom="@dimen/margin_default"
    android:showDividers="end">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:id="@+id/tagTextView"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:id="@+id/usrTextView"
            />

</LinearLayout>

求推荐。

在 listitemlayout xml 文件中使线性布局的高度包裹内容

在列表项布局中更改线性布局 android:height="wrap_content"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:divider="@drawable/view_separator"
android:paddingTop="@dimen/margin_default"
android:paddingBottom="@dimen/margin_default"
android:showDividers="end">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/tagTextView"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/usrTextView"
        />
</LinearLayout>

随着支持库 23.2 google 对 recyclerview 进行了更改:

LayoutManager API: auto-measurement! This allows a RecyclerView to size itself based on the size of its contents. This means that previously unavailable scenarios, such as using WRAP_CONTENT for a dimension of the RecyclerView, are now possible. You’ll find all built in LayoutManagers now support auto-measurement. Due to this change, make sure to double check the layout parameters of your item views: previously ignored layout parameters (such as MATCH_PARENT in the scroll direction) will now be fully respected.

Source

您需要将项目的高度更改为 wrap_content 而不是 match_parent。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="@drawable/view_separator"
    android:paddingTop="@dimen/margin_default"
    android:paddingBottom="@dimen/margin_default"
    android:showDividers="end">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:id="@+id/tagTextView"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:id="@+id/usrTextView"
            />

</LinearLayout>