如何使用 dagger2 从 RecyclerView.Adapter click for android application build 调用片段函数

How to call fragment function from RecyclerView.Adapter click for android application build using dagger2

谁能建议我从 RecyclerView 调用片段函数而不会导致任何内存泄漏的最佳方法

我的片段代码如下

    <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_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.auth.AuthActivity">


   <com.google.android.exoplayer2.ui.PlayerView
    android:id="@+id/videoID"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black"
    app:resize_mode="fill"
    app:controller_layout_id="@layout/player_control_view"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent" />

    <androidx.recyclerview.widget.RecyclerView
    android:id="@+id/materialList"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/videoID">

    </androidx.recyclerview.widget.RecyclerView>


</androidx.constraintlayout.widget.ConstraintLayout>

我的回收站视图布局是

<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_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="@dimen/_10sdp">

    <androidx.cardview.widget.CardView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:cardBackgroundColor="@color/circle_color_light"
        app:layout_constraintVertical_bias="0.6">

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
        />

    </androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>    

请忽略上述布局中的任何错误,因为我的问题在于 java 代码

我的片段代码如下

public class MaterialFragment extends DaggerFragment implements View.OnClickListener {

    SimpleExoPlayer player;
    PlayerView playerView;

    @Inject
    MaterialAdapter adapter;
    @Inject
    ViewModelProviderFactory providerFactory;

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {


        makeVideo("https://samplevideo.com/test.mp4");

        materialList = view.findViewById(R.id.materialList);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(),3,GridLayoutManager.VERTICAL,false);
        materialList.setLayoutManager(gridLayoutManager);
        materialList.setAdapter(adapter);
        // The above code succesfully load the grid view using recycler adapter(Code for adapter is given below)

    }

    public void makeVideo(){

        // Video loading code goes here  : Loads video on findViewById(R.id.videoID)
    }


}

最后是我的 Recycler Adapter 视图

public class MaterialAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final String TAG = "MaterialAdapter";
    private List<Material> materials = new ArrayList<>();
    private Context context;

    public MaterialAdapter(){
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.material_grid_layout, parent, false);
        PlayerView playerView = ((Activity)parent.getContext()).getWindow().getDecorView().findViewById(R.id.matVideo);
        SimpleExoPlayer player = new SimpleExoPlayer.Builder(((Activity)parent.getContext()).getApplicationContext()).build();
        context   = parent.getContext();
        return new MaterialAdapter.MaterialViewHolder(view,parent.getContext(),playerView,player);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ((MaterialAdapter.MaterialViewHolder)holder).bind(materials.get(position),context);
    }

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

    public void setMaterials(List<Material> materials){
        this.materials = materials;
        notifyDataSetChanged();
    }

    public class MaterialViewHolder extends RecyclerView.ViewHolder {
        TextView name,letter;
        public Context cxt;

        private  SimpleExoPlayer player;
        private  PlayerView playerView;

        public MaterialViewHolder(@NonNull View itemView, Context cxt, final PlayerView playerView, SimpleExoPlayer player) {

            super(itemView);
            this.cxt = cxt;
            this.playerView = playerView;
            this.player = player;
            name = itemView.findViewById(R.id.name);
            clayout = (ConstraintLayout) itemView.findViewById(R.id.card_layout);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                     // I need to change the video of the fragment on click of each block.
                     //The below code will work by implementing full makevideo function on this file 
                     //But I need the bes method without repeting this function and call the methods of he fragment by passing video url or setting view contents of the outer fragment
                    //makeVideo("https://samplevideo.com/test.mp4");  // Not think this is he best solution as I need fullscreen function also.

                    // How to trigger button click of fragment, eg full screen

                }
            });
        }

        

        public void bind(Material material, Context context){

            name.setText(material.getName());
        }

    }
}

适配器是使用下面给出的模块注入的

@Provides
static MaterialAdapter provideMaterialAdapter(){
    return new MaterialAdapter();
}

经过参考,我得到了一些建议,使用我自己的接口作为适配器和片段之间的桥梁,但它没有用,可能是我的匕首导致了一些注入错误,

谁能给我最好的解决方案来解决这个问题

我的屏幕截图如下

在您的片段中使用此 adapter = new MaterialAdapter(); 而不是注入它。只需删除 @Inject 注释并在片段的 onViewCreated 中添加上述适配器创建语句。通过这种方式,您可以更好地控制适配器的创建,并且可以通过构造函数传递接口。