wjc
2025-04-09 87cd5df70918e6ba1af849c5f026d3719bfdb1ac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.sahooz.library.countrypicker;
 
import android.annotation.SuppressLint;
import android.content.Context;
 
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
 
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.ViewGroup;
 
import java.util.ArrayList;
 
public class Adapter extends RecyclerView.Adapter<VH> {
 
    private ArrayList<Country> selectedCountries = new ArrayList<>();
    private final LayoutInflater inflater;
    private PickCountryCallback callback = null;
    private final Context context;
 
    public Adapter(Context ctx) {
        inflater = LayoutInflater.from(ctx);
        context = ctx;
    }
 
    public void setSelectedCountries(ArrayList<Country> selectedCountries) {
        this.selectedCountries = selectedCountries;
        notifyDataSetChanged();
 
    }
 
    public void setCallback(PickCountryCallback callback) {
        this.callback = callback;
    }
 
    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new VH(inflater.inflate(R.layout.item_country, parent, false));
    }
 
    private int itemHeight = -1;
 
    public void setItemHeight(float dp) {
        itemHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, dp, context.getResources().getDisplayMetrics());
    }
 
    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(VH holder, int position) {
        final Country country = selectedCountries.get(position);
        holder.ivFlag.setImageResource(country.flag);
        holder.tvName.setText(country.translate);
        holder.tvCode.setText("+" + country.code);
        if (itemHeight != -1) {
            ViewGroup.LayoutParams params = holder.itemView.getLayoutParams();
            params.height = itemHeight;
            holder.itemView.setLayoutParams(params);
        }
        holder.itemView.setOnClickListener(v -> {
            if (callback != null) callback.onPick(country);
        });
    }
 
    @Override
    public int getItemCount() {
        return selectedCountries.size();
    }
 
}