<?xml version="1.0" encoding="utf-8"?> <CheckBox xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/cbx" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="false" />
CheckBox cbx = (CheckBox) findViewById(R.id.cbx);
cbx.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//do something
}
});
boolean isChecked= false;
CheckBox cbx = (CheckBox) findViewById(R.id.cbx);
cbx.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
//do something
}else{
//do something else
}
}
});
cbx.setChecked(isChecked);
holder.cbx.setTag(item);
holder.cbx.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Item item =(Item) buttonView.getTag();
if(isChecked){
item.setCheckState(true);
//do something
}else{
item.setCheckState(false);
//do something else
}
}
});
cbx.setChecked(item.getCheckState());
private class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
SparseBooleanArray mCheckStates=new SparseBooleanArray();
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//...
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
holder.cbx.setTag(position);
holder.cbx.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int pos =(int)buttonView.getTag();
if(isChecked){
mCheckStates.put(pos,true);
//do something
}else{
mCheckStates.delete(pos);
//do something else
}
}
});
cbx.setChecked(mCheckStates.get(position,false));
}
@Override
public int getItemCount() {
//...
}
}
mCheckStates.put(pos,true); adapter.notifyDatasetChanged();
ArrayList<Item> selItems=new ArrayList<>();
for(int i=0;i < mCheckStates.size();i++){
if(mCheckStates.valueAt(i)){
selItems.add(allItems.get(mCheckStates.keyAt(i)));
}
}
holder.cbx.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
buttonView.setChecked(!isChecked);
//...
}
});
boolean lockState=false;
holder.cbx.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(lockState)return;
//不然cbx改变状态.
lockState=true;
buttonView.setChecked(!isChecked);
lockState=false;
//...
}
});
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int pos =(int)buttonView.getTag();
if(isChecked){
mCheckStates.put(pos,true);
//do something
}else{
mCheckStates.delete(pos);
//do something else
}
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(lockState)return;
int pos =(int)buttonView.getTag();
if(mCheckStates.get(pos,false) == isChecked)return;
if(isChecked){
mCheckStates.put(pos,true);
//do something
}else{
mCheckStates.delete(pos);
//do something else
}
}
public class MainActivity extends AppCompatActivity {
String[] filter_type_strs = {"音乐", "书籍", "电影"};
CheckBox cbx;
private boolean lockState=false;
int current_filter_type=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cbx = (CheckBox) findViewById(R.id.cbx);
cbx.setText(filter_type_strs[current_filter_type]);
cbx.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (lockState) return;
try {
if (isChecked) {
//此处传入了cbx做参数
PopupWindow pw = new FilterLinePw(buttonView.getContext(), cbx, filter_type_strs);
pw.showAsDropDown(cbx);
} else {
//此处的buttonView就是cbx
Integer pos = (Integer) buttonView.getTag();
if (pos == null || pos == -1) return;
current_filter_type = pos;
Toast.makeText(MainActivity.this, "搜索"+filter_type_strs[current_filter_type], Toast.LENGTH_SHORT).show();
}
} catch (NullPointerException e) {
//以防万一
lockState = true;
buttonView.setChecked(!isChecked);
lockState = false;
}
}
});
}
}
public class FilterLinePw extends PopupWindow {
RadioGroup radioGroup;
CheckBox outCbx;
//为动态生成radioButton生成id
int[] rbtIds = {0, 1, 2};
public FilterLinePw(Context context, CheckBox outCbx, String[] items) {
super(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
View contentview = LayoutInflater.from(context).inflate(R.layout.filter_line_popupwindow, null);
setContentView(contentview);
setFocusable(true);
setOutsideTouchable(true);
this.outCbx = outCbx;
contentview.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
dismiss();
return true;
}
return false;
}
});
contentview.setFocusable(true); // 这个很重要
contentview.setFocusableInTouchMode(true);
contentview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
init(context, contentview,items);
}
private void init(Context context, View contentview, String[] items) {
/**
* 用传入的筛选条件初始化UI
*/
radioGroup = (RadioGroup) contentview.findViewById(R.id.filter_layout);
radioGroup.clearCheck();
if (items == null) return;
for (int i = 0; i < items.length; i++) {
RadioButton radioButton = (RadioButton) LayoutInflater.from(context).inflate(R.layout.line_popupwindow_rbt, null);
radioButton.setId(rbtIds[i]);
radioButton.setText(items[i]);
radioGroup.addView(radioButton, -1, radioGroup.getLayoutParams());
if (items[i].equals(outCbx.getText())) {
outCbx.setTag(i);
radioButton.setChecked(true);
}
}
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
dismiss();
}
});
}
//重点内容,重写dismiss();
@Override
public void dismiss() {
if (outCbx != null && outCbx.isChecked()) {
int id = radioGroup.getCheckedRadioButtonId();
RadioButton rbt = (RadioButton) radioGroup.findViewById(id);
Integer old_tag = (Integer) outCbx.getTag();
if (old_tag == null) {
super.dismiss();
return;
}
if (old_tag != id) {
outCbx.setTag(id);
outCbx.setText(rbt.getText());
} else {
outCbx.setTag(-1);
}
//下面执行之后,会执行MainActivity中的onCheckedChanged里的否定分支
outCbx.setChecked(false);
}
super.dismiss();
}
}
public class FrameLayoutCheckBox extends FrameLayout {
CompoundButton cbx;
public FrameLayoutCheckBox(Context context) {
super(context);
}
public FrameLayoutCheckBox(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FrameLayoutCheckBox(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
private CheckBox findCheckBox(View view) {
//无递归广度优先遍历寻找CheckBox - -!我只是想重温一下C
ArrayList<View> views = new ArrayList<>();
views.add(view);
while (!views.isEmpty()) {
View c = views.remove(0);
if (c instanceof CheckBox) {
return (CheckBox) c;
} else if (c instanceof ViewGroup) {
ViewGroup fa = (ViewGroup) c;
for (int i = 0; i < fa.getChildCount(); i++) {
views.add(fa.getChildAt(i));
}
}
}
return null;
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
if (getChildCount() > 0) {
View child = findCheckBox(this);
if (child instanceof CompoundButton) cbx = (CompoundButton) child;
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (cbx != null) {
Rect bounds = new Rect(getPaddingLeft(), getPaddingTop(), getPaddingLeft() + getMeasuredWidth() + getPaddingRight(), getPaddingTop() + getMeasuredHeight() + getPaddingBottom());
TouchDelegate delegate = new TouchDelegate(bounds, cbx);
setTouchDelegate(delegate);
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有