Untitled

mail@pastecode.io avatar
unknown
java
a year ago
4.0 kB
2
Indexable
Never
public class FileAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<File> file;
    private List<Boolean> checkedStates;
    private boolean isAllChecked = false;
    private boolean isCheckBoxVisible = false;
    private FileOnClickInterface fileOnClickInterface;
    private CompoundButton.OnCheckedChangeListener checkedChangeListener;
    private File selectedFile = null;
    private List<File> selectedFiles = new ArrayList<>();
    private static final int VIEW_TYPE_FILE = 0;
    private static final int VIEW_TYPE_AD = 1;


    public FileAdapter(Context context, List<File> file, FileOnClickInterface fileOnClickInterface) {
        this.context = context;
        this.file = file;
        this.fileOnClickInterface = fileOnClickInterface;
        checkedStates = new ArrayList<>(Collections.nCopies(file.size(), false));
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == VIEW_TYPE_FILE) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.listview_file, parent, false);
            return new FileViewHolder(itemView);
        } else {
            View adView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_ad, parent, false);
            return new AdViewHolder(adView);
        }

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        int viewType = getItemViewType(position);

        if (viewType == VIEW_TYPE_FILE) {
            FileViewHolder fileViewHolder = (FileViewHolder) holder;

            fileViewHolder.txtFName.setText(file.get(position).getName());
        
        }
        //
        else {
            AdViewHolder adViewHolder = (AdViewHolder) holder;
            AdRequest adRequest = new AdRequest.Builder().build();
            adViewHolder.adView.loadAd(adRequest);
        }
    }



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

    @Override
    public int getItemViewType(int position) {
        if (file.size() > 5) {
            return (position + 1) % 6 == 0 ? VIEW_TYPE_AD : VIEW_TYPE_FILE;
        } else {
            return VIEW_TYPE_FILE;
        }
    }


    public File getFile(int position) {
        return file.get(position);
    }


    public class FileViewHolder extends RecyclerView.ViewHolder {
        public TextView txtFName, txtFSize;
        public CardView card;
        public ImageView img;
        private CheckBox checkBox;


        public FileViewHolder(@NonNull View itemView) {
            super(itemView);
            txtFName = itemView.findViewById(R.id.file_name);
            txtFSize = itemView.findViewById(R.id.file_size);
            img = itemView.findViewById(R.id.file_img);
            checkBox = itemView.findViewById(R.id.file_checkbox);
            card = itemView.findViewById(R.id.LinearCardView);

            checkBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = getAdapterPosition();
                    if (position != RecyclerView.NO_POSITION) {
                        boolean isChecked = !checkedStates.get(position);
                        checkedStates.set(position, isChecked);
                        fileOnClickInterface.onCheckboxSelectedChanged(isAllSelected());
                    }
                }
            });


        }

    }

    private static class AdViewHolder extends RecyclerView.ViewHolder {
        private AdView adView;

        AdViewHolder(@NonNull View itemView) {
            super(itemView);
            adView = itemView.findViewById(R.id.adView);
        }
    }


}