【问题标题】:How to Send the data in Bundle from RecyclerView Adapter, if Button is Clicked on Fragment如果在片段上单击按钮,如何从 RecyclerView 适配器发送 Bundle 中的数据
【发布时间】: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


    【解决方案1】:

    让我们暂时假设您只能在回收站视图中选择一张卡。只需将private int mSelectedCardIndex 作为成员变量添加到您的适配器。 onBinderViewHolder 实现点击逻辑的地方只是 onClick 将位置存储到 mSelectedCardIndex 中。还要向您的适配器添加一个 get 方法,以便您可以检索所选卡的数据:

    public BookEntry getSelectedBookEntry(){
        return this.entry.get(mSelectedCardIndex);
    }
    

    当没有选择条目或多个条目由您决定时,您将如何处理。

    然后你可以从你的 Fragment 调用类似的东西(当然是在按钮点击上):

    BookEntry selectedBookEntry = recyclerView.getAdapter().getSelectedBookEntry();
    

    接着是:

    Bundle bundle=new Bundle();
    bundle.putString(selectedBookEntry.getValueX());
    

    并将 args 设置为新的详细信息片段。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多