簡體   English   中英

使用 Fragment 中的按鈕在 recyclerview 中發送數據

[英]Send data inside recyclerview with button in Fragment

我有 2 個布局,其中 Fragment 的第一個布局和 RecyclerView 的第二個布局。 我在 RecyclerView 中創建了 CardView,每個 CardView 都有一些數據。 我在 RecyclerView 下方創建了按鈕,其中用於在 CardView 上發送數據的按鈕的功能。 我的問題是當我單擊 Fragment 中的按鈕時,我不知道如何發送數據。

片段布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container_daftar_alamat"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:scrollbars="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".ui.menu.menu_signin.menu.MenuAddressFragment">

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <RelativeLayout
                android:layout_marginTop="15dp"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="15dp"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:orientation="horizontal">

                <Button
                    android:id="@+id/buttonMoveTambahAlamat"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentEnd="true"
                    android:background="@null"
                    android:drawableStart="@drawable/ic_tambah_alamat"
                    android:drawablePadding="10dp"
                    android:text="Tambah Alamat"
                    android:textAllCaps="false"
                    android:textColor="@color/colorRed"
                    android:textSize="14sp"
                    android:textStyle="normal" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:gravity="center_vertical"
                    android:text="Daftar Alamat"
                    android:textColor="@color/colorRed"
                    android:textSize="14sp"
                    android:textStyle="bold" />
            </RelativeLayout>

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rc_tambah_alamat"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <Button
                android:id="@+id/gunakanAlamat"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_marginStart="10dp"
                android:layout_marginTop="50dp"
                android:layout_marginEnd="10dp"
                android:layout_marginBottom="100dp"
                android:background="@drawable/bg_button_red"
                android:text="Gunakan Alamat"
                android:textAllCaps="false"
                android:textColor="@android:color/white"
                android:textSize="16sp" />
        </LinearLayout>

    </androidx.core.widget.NestedScrollView>

</FrameLayout>

RecyclerView 布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="12dp"
    android:layout_marginRight="15dp"
    android:layout_marginBottom="12dp"
    android:orientation="vertical"
    app:cardBackgroundColor="@color/colorWhite"
    app:cardCornerRadius="4dp"
    app:cardElevation="5dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="8dp">

        <RadioButton
            android:id="@+id/radioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toEndOf="@+id/radioButton">

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginBottom="8dp">

                <TextView
                    android:id="@+id/nameUser"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:layout_weight="1"
                    android:justificationMode="inter_word"
                    android:text="Muhammad Rafi Bahrur Rizki"
                    android:textColor="@color/colorBlack"
                    android:textSize="14sp"
                    android:textStyle="bold"
                    tools:ignore="UnusedAttribute" />

                <TextView
                    android:id="@+id/addressUser"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:maxLines="1"
                    android:text="(Alamat Kantor)"
                    android:textSize="14sp" />

            </TableRow>

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp">

                <TextView
                    android:id="@+id/streetName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:layout_weight="1"
                    android:justificationMode="inter_word"
                    android:text="Mangga Dua Square Lantai 1 Jakarta,"
                    android:textColor="@color/textColorSaksFifthAvenue"
                    android:textSize="14sp"
                    tools:ignore="UnusedAttribute" />

            </TableRow>

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp">

                <TextView
                    android:id="@+id/blokAddressUser"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:layout_weight="1"
                    android:text="Blok C no. 148 - 150"
                    android:textColor="@color/textColorSaksFifthAvenue"
                    android:textSize="14sp" />

            </TableRow>

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp">

                <TextView
                    android:id="@+id/cityAddressUser"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:layout_weight="1"
                    android:text="DKI Jakarta - 15025"
                    android:textColor="@color/textColorSaksFifthAvenue"
                    android:textSize="14sp" />

            </TableRow>

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp">

                <TextView
                    android:id="@+id/countryAddressUser"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:layout_weight="1"
                    android:text="DKI Jakarta - 15025"
                    android:textColor="@color/textColorSaksFifthAvenue"
                    android:textSize="14sp" />

            </TableRow>

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp">

                <TextView
                    android:id="@+id/phoneUser"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:layout_weight="1"
                    android:text="0812951825"
                    android:textColor="@color/textColorSaksFifthAvenue"
                    android:textSize="14sp" />

            </TableRow>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp">

                <Button
                    android:id="@+id/buttonEdit"
                    android:layout_width="wrap_content"
                    android:layout_height="40dp"
                    android:layout_marginStart="8dp"
                    android:drawableStart="@drawable/ic_edit"
                    android:text="Edit"
                    android:textAllCaps="false"
                    android:textSize="12sp" />

                <Button
                    android:id="@+id/buttonDelete"
                    android:layout_width="wrap_content"
                    android:layout_height="40dp"
                    android:layout_marginStart="10dp"
                    android:layout_toEndOf="@id/buttonEdit"
                    android:drawableStart="@drawable/ic_delete"
                    android:text="Hapus"
                    android:textAllCaps="false"
                    android:textSize="12sp" />

            </RelativeLayout>

        </TableLayout>

    </RelativeLayout>

</androidx.cardview.widget.CardView>

當我選擇 RadioButton 時,只會獲取 RadioButton 中的數據。 當我點擊按鈕時,數據將被發送到另一個活動。

適配器回收器視圖:

public class AdapterGetAddress extends RecyclerView.Adapter<AdapterGetAddress.ViewHolder> {
    Context context;
    private List<ModelGetAddress> modelGetAddressList;
    private BaseApiService baseApiService;
    private int previousSelected = -1;

    public AdapterGetAddress(Context context, List<ModelGetAddress> modelGetAddressList) {
        this.context = context;
        this.modelGetAddressList = modelGetAddressList;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        final ModelGetAddress adapterAddress = modelGetAddressList.get(position);
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView nameUser, addressUser, streetName, blokAddressUser, cityAddressUser, countryUser, phoneUser;
        private RadioButton radioButton;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            nameUser = itemView.findViewById(R.id.nameUser);
            addressUser = itemView.findViewById(R.id.addressUser);
            streetName = itemView.findViewById(R.id.streetName);
            countryUser = itemView.findViewById(R.id.countryAddressUser);
            blokAddressUser = itemView.findViewById(R.id.blokAddressUser);
            cityAddressUser = itemView.findViewById(R.id.cityAddressUser);
            phoneUser = itemView.findViewById(R.id.phoneUser);
            radioButton = itemView.findViewById(R.id.radioButton);

            radioButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    previousSelected = getAdapterPosition();
                    notifyItemRangeChanged(0, modelGetAddressList.size());

                    // I want this All String will be send when i click button in Fragment
                    String getNameUser = nameUser.getText().toString();
                    String getAddressUser = streetName.getText().toString();
                    String getCountryUser = countryUser.getText().toString();
                    String getBlokUser = blokAddressUser.getText().toString();
                    String getCityUser = cityAddressUser.getText().toString();
                    String getPhoneUser = phoneUser.getText().toString();
                }
            });
        }
    }
}

片段中的代碼:

public class MenuAddressFragment extends Fragment {
    private Button gunakanAlamat;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_menu_address, container, false);

        gunakanAlamat = view.findViewById(R.id.gunakanAlamat);
        gunakanAlamat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Here's were i'm stuck
            }
        });

        return view;
    }
}

您可以通過接口解決此問題。

public interface ItemClickedCallback{

    void onItemClicked(String nameUser //, the other data you wanna pass );
}

通過適配器的構造函數傳遞它。

使用此適配器構造函數

ItemClickedCallback callback;
public AdapterGetAddress(Context context, List<ModelGetAddress> modelGetAddressList, ItemClickedCallback callback) {
    this.context = context;
    this.modelGetAddressList = modelGetAddressList;
    this.callback= callback;
}

檢查任何項目的單選按鈕時; 調用接口方法,像這樣。

  radioButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                previousSelected = getAdapterPosition();
                notifyItemRangeChanged(0, modelGetAddressList.size());

                // I want this All String will be send when i click button in Fragment
                String getNameUser = nameUser.getText().toString();
                String getAddressUser = streetName.getText().toString();
                String getCountryUser = countryUser.getText().toString();
                String getBlokUser = blokAddressUser.getText().toString();
                String getCityUser = cityAddressUser.getText().toString();
                String getPhoneUser = phoneUser.getText().toString();

                callback.onItemClicked(getNameUser //, the rest..);

            }
        });

現在,當您在片段中初始化適配器時,通過構造函數創建新接口並將數據分配給這樣的類變量。

public class MenuAddressFragment extends Fragment {
private Button gunakanAlamat;

private String nameUser_;
// the rest...

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_menu_address, container, false);

    AdapterGetAddress adapter = new AdapterGetAddress(getActivity, list, new ItemClickedCallback() {
        @Override
        public void onItemClicked(String userName //, the rest..) {

       // assign to the class variable like this
            nameUser_ = userName;
            //  the rest..

        }
    }););
    gunakanAlamat = view.findViewById(R.id.gunakanAlamat);
    gunakanAlamat.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // Use the data as you want..
        }
    });

    return view;
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM