java 查看装订的黄油刀(查看者)

6g8kf2rb  于 2022-11-20  发布在  Java
关注(0)|答案(1)|浏览(114)

我正在重建一个旧的漫画应用程序,我想在我的RecyclerView适配器类中使用视图绑定而不是ButterKnife?我被它绊倒了,不能让它通过。所以如果有人有什么想法,请随时发表评论。
这是我的课,要充分理解我所做的事情:

package mowmow.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.comic_app.R;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import mowmow.database.aService;
import mowmow.database.dRetrofit;
import mowmow.model.TrendingResponse;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class adTrendingComic extends RecyclerView.Adapter<adTrendingComic.ViewHolder> {

    @NonNull
    @Override
    public adTrendingComic.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.image_row, parent, false);
        ButterKnife.bind(this, itemView);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull adTrendingComic.ViewHolder holder, int position) {
        getPosterPaths();
        Picasso.get()
                .load(posterPaths.get(position))
                .placeholder(R.drawable.cloud_black_24dp).resize(350, 450)
                .into(holder.trendingComicPosterIV);

    }

    private TrendingResponse trendingComicResponse;
    private Context myContext;
    private List<String> posterPaths = new ArrayList<>();

    public adTrendingComic(TrendingResponse trendingComicResponse, Context context){
        this.trendingComicResponse = trendingComicResponse;
        this.myContext = context;
    }

    private void getPosterPaths(){
        for(int i = 0; i < trendingComicResponse.getTop().size(); i++){
            String posterUrlWithoutSize = trendingComicResponse
                    .getTop()
                    .get(i)
                    .getImageUrl()
                    .replace("/r/100x140", "");
            posterPaths.add(posterUrlWithoutSize);
        }
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.trendingPosterIV)
        ImageView trendingComicPosterIV;
        private Intent intent;
        private aService service = dRetrofit.getData().create(aService.class);

        private ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            intent = new Intent(myContext, DetailsActivity.class);
            trendingComicPosterIV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int malId = trendingComicResponse.getTop().get(getAdapterPosition()).getMalId();
                    Call<ComicResponse> call = service.getComicByID(malId);
                    call.enqueue(new Callback<ComicResponse>() {

                        @Override
                        public void onResponse(@NonNull Call<ComicResponse> call, @NonNull Response<ComicResponse> response) {
                            if(response.body() != null) {
                                TempEnumForComics enumForComic = TempEnumForComics.INSTANCE;
                                enumForComic.setComic(response.body());

                                myContext.startActivity(intent);
                            }
                            else{

                                Toast.makeText(myContext, "No result", Toast.LENGTH_LONG).show();
                            }
                        }

                        @Override
                        public void onFailure(@NonNull Call<ComicResponse> call, @NonNull Throwable t) {
                            Toast.makeText(myContext, "No result", Toast.LENGTH_LONG).show();
                        }
                    });
                }
            });
        }
    }
}

如果有人想帮忙,我会非常感激:)

e5nqia27

e5nqia271#

按照documentation设置视图绑定,然后生成绑定类。

@NonNull
@Override
public adTrendingComic.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    ImageRowBinding binding = ImageRowBinding.inflate(inflater, parent, false);
    return new ViewHolder(binding);
}

和视图保持器:

private ViewHolder(ImageRowBinding binding) {
    super(binding.getRoot());
    binding.getTrendingPosterIv().setOnClickListener(...);
    [...]
}

相关问题