wjc
2025-04-15 cdf49871675e42a5576f725a93eec7ca15294c6f
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package com.sahooz.library.countrypicker;
 
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
 
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
 
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
 
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
 
import java.util.ArrayList;
 
/**
 * Created by android on 17/10/17.
 */
 
public class CountryPickerFragment extends Dialog {
 
    private View mView;
    private final ArrayList<Country> allCountries = new ArrayList<>();
    private final ArrayList<Country> selectedCountries = new ArrayList<>();
    private PickCountryCallback mCallback;
    private Context mContext;
 
    public CountryPickerFragment(Context context, PickCountryCallback callback) {
        super(context, R.style.video_dialog);
        mContext = context;
        mCallback = callback;
    }
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }
 
    public void initView() {
 
        LayoutInflater inflater = LayoutInflater.from(mContext);
        mView = inflater.inflate(R.layout.dialog_country_picker, null);
        setContentView(mView);
 
        EditText etSearch = mView.findViewById(R.id.et_search);
        final RecyclerView rvCountry = mView.findViewById(R.id.rv_country);
        allCountries.clear();
        allCountries.addAll(Country.getAll());
        selectedCountries.clear();
        selectedCountries.addAll(allCountries);
        final Adapter adapter = new Adapter(getContext());
        adapter.setCallback(country -> {
            dismiss();
            if (mCallback != null) mCallback.onPick(country);
        });
        adapter.setSelectedCountries(selectedCountries);
        rvCountry.setAdapter(adapter);
        rvCountry.setLayoutManager(new LinearLayoutManager(getContext()));
        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
 
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
 
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void afterTextChanged(Editable s) {
                String string = s.toString();
                selectedCountries.clear();
                for (Country country : allCountries) {
                    if (country.name.toLowerCase().contains(string.toLowerCase())
                            || country.translate.toLowerCase().contains(string.toLowerCase())
                            || country.getPinyin().toLowerCase().contains(string.toLowerCase())
                    )
                        selectedCountries.add(country);
                }
                adapter.notifyDataSetChanged();
            }
        });
    }
 
    public void setDialogSize() {
        WindowManager windowManager = this.getWindow().getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = this.getWindow().getAttributes();
        lp.width = (int) (display.getWidth() * 0.8); //设置宽度
        lp.height = (int) (display.getHeight() * 0.8); //设置高度
        this.getWindow().setAttributes(lp);
    }
}