amit srivastava
amit srivastava

Reputation: 743

Unexpected space is coming in recyclerview items

This is silly problem but I could not find what I am doing wrong.

I am using FirebaseRecyclerAdapter to fill recyclerView but there is unexpected space is coming with items. enter image description here

Fragment 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;
}

Fragment layout:

<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"
    />

List item layout:

<?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>

Please suggest.

Upvotes: 1

Views: 440

Answers (3)

meh
meh

Reputation: 22479

With the support library 23.2 google introduced a change to the 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

You need to change the height of the item to wrap_content instead of 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>

Upvotes: 1

Jaskaran Singh
Jaskaran Singh

Reputation: 157

In list item layout change the linear layout 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>

Upvotes: 1

Moulesh
Moulesh

Reputation: 2810

In listitemlayout xml file make the height of linear layout to wrap content

Upvotes: 6

Related Questions