RecyclerView在项目之间创建边距

San*_*los 1 android material-design android-recyclerview

首先,对不起,如果这是一个愚蠢的问题.我不是很懒.所以,问题是,我试图在Android应用程序中实现CardView/RecyclerView.我做到了,但问题是卡片彼此隔开,我不知道如何解决它.我探索了代码,但一切似乎都很好.

代码 :

RecyclerView

<android.support.v7.widget.RecyclerView
        android:id="@+id/collapsing_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        android:padding="@dimen/activity_horizontal_margin">

 </android.support.v7.widget.RecyclerView>
Run Code Online (Sandbox Code Playgroud)

CardView项目

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    card_view:cardBackgroundColor="@color/colorAccent"
    android:layout_gravity="center"
    android:layout_width="fill_parent"
    android:layout_height="100dp"
    android:layout_margin="5dp"
    card_view:cardCornerRadius="2dp">

    <TextView
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="New Text"
        android:id="@+id/cardview.name" />

</android.support.v7.widget.CardView>


</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

适配器:

public class MyRecyclerViewAdapter extends RecyclerView
    .Adapter<MyRecyclerViewAdapter
    .DataObjectHolder> {
private static String LOG_TAG = "MyRecyclerViewAdapter";
private ArrayList<CardViewItem> mDataset;
private static MyClickListener myClickListener;

public static class DataObjectHolder extends RecyclerView.ViewHolder
        implements View
        .OnClickListener {
    TextView label;

    public DataObjectHolder(View itemView) {
        super(itemView);
        label = (TextView) itemView.findViewById(R.id.cardview_name);
        Log.i(LOG_TAG, "Adding Listener");
        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        myClickListener.onItemClick(getAdapterPosition(), v);
    }
}

public void setOnItemClickListener(MyClickListener myClickListener) {
    this.myClickListener = myClickListener;
}

public MyRecyclerViewAdapter(ArrayList<CardViewItem> myDataset) {
    mDataset = myDataset;
}

@Override
public DataObjectHolder onCreateViewHolder(ViewGroup parent,
                                           int viewType) {
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.example_card_view, parent, false);

    DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
    return dataObjectHolder;
}

@Override
public void onBindViewHolder(DataObjectHolder holder, int position) {
    holder.label.setText(mDataset.get(position).getName());;
}

public void addItem(CardViewItem dataObj, int index) {
    mDataset.add(index, dataObj);
    notifyItemInserted(index);
}

public void deleteItem(int index) {
    mDataset.remove(index);
    notifyItemRemoved(index);
}

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

public interface MyClickListener {
    public void onItemClick(int position, View v);
}
}
Run Code Online (Sandbox Code Playgroud)

希望你们能帮助我.谢谢!

编辑:所以,我找到了答案.coulprit是我的cardview项目中的android:layout_height ="match_parent".我将其更改为"wrap_content"并修复了问题.谢谢你的帮助!:)

MrL*_*ond 9

你试过ItemDecoration使用RecyclerView吗?这对于在RecyclerView中自定义分隔符非常有用

您可以看一下:我的自定义ItemDecoration使用getItemOffsets()如下:

public class MyItemDecoration extends RecyclerView.ItemDecoration {

    private final int decorationHeight;
    private Context context;

    public MyItemDecoration(Context context) {
        this.context = context;
        decorationHeight = context.getResources().getDimensionPixelSize(R.dimen.decoration_height);
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);

        if (parent != null && view != null) {

            int itemPosition = parent.getChildAdapterPosition(view);
            int totalCount = parent.getAdapter().getItemCount();

            if (itemPosition >= 0 && itemPosition < totalCount - 1) {
                outRect.bottom = decorationHeight;
            }

        }

    }
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以将您设置为您R.dimen.decoration_height想要的值.

在我的活动中,当我实例化我的RecyclerListView时,我需要这样做:

myList.addItemDecoration(MyItemDecoration(this))
myList.setLayoutManager(LinearLayoutManager(this))
myList.setAdapter(adapter)
Run Code Online (Sandbox Code Playgroud)

我希望这能帮到您