【发布时间】:2018-09-11 14:55:08
【问题描述】:
我在 Fragment 上的RecyclerView 中设置了数据。我在 RecyclerView 下方的底部有一个 Button。单击按钮后,我想在选中 Checkbox 后从个别卡中获取一些详细信息。之后从Fragment单击按钮我想使用Bundles将卡片详细信息发送到下一个片段。
这是设置RecyclerView 和底部Button 的代码
<FrameLayout 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:orientation="vertical"
tools:context="sam.sam.MyBook">
<!-- TODO: Update blank fragment layout -->
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
<Button
android:id="@+id/assignTable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000"
android:textColor="#fff"
android:textSize="18sp"
android:layout_marginBottom="10dp"
android:layout_gravity="center|bottom"
android:layout_alignParentBottom="true"
android:text="Assign"/>
</FrameLayout>
这是适配器的代码
public class BookingAdapter extends RecyclerView.Adapter<BookingAdapter.ViewHolder> {
private ArrayList<BookEntry> entry;
Context context;
String id;
public BookingAdapter(ArrayList<BookEntry> entry, Context context) {
this.entry = entry;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.booking_card, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
final BookEntry currentEntry = entry.get(position);
holder.name.setText(currentEntry.getName());
holder.description.setText(currentEntry.getDescription());
holder.capacity.setText(currentEntry.getCapacity());
holder.tableId.setText(currentEntry.getId());
holder.check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String id=entry.get(position).getId();
final boolean isChecked=holder.check.isChecked();
if(isChecked)
{
Bundle bundle=new Bundle();
bundle.putString("tableIds",id);
TableAssignConfirm confirm=new TableAssignConfirm();
confirm.setArguments(bundle);
}
}
});
}
@Override
public int getItemCount() {
return entry.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView name, description, capacity,tableId;
final CheckBox check;
public ViewHolder(final View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.name);
description = (TextView) itemView.findViewById(R.id.description);
capacity = (TextView) itemView.findViewById(R.id.capacity);
tableId=(TextView)itemView.findViewById(R.id.tableId);
check=(CheckBox)itemView.findViewById(R.id.book);
check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isCheck=check.isChecked();
if(isCheck)
Log.i("isCheck","I am Check");
Log.i("id",id);
}
});
}
}
}
如何做到这一点?
【问题讨论】:
标签: android android-fragments android-recyclerview android-button android-bundle