Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Big space between items in Android RecyclerView

I'm using RecyclerView to load data from a list of objects that is populated every time that I hit "Enter" in an EditText. But the problem that I am facing is that the first item I add it shows OK, when I add the second it shows a big gap between the first and the second, and if I keep adding items the gap gets bigger and the list doesn't fit the whole screen.

This is my Adapter:

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

    private Context mContext;
    private List<Category> categoryList;
    private Category category;

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView title, count;
        public EditText nameCategory;

        public MyViewHolder(View view) {
            super(view);
            nameCategory = (EditText) view.findViewById(R.id.edittext_category);
            //overflow = (ImageView) view.findViewById(R.id.overflow);
        }
    }


    public CategoryAdapter(Context mContext, List<Category> categoryList) {
        this.mContext = mContext;
        this.categoryList = categoryList;
    }

    @Override
    public CategoryAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.adapter_category, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final CategoryAdapter.MyViewHolder holder, final int position) {
        category = categoryList.get(position);
        holder.nameCategory.setText(category.getCategory_name());
    }


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

This is the XML for the Adapter:

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

    <LinearLayout
        android:layout_below="@+id/toolbar"
        android:id="@+id/add_category_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/minus_red"
            android:paddingTop="20dp"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:paddingBottom="20dp"/>

        <EditText
            android:id="@+id/edittext_category"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="@string/add_new_category"
            android:textColorHint="#b1b1b1"
            android:textColor="@color/text_color"
            android:textSize="@dimen/s_text_size"
            android:paddingTop="20dp"
            android:background="#ffffff"
            android:maxLines="1"/>
    </LinearLayout>

</LinearLayout>

This is the XML code for the RecyclerView inside my Activity:

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clipToPadding="false"
    android:scrollbars="vertical"
    android:visibility="invisible"
    android:layout_below="@id/layout_bar"/>

This is how the recyclerView is defined in my Activity:

recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

And I use this code to populate it inside my Activity as well:

edit_category.setOnKeyListener(new OnKeyListener() {
                            public boolean onKey(View v, int keyCode, KeyEvent event) {
                                // If the event is a key-down event on the "enter" button
                                if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                                        (keyCode == KeyEvent.KEYCODE_ENTER)) {

                                    System.out.println("Entrou");
                                    Category category = new Category();
                                    category.setCategory_name(edit_category.getText().toString());
                                    categoriesList.add(category);
                                    adapter = new CategoryAdapter(getApplicationContext(), categoriesList);
                                    recyclerView.setAdapter(adapter);
                                    recyclerView.setVisibility(View.VISIBLE);
                                    edit_category.setText("");



                                    // hide virtual keyboard
                                    InputMethodManager imm =
                                            (InputMethodManager)getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                                    imm.hideSoftInputFromWindow(edit_category.getWindowToken(), 0);
                                    return true;
                                }
                                return false;
                            }
                        });
like image 342
Marcos Guimaraes Avatar asked Aug 29 '17 20:08

Marcos Guimaraes


2 Answers

Your adapter is inflating rows with layout_height="match_parent", this means that each row's height will correspond to the full height of the RecyclerView. Maybe you want to use wrap_content instead:

<?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">
    ...
like image 65
lelloman Avatar answered Sep 20 '22 22:09

lelloman


just add android:layout_margin property in your parent layout of list row of your custom layout

like image 38
Asadullah Mumtaz Avatar answered Sep 19 '22 22:09

Asadullah Mumtaz