Android RecyclerView 中条目间距过大

8
我正在使用RecyclerView从一个对象列表中加载数据,每当我在EditText中按下“Enter”键时,该列表就会被填充。但我面临的问题是,我添加的第一项显示正常,当我添加第二项时,它显示出第一项和第二项之间的很大空隙,如果我不断添加项目,空隙就变得越来越大,而列表无法适应整个屏幕。

这是我的适配器:

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();
    }
}

这是适配器的 XML 内容:
<?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>

这是我活动中RecyclerView的XML代码:

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

这是我在活动中定义 recyclerView 的方式:

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

我使用以下代码在我的Activity中填充它:

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;
                            }
                        });
4个回答

45

你的适配器正在使用 layout_height="match_parent" 来填充每一行,这意味着每一行的高度将对应于 RecyclerView 的完整高度。也许你希望改用 wrap_content

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

非常感谢!我不知道这个,它起作用了。 - Marcos Guimaraes

2
只需要在你自定义布局的列表行的父布局中添加android:layout_margin属性。最初的回答。

0
我遇到了同样的错误。我通过更改我的list_view类的layout_height来解决它,问题得到了解决。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">

-1

Recyclerview 中,将高度更改为自适应内容:

<androidx.recyclerview.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
</androidx.recyclerview.widget.RecyclerView>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接