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);
|
}
|
}
|