首先,抱歉,如果这是一个愚蠢的问题。我不是懒惰的。 所以,问题是,我试图在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>
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>
适配器:
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);
}
}
希望你们能帮助我。谢谢!
编辑:所以,我找到了答案。 coulprit是android:layout_height =&#34; match_parent&#34; 在我的cardview项目中。我改为#34; wrap_content&#34;并解决了问题。谢谢你的帮助! :)答案 0 :(得分:8)
您是否尝试过使用RecyclerView ItemDecoration
?这对于在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;
}
}
}
}
然后,您可以将R.dimen.decoration_height
设置为您想要的值。
在我的活动中,当我实例化我的RecyclerListView时,我需要这样做:
myList.addItemDecoration(MyItemDecoration(this))
myList.setLayoutManager(LinearLayoutManager(this))
myList.setAdapter(adapter)
我希望这会对你有所帮助
答案 1 :(得分:1)
从CardView项目.xml
中删除此行机器人:layout_margin =&#34; 5DP&#34;
您可能希望它是padding
而不是margin
答案 2 :(得分:1)
CardView默认添加填充。尝试使用CardView属性card_view:contentPadding但是为此属性设置负值
card_view:了contentPadding = “ - 3”
答案 3 :(得分:0)
为了在项目之间创建间距,我们可以使用RecyclerView
的{{1}}的:
ItemDecorator
考虑到我粘贴的代码,需要考虑以下几点:
您实际上不需要调用addItemDecoration(object : RecyclerView.ItemDecoration() {
override fun getItemOffsets(
outRect: Rect,
view: View,
parent: RecyclerView,
state: RecyclerView.State,
) {
super.getItemOffsets(outRect, view, parent, state)
if (parent.getChildAdapterPosition(view) > 0) {
outRect.top = 8.dp // Change this value with anything you want. Remember that you need to convert integers to pixels if you are working with dps :)
}
}
})
,但我选择了,因为我想扩展基类定义的行为。如果该库进行了更新,在后台进行了更多的逻辑处理,那么我们将错过它。
作为向super.getItemOffsets
添加顶部间距的替代方法,您还可以添加底部间距,但是与获取适配器的最后一项有关的逻辑更加复杂,因此这可能会更好
我使用了extension属性将简单的整数转换为dps:Rect
。这样的事情可能会起作用:
8.dp