在适配器中创建自定义对话框

Posted

技术标签:

【中文标题】在适配器中创建自定义对话框【英文标题】:Create Custom Dialog in an Adapter 【发布时间】:2021-05-09 05:28:09 【问题描述】:

我正在尝试显示一个点击卡片的自定义对话框(我使用 recyclerview 和 firebase)。我有一个适配器,其中有 onClick 方法。我想在其中放置自定义对话框,以便在单击卡片时将其打开。

这里是 ArticoloAdapter.java

public class ArticoloAdapter extends RecyclerView.Adapter<ArticoloAdapter.VersionVH> 

    private List<Articolo> listaArticoli;
    private Context mCtx;

    public ArticoloAdapter(Context mCtx, List<Articolo> listaArticoli)
        this.mCtx = mCtx;
        this.listaArticoli = listaArticoli;
    

    @NonNull
    @Override
    public ArticoloAdapter.VersionVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.articolo_stock, parent, false);
        return new VersionVH(view);
    

    @Override
    public void onBindViewHolder(@NonNull final ArticoloAdapter.VersionVH holder, int position) 
        final Articolo articolo = listaArticoli.get(position);
        holder.nome.setText(articolo.getNome());

        if(articolo.getQuantita() == 1)
            String quantita = Integer.toString(articolo.getQuantita()) + " Disponibile";
            holder.quantita.setText(quantita);
         else 
            String quantita = Integer.toString(articolo.getQuantita()) + " Disponibili";
            holder.quantita.setText(quantita);
        
        Picasso.get().load(articolo.getImg()).into(holder.img);
    

    public int getItemCount()
        return listaArticoli.size();
    

    public class VersionVH extends RecyclerView.ViewHolder 
        private CardView cardView;
        private ImageView img;
        private TextView nome, quantita;

        public VersionVH(View itemView)
            super(itemView);

            nome = itemView.findViewById(R.id.nomeArticolo);
            quantita = itemView.findViewById(R.id.quantita);
            img = itemView.findViewById(R.id.immagine);
            cardView = itemView.findViewById(R.id.cardView);

            cardView.setOnClickListener(new View.OnClickListener() 
                @Override
                public void onClick(View v) 
                    //here i want to put my custom_dialog
                
            );
        
    

这是我的 StockFragment(我从 Firebase 中检索到我的卡):

public class StockFragment extends Fragment 

    private RecyclerView recyclerView;
    private List<Articolo> listaArticoli;
    private ArticoloAdapter articoloAdapter;
    private DatabaseReference query;
    private LinearLayoutManager mLayoutManager;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) 
        View root = inflater.inflate(R.layout.fragment_stock, container, false);
        mLayoutManager = new LinearLayoutManager(getActivity());
        mLayoutManager.setReverseLayout(true);
        mLayoutManager.setStackFromEnd(true);
        recyclerView = root.findViewById(R.id.recycleViewStock);
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setAdapter(articoloAdapter);
        listaArticoli = new ArrayList<>();

        query = FirebaseDatabase.getInstance().getReference("Articoli");
        query.addListenerForSingleValueEvent(valueEventListener);

        return root;
    

    ValueEventListener valueEventListener = new ValueEventListener() 
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) 
            listaArticoli.clear();
            for(DataSnapshot snapshot : dataSnapshot.getChildren())
                Articolo articolo = snapshot.getValue(Articolo.class);
                listaArticoli.add(articolo);
            
            articoloAdapter = new ArticoloAdapter(getActivity(), listaArticoli);
            recyclerView.setAdapter(articoloAdapter);
        

        @Override
        public void onCancelled(@NonNull DatabaseError error) 
    ;

这是我的 custom_dialog.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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_
    android:layout_
    android:background="@drawable/dialog_bg"
    tools:ignore="UseSwitchCompatOrMaterialXml">

    <ImageView
        android:id="@+id/imageView2"
        android:layout_
        android:layout_
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.06999999"
        app:srcCompat="@android:drawable/sym_def_app_icon">
    </ImageView>

    <TextView
        android:id="@+id/textView2"
        android:layout_
        android:layout_
        android:layout_marginStart="146dp"
        android:layout_marginEnd="146dp"
        android:text="Nome Prodotto"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.19999999">

    </TextView>

    <com.google.android.material.textview.MaterialTextView
        android:id="@+id/materialTextView"
        android:layout_
        android:layout_
        android:layout_marginStart="32dp"
        android:layout_marginEnd="32dp"
        android:gravity="center_horizontal"
        android:inputType="textMultiLine"
        android:text="Ciao questo è un testo di prova per daniele e vedere se gli piace o meno come esce"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.3">

    </com.google.android.material.textview.MaterialTextView>


    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/textInputLayout"
        style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.Dense"
        android:layout_
        android:layout_
        android:layout_marginStart="50dp"
        android:layout_marginTop="25dp"
        android:layout_marginEnd="32dp"
        android:hint="Nome"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/materialTextView"
        app:layout_constraintVertical_bias="0.0"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="32dp">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/textNome"
            android:layout_
            android:layout_ />
    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/textInputLayout2"
        style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.Dense"
        android:layout_
        android:layout_
        android:layout_marginStart="50dp"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="15dp"
        android:layout_marginEnd="32dp"
        android:layout_marginRight="32dp"
        android:hint="Cognome"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textInputLayout"
        app:layout_constraintVertical_bias="0.0">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/textCognome"
            android:layout_
            android:layout_ />
    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/textInputLayout3"
        style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.Dense"
        android:layout_
        android:layout_
        android:layout_marginStart="50dp"
        android:layout_marginTop="15dp"
        android:layout_marginEnd="32dp"
        android:hint="Numero Di Telefono"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textInputLayout2"
        app:layout_constraintVertical_bias="0.0"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="32dp">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/textNumero"
            android:layout_
            android:layout_ />
    </com.google.android.material.textfield.TextInputLayout>

    <EditText
        android:id="@+id/testEnabeld"
        android:layout_
        android:layout_
        android:layout_marginTop="20dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Prova"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/switchGaranzia"
        app:layout_constraintVertical_bias="0.0" />

    <Switch
        android:id="@+id/switchGaranzia"
        android:layout_
        android:layout_
        android:layout_marginTop="30dp"
        android:layout_marginBottom="2dp"
        android:text="Garanzia"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textInputLayout3"
        app:layout_constraintVertical_bias="0.0" />




    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_
        android:layout_
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.925">


        <ImageView
            android:layout_
            android:layout_
            android:layout_marginLeft="10dp"
            android:layout_marginTop="1dp"
            android:layout_marginRight="20dp"
            android:layout_marginBottom="16dp"
            app:srcCompat="@drawable/icons8_utente_24" />

        <ImageView
            android:layout_
            android:layout_
            android:layout_marginLeft="10dp"
            android:layout_marginRight="20dp"
            android:layout_marginBottom="16dp"
            app:srcCompat="@drawable/icons8_utente_24" />


    </LinearLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

【参考方案1】:

使用下面的代码在 CardView 中添加一个具有 match_parent 属性 width 和 height 的子视图,并将侦听器添加到子视图而不是 CardVeiw

    cardView.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
            //here i want to put my custom_dialog
            Dialog dialog = new Dialog(mContext);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setContentView(R.layout.custom_dialog);
            dialog.getWindow().setLayout(ViewPager.LayoutParams.MATCH_PARENT, ViewPager.LayoutParams.WRAP_CONTENT);
            dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        
    );

【讨论】:

以上是关于在适配器中创建自定义对话框的主要内容,如果未能解决你的问题,请参考以下文章

使用 Blossom 在 Magnolia CMS 对话框中创建自定义字段

是否有一种编程方式可以在 Windows 中创建自定义网络配置文件并为其分配虚拟网络适配器?

Laravel JSON API: 如何从数据库中创建自定义的虚拟资源(用于聚合值)?

如何在 laravel 中创建自定义关系?

如何在 QML 中创建自定义对象?

在 Java 中创建自定义 JButton