简体   繁体   English

回收站中的间距查看项目

[英]spacing in recycler view item

i am facing a problem i cannot resolve. 我面临一个我无法解决的问题。 i googled it but couldnt get the solution im looking for. 我用谷歌搜索,但无法得到即时解决方案。 i have a recycle view as follow: 我有一个回收视图,如下所示:

<android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:paddingBottom="70dp"
        android:layout_marginTop="@dimen/activity_vertical_margin"

this recycle view is showing in the screen as follow 该回收视图在屏幕上显示如下 在此处输入图片说明

if you notice, there is no spacing above the text and bottom of the text for each item. 如果您注意到,每个项目的文本上方和文本底部都没有空格。 most likely due to wrap_content. 最有可能是由于wrap_content。 i want to add space within the item cell on top and bottom. 我想在顶部和底部的项目单元格内添加空间。 something like this image 像这张图片 在此处输入图片说明

if you noticed, i draw red arrows to indicate the extra space and the text in the center of each item list. 如果您注意到了,我会在每个项目列表的中心绘制红色箭头指示额外的空间和文本。 how can i add space within the cell(space on top of text and space on bottom of text? left and right space will be cool too. 我如何在单元格内添加空间(文本顶部的空间和文本底部的空间?左右空间也会很酷。

when i googled this, i only found code to add spacing between items. 当我用谷歌搜索时,我只找到在项目之间添加间距的代码。 but what i am looking for is to add spacing within the cell item itself like the second picture attach. 但是我正在寻找的是像第二张图片一样在单元格项本身中增加间距。 i would appreciate your help. 我将感谢您的帮助。 thanks in advance 提前致谢

You would have to add a padding to your recycler item. 您将必须在回收项目中添加填充。 If you're using a default item layout from android I would suggest creating your own layout. 如果您使用的是Android的默认项目布局,建议您创建自己的布局。

Definitely you are using an adapter for your recycler view and that adapter is responsible to create children. 无疑,您在回收者视图中使用的是适配器,而该适配器负责创建子代。 As @cocored said you have to create your own layout. 正如@cocored所说,您必须创建自己的布局。 you have to do it in your adapter (usually in onCreateViewHolder). 您必须在适配器中执行此操作(通常在onCreateViewHolder中)。 you can use inflater service to inflate an xml layout for each child. 您可以使用充气服务为每个孩子充气xml布局。

recyclerview_child.xml recyclerview_child.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="10dp">

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"/>

    ...

</LinearLayout>

and in your adapter do something like this 并在您的适配器中执行类似的操作

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {

    private List<Whatever> mData;
    private LayoutInflater mInflater;

    MyRecyclerViewAdapter(Context context, List<Whatever> data) {
        this.mInflater = LayoutInflater.from(context);
        this.mData = data;
    }

    // inflates the child layout from xml when needed
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.recyclerview_child, parent, false);
        return new ViewHolder(view);
    }

    // binds the data to the TextView in each child
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Whatever obj = mData.get(position);
        holder.myTextView.setText(obj.getName());
        ...
    }

    // total number of children
    @Override
    public int getItemCount() {
        return mData.size();
    }

    // stores and recycles views as they are scrolled off screen
    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView myTextView;

        ViewHolder(View itemView) {
            super(itemView);
            myTextView = itemView.findViewById(R.id.tv);
        }
    }
}

hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM