有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

嵌套Recyclerview中的java自定义对话框

我已经实现了一个可扩展的嵌套回收视图。现在我想在单击RecyclerView项时显示一个AlertDialog。我尝试了很多方法,但都没有成功。这是我的适配器类


共 (2) 个答案

  1. # 1 楼答案

    **Child adopter class**
    
    package com.pscripto.mxmoviehub;
    
    import android.app.Dialog;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import androidx.annotation.NonNull;
    import androidx.appcompat.app.AlertDialog;
    import androidx.recyclerview.widget.ItemTouchHelper;
    import androidx.recyclerview.widget.RecyclerView;
    
    import java.util.List;
    
    public class ChildAdopter extends RecyclerView.Adapter<ChildAdopter.ChildAdopterViewHolder> {
     private    List<Child_Model> child_modelList;
       private ChildAdopter childAdopter ;
    
      private   Context context;
       private CustomClickListener customClickListener;
    
        public void setCustomClickListener(CustomClickListener customClickListener) {
            this.customClickListener = customClickListener;
        }
    
        interface CustomClickListener{
        void onItemClick (int position);
    }
    
        public ChildAdopter(List<Child_Model> child_modelList, Context context) {
            this.child_modelList = child_modelList;
            this.context = context;
        }
    
    
        @NonNull
        @Override
        public ChildAdopterViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View rootview = LayoutInflater.from(parent.getContext()).inflate(R.layout.horizental_layout, parent, false);
            return new ChildAdopterViewHolder(rootview);
        }
    
        @Override
        public void onBindViewHolder(@NonNull ChildAdopterViewHolder holder, int position) {
            Child_Model child_model = child_modelList.get(position);
            holder.movieImg.setImageResource(child_model.getImgResId());
            holder.movieTitle.setText(child_model.getTitle());
            Boolean isExpanded = child_modelList.get(position).getExpanded();
            holder.expandidLayout.setVisibility(isExpanded ? View.VISIBLE : View.INVISIBLE);
    
        }
    
    
        @Override
        public int getItemCount() {
            return child_modelList.size();
        }
    
        class ChildAdopterViewHolder extends RecyclerView.ViewHolder {
    
            private ImageView movieImg ,ivEdit,ivDel;
            private TextView movieTitle;
            LinearLayout expandidLayout;
    
    
            public ChildAdopterViewHolder(@NonNull View itemView) {
                super(itemView);
                movieImg = itemView.findViewById(R.id.movie_img);
                movieTitle = itemView.findViewById(R.id.movie_name);
                expandidLayout = itemView.findViewById(R.id.expandable_layout);
                ivEdit = itemView.findViewById(R.id.iv_Edit);
                ivDel = itemView.findViewById(R.id.del_img);
    
                movieTitle.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position = getAdapterPosition();
                        Child_Model child_model = child_modelList.get(position);
                        child_model.setExpanded(!child_model.getExpanded());
                        notifyItemChanged(getAdapterPosition());
                        }
    
                });
                ivEdit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int position= getAdapterPosition();
                        if (customClickListener != null){
                            customClickListener.onItemClick(position);
                        }
                    }
                });
    
            }
        }
    }
    
  2. # 2 楼答案

    **Parent Adopter class code**'
    $ package com.pscripto.mxmoviehub;
    
    import android.annotation.SuppressLint; import android.app.Dialog; import android.content.Context; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast;
    
    import androidx.annotation.NonNull; import androidx.recyclerview.widget.ItemTouchHelper; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView;
    
    import java.util.ArrayList; import java.util.List;
    
    public class ParentAdopter extends RecyclerView.Adapter<ParentAdopter.ParentAdopterViewHolder> {    private List<Movie_Model> movieModelList;   private   List<Child_Model> child_modelList;   private   ChildAdopter childAdopter;   private   Context context;
        public ParentAdopter(@NonNull List<Movie_Model> movieModelList) {
            this.movieModelList = movieModelList;
        }
    
    
    
        @NonNull
        @Override
        public ParentAdopterViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View rootview = LayoutInflater.from(parent.getContext()).inflate(R.layout.vertical_rv,parent,false);
    
            return new ParentAdopterViewHolder(rootview);
        }
    
        @SuppressLint("WrongConstant")
        @Override
        public void onBindViewHolder(@NonNull ParentAdopterViewHolder holder, int position) { Movie_Model movie_model = movieModelList.get(position); holder.tvSection.setText(movie_model.getHeaderTitle()); if (movie_model.getHeaderTitle().contains("Latest Movies")){ childAdopter= new ChildAdopter(getLatestMovie(),context);
    
    
        } else  if (movie_model.getHeaderTitle().contains("Action Movies")){
        childAdopter= new ChildAdopter(getActionMovies(),context);
    
    } else if (movie_model.getHeaderTitle().contains("Romantic Movies")){
        childAdopter = new ChildAdopter(getRomanticMovies(),context); }  holder.childRecyclerview.setHasFixedSize(true); holder.childRecyclerview.setLayoutManager(new LinearLayoutManager(context, LinearLayout.HORIZONTAL,false)); holder.childRecyclerview.setAdapter(childAdopter); //customClicl();
    
        } private void customClicl(){
            childAdopter.setCustomClickListener(new ChildAdopter.CustomClickListener() {
                @Override
                public void onItemClick(int position) {
                    Toast.makeText(context, "hi", Toast.LENGTH_SHORT).show();
                }
            }); }
    
    
        private List<Child_Model> getLatestMovie(){ child_modelList = new ArrayList<>(); child_modelList.add(new Child_Model(R.drawable.salmankhanmovieone,"Bajrangi Bhaijaan"));
    
            child_modelList.add(new Child_Model(R.drawable.pati,"Pati Patni Aur Woh"));
            child_modelList.add(new Child_Model(R.drawable.blackmail,"Blackmail 2019"));
            child_modelList.add(new Child_Model(R.drawable.bharat,"Bharat 2019"));
            child_modelList.add(new Child_Model(R.drawable.gang,"Gangster 3"));
    
    
            return child_modelList;
        }
        private List<Child_Model> getActionMovies(){
            child_modelList = new ArrayList<>();
            child_modelList.add(new Child_Model(R.drawable.salmankhanmovieone,"Bajrangi Bhaijaan"));
            child_modelList.add(new Child_Model(R.drawable.blackmail,"Blackmail 2019"));
            child_modelList.add(new Child_Model(R.drawable.bharat,"Bharat 2019"));
            child_modelList.add(new Child_Model(R.drawable.gang,"Gangster 3"));
    
    
    
            return child_modelList;
        }
        private List<Child_Model> getRomanticMovies(){
            child_modelList = new ArrayList<>();
            child_modelList.add(new Child_Model(R.drawable.star,"Love Star"));
            child_modelList.add(new Child_Model(R.drawable.bestofme,"The Best Of Me"));
            child_modelList.add(new Child_Model(R.drawable.trailer,"After Trailer"));
            child_modelList.add(new Child_Model(R.drawable.buylove,"Cant Buy Love"));
    
    
            return child_modelList;
        }
    
        @Override
        public int getItemCount() {
            return movieModelList.size();
        }
    
        class ParentAdopterViewHolder extends RecyclerView.ViewHolder { TextView tvSection; RecyclerView childRecyclerview;
            public ParentAdopterViewHolder(@NonNull View itemView) {
                super(itemView);
                tvSection = itemView.findViewById(R.id.tvsection);
                childRecyclerview = itemView.findViewById(R.id.child_rv);
    
            }
        }
    
    
    }'$'