gpt4 book ai didi

android - Cardview 全部进入一张卡片

转载 作者:行者123 更新时间:2023-11-30 01:34:34 25 4
gpt4 key购买 nike

我正在尝试为将进入学校应用程序的 list 创建卡片 View 。 ArrayList 中的所有项目都进入一张卡片,我无法弄清楚为什么。我已经看过我能找到的每一个教程,但没有运气。这是相关代码。非常感谢任何帮助。

Activity

recyclerView = (RecyclerView) findViewById(R.id.masterListRecView);
recyclerView.setHasFixedSize(true);

layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());

theList = new ArrayList<MasterListItem>();

theList.add(new MasterListItem("Test Item1",new Date(),new Date(),"here are some notes",false));
theList.add(new MasterListItem("Test Item2",new Date(),new Date(),"here are some notes",false));
theList.add(new MasterListItem("Test Item3",new Date(),new Date(),"here are some notes",false));
theList.add(new MasterListItem("Test Item4",new Date(),new Date(),"here are some notes",false));
theList.add(new MasterListItem("Test Item5",new Date(),new Date(),"here are some notes",false));
theList.add(new MasterListItem("Test Item6",new Date(),new Date(),"here are some notes",false));
theList.add(new MasterListItem("Test Item7",new Date(),new Date(),"here are some notes",false));
theList.add(new MasterListItem("Test Item8",new Date(),new Date(),"here are some notes",false));
theList.add(new MasterListItem("Test Item9",new Date(),new Date(),"here are some notes",false));
theList.add(new MasterListItem("Test Item10",new Date(),new Date(),"here are some notes",false));

masterListAdapter = new RecViewAdapter(theList);
recyclerView.setAdapter(masterListAdapter);

masterListAdapter.notifyDataSetChanged();

回收 View 适配器

public class RecViewAdapter extends RecyclerView.Adapter<RecViewAdapter.MyViewHolder> {
TextView title;
TextView dueDate;
TextView completedDate;
TextView notes;
CheckBox completed;
private ArrayList<MasterListItem> theList;
//private LayoutInflater inflater;

public RecViewAdapter(ArrayList<MasterListItem> theList) {
this.theList = theList;
//inflater = LayoutInflater.from(context);
}

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

MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
title = holder.title;
dueDate = holder.dueDate;
completedDate = holder.completedDate;
notes = holder.notes;
completed = holder.completed;

title.setText(theList.get(position).getTitle());
dueDate.setText(theList.get(position).getDueDate().toString());
completedDate.setText(theList.get(position).getCompletedDate().toString());
notes.setText(theList.get(position).getNotes());
completed.setChecked(theList.get(position).isCompleted());
}



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

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}


public class MyViewHolder extends RecyclerView.ViewHolder{
private TextView title;
private TextView dueDate;
private TextView completedDate;
private TextView notes;
private CheckBox completed;
public MyViewHolder(View itemView){
super(itemView);
this.title = (TextView) itemView.findViewById(R.id.masterListTitle);
this.dueDate = (TextView) itemView.findViewById(R.id.masterListDueDate);
this.completedDate = (TextView) itemView.findViewById(R.id.masterListCompletedDate);
this.notes = (TextView) itemView.findViewById(R.id.masterListNotes);
this.completed = (CheckBox) itemView.findViewById(R.id.masterListCompleted);
}
}



}

回收 View XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="edu.uco.weddingcrashers.hitched.MasterWeddingList">

<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:id="@+id/addTaskButton"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />

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



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


</RelativeLayout>

卡片 XML

    <?xml version="1.0" encoding="utf-8"?>


<android.support.v7.widget.CardView
android:id="@+id/masterListCardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
card_view:cardCornerRadius="4dp"
xmlns:android="http://schemas.android.com/apk/res/android">


<!--
android:layout_alignParentStart="true"
android:layout_gravity="center"
android:layout_below="@+id/addTaskButton"
-->


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">


<TextView
android:id="@+id/masterListTitle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="test title" />
<TextView
android:id="@+id/masterListDueDate"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="test title" />
<TextView
android:id="@+id/masterListCompletedDate"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="test title" />
<TextView
android:id="@+id/masterListNotes"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="test title" />
<CheckBox
android:id="@+id/masterListCompleted"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Completed"
android:editable="false"
android:layout_gravity="center_horizontal" />
</LinearLayout>


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

再次感谢您!

最佳答案

您必须在 RecyclerView 项之间添加分隔符。给 RecyclerView 添加分隔符和装饰,与其他 ListView 不同。您可以通过以下答案实现此目的:

与ListView不同,RecyclerView类没有divider相关的参数。相反,您需要扩展 ItemDecoration , RecyclerView 的内部类:

An ItemDecoration allows the application to add a special drawing and layout offset to specific item views from the adapter's data set. This can be useful for drawing dividers between items, highlights, visual grouping boundaries and more.

All ItemDecorations are drawn in the order they were added, before the item views (in onDraw() and after the items (in onDrawOver(Canvas,
RecyclerView, RecyclerView.State)
.

垂直间距ItemDecoration扩展 ItemDecoration,添加以空间高度为参数的自定义构造函数并覆盖 getItemOffsets():

public class VerticalSpaceItemDecoration extends RecyclerView.ItemDecoration {

private final int mVerticalSpaceHeight;

public VerticalSpaceItemDecoration(int mVerticalSpaceHeight) {
this.mVerticalSpaceHeight = mVerticalSpaceHeight;
}

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
RecyclerView.State state) {
outRect.bottom = mVerticalSpaceHeight;
}
}

如果您不想在最后一项下方插入空格,请添加以下条件:

if (parent.getChildAdapterPosition(view) !=
parent.getAdapter().getItemCount() - 1) {
outRect.bottom = mVerticalSpaceHeight;
}

注意:您还可以修改 outRect.top、outRect.left 和 outRect.right 属性以获得所需的效果。

分隔项装饰扩展 ItemDecoration 并覆盖 onDraw():

public class DividerItemDecoration extends RecyclerView.ItemDecoration {
private static final int[] ATTRS = new int[]{android.R.attr.listDivider};

private Drawable mDivider;

/**
* Default divider will be used
*/
public DividerItemDecoration(Context context) {
final TypedArray styledAttributes = context.obtainStyledAttributes(ATTRS);
mDivider = styledAttributes.getDrawable(0);
styledAttributes.recycle();
}

/**
* Custom divider will be used
*/
public DividerItemDecoration(Context context, int resId) {
mDivider = ContextCompat.getDrawable(context, resId);
}

@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
int left = parent.getPaddingLeft();
int right = parent.getWidth() - parent.getPaddingRight();

int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);

RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

int top = child.getBottom() + params.bottomMargin;
int bottom = top + mDivider.getIntrinsicHeight();

mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
}

您可以调用使用默认 Android 分频器属性的第一个构造函数,或者调用使用您自己的 drawable 的第二个构造函数,例如 drawable/divider.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size
android:height="1dp" />
<solid android:color="#ff992900" />
</shape>

注意:如果您希望在项目上绘制分隔线,请改写 onDrawOver()

用法

要使用新类,请将 VerticalSpaceItemDecoration 或 DividerSpaceItemDecoration 添加到 RecyclerView,例如在 fragment 的 onCreateView() 中:

private static final int VERTICAL_ITEM_SPACE = 48;
private RecyclerView mUiRecyclerView;
private LinearLayoutManager mLinearLayoutManager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_feed, container, false);

mUiRecyclerView = (RecyclerView) rootView.findViewById(R.id.fragment_home_recycler_view);
mLinearLayoutManager = new LinearLayoutManager(getActivity());
mUiRecyclerView.setLayoutManager(mLinearLayoutManager);

//add ItemDecoration
mUiRecyclerView.addItemDecoration(new VerticalSpaceItemDecoration(VERTICAL_ITEM_SPACE));
//or
mUiRecyclerView.addItemDecoration(
new DividerItemDecoration(getActivity());
//or
mUiRecyclerView.addItemDecoration(
new DividerItemDecoration(getActivity(), R.drawable.divider));

mUiRecyclerView.setAdapter(...);

return rootView;
}

还有 Lucas Rocha 的库,可以简化元素的装饰过程。虽然还没有尝试过。

它的特点是:

一组库存元素装饰品,包括:

  • 项目间距水平/垂直分隔线。
  • 列表项

关于android - Cardview 全部进入一张卡片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35262066/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com