我的自定义Spinner
包含一个TextView
和一个CheckBox
。我通过适配器添加项目到微调器。当我再次选中复选框时,取消选中或检查位置是否有变化。
我有两个Array
来自数据来自即product_30或product_80
Array
,旋转器选择的项目应显示在EditText
中。码
数组:
<string-array name="product_2_30">
<item>A</item>
<item>B</item>
<item>C</item>
<item>D</item>
<item>E</item>
<item>N</item>
</string-array>
<string-array name="product_2_80">
<item>A1</item>
<item>B2</item>
<item>C3</item>
<item>D4</item>
<item>F5</item>
<item>NN</item>
</string-array>
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.pankajkumar.adaptertest.MainActivity">
<RadioGroup
android:id="@+id/rgGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/item_2_30"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="30" />
<RadioButton
android:id="@+id/item_2_80"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="80" />
</RadioGroup>
<LinearLayout
android:id="@+id/spinerlayout"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="@+id/rgGroup"
android:background="@drawable/insect_test"
android:orientation="horizontal">
<Spinner
android:id="@+id/myspinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="4"
android:background="@android:color/transparent"
android:gravity="center"
android:spinnerMode="dropdown" />
<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:onClick="showDropDown"
android:paddingRight="5dp"
android:src="@drawable/drop_down_arrow" />
</LinearLayout>
<EditText
android:id="@+id/edt_itemList"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="@+id/spinerlayout"
android:layout_marginTop="@dimen/activity_horizontal_margin" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
private String[] item_2_30, item_2_80;
private Spinner mSpinner;
private MyBaseAdapter adapter_30, adapter_80;
public static ArrayList<Modal> modals_2_30, modals_2_80;
private RadioGroup group;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSpinner = (Spinner) findViewById(R.id.myspinner);
item_2_30 = getResources().getStringArray(R.array.product_2_30);
item_2_80 = getResources().getStringArray(R.array.product_2_80);
group = (RadioGroup) findViewById(R.id.rgGroup);
editText = (EditText) findViewById(R.id.edt_itemList);
modals_2_30 = AddItemToSpinner.addItem(item_2_30);
modals_2_80 = AddItemToSpinner.addItem(item_2_80);
adapter_30 = new MyBaseAdapter(this, modals_2_30, editText);
adapter_80 = new MyBaseAdapter(this, modals_2_80, editText);
mSpinner.setAdapter(adapter_30);
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i) {
case R.id.item_2_30:
mSpinner.setAdapter(adapter_30);
break;
case R.id.item_2_80:
mSpinner.setAdapter(adapter_80);
break;
}
}
});
}
public void showDropDown(View view) {
mSpinner.performClick();
}
}
MyBaseAdapter.java
public class MyBaseAdapter extends BaseAdapter implements SpinnerAdapter {
private static final String TAG = "MyBaseAdapter";
private Context mContext;
private ArrayList<Modal> modals;
private LayoutInflater inflater;
private ViewHolder holder;
private EditText editText;
private int count = 1;
private int po;
public MyBaseAdapter(MainActivity mContext, ArrayList<Modal> modals, EditText editText) {
this.mContext = mContext;
this.modals = modals;
this.editText = editText;
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
Log.e(TAG, "unregisterDataSetObserver: ");
addSelectedItem(MainActivity.modals_2_30);
addSelectedItem(MainActivity.modals_2_80);
}
@Override
public View getDropDownView(int position, View view, ViewGroup parent) {
po = position;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.spinner_row, parent, false);
holder.txt_item_name = (TextView) view.findViewById(R.id.txt_item_name);
holder.chk_check_item = (CheckBox) view.findViewById(R.id.chk_item_check);
view.setTag(holder);
view.setTag(R.id.txt_item_name, holder.txt_item_name);
view.setTag(R.id.chk_item_check, holder.chk_check_item);
} else {
holder = (ViewHolder) view.getTag();
}
holder.txt_item_name.setText(modals.get(position).getListItem());
holder.chk_check_item.setChecked(modals.get(position).isCheck());
holder.chk_check_item.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
modals.get(po).setCheck(b);
}
});
holder.txt_item_name.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//int getposition1 = (Integer) view.getTag();
if (modals.get(po).isCheck()) {
modals.get(po).setCheck(false);
} else {
modals.get(po).setCheck(true);
}
}
});
// notifyDataSetChanged();
return view;
}
@Override
public int getCount() {
return modals.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
po = position;
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.spinner_row, viewGroup, false);
holder.txt_item_name = (TextView) view.findViewById(R.id.txt_item_name);
holder.chk_check_item = (CheckBox) view.findViewById(R.id.chk_item_check);
view.setTag(holder);
view.setTag(R.id.txt_item_name, holder.txt_item_name);
view.setTag(R.id.chk_item_check, holder.chk_check_item);
} else {
holder = (ViewHolder) view.getTag();
}
holder.txt_item_name.setText(modals.get(position).getListItem());
holder.chk_check_item.setChecked(modals.get(position).isCheck());
// holder.chk_check_item.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
// @Override
// public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
// modals.get(po).setCheck(b);
// }
// });
//
// holder.txt_item_name.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// //int getposition1 = (Integer) view.getTag();
// if (modals.get(po).isCheck()) {
// modals.get(po).setCheck(false);
// } else {
// modals.get(po).setCheck(true);
// }
//
// }
// });
// notifyDataSetChanged();
return view;
}
class ViewHolder {
TextView txt_item_name;
CheckBox chk_check_item;
}
public void addSelectedItem(ArrayList<Modal> modals) {
for (Modal m : modals) {
if (m.isCheck()) {
editText.setText(m.getListItem() + "\n");
}
}
}
}
答案 0 :(得分:0)
在getDropDownView方法中,您获取微调器项的位置而不是列表位置。因此,当您更新列表中的值时,出错了
答案 1 :(得分:0)
由于CheckBox(最终)扩展了View,您可以使用标准OnClickListener
来检测用户实际点击CheckBox的时间而不是CheckedChangeListener
holder.chk_check_item
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(((CheckBox)v).isChecked()){
//Checkbox is checked Perform logic here
} else {
}
}
});
选中的更改侦听器在滚动时没有正确保持检查,这就是为什么在滚动时更改复选框状态的原因。试一试,让我知道。它对我有用..
答案 2 :(得分:0)
模态类
public class Modal {
private boolean isCheck;
private String listItem;
public boolean isCheck() {
return isCheck;
}
public void setCheck(boolean check) {
isCheck = check;
}
public String getListItem() {
return listItem;
}
public void setListItem(String listItem) {
this.listItem = listItem;
}
}
// ==========适配器类======================
public class MyNewSpinnerAdapter extends BaseAdapter implements SpinnerAdapter {
private static final String TAG = "MyNewSpinnerAdapter";
private Context mContext;
private ArrayList<Modal> modals;
private LayoutInflater inflater;
private ViewHolder holder;
private EditText editText;
private int count = 1;
private int po;
private Spinner mSpinner;
public MyNewSpinnerAdapter(Context mContext, ArrayList<Modal> modals, EditText editText, Spinner mSpinner) {
this.mContext = mContext;
this.modals = modals;
this.editText = editText;
this.mSpinner = mSpinner;
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getDropDownView(final int position, View view, ViewGroup viewGroup) {
Log.e(TAG, "getDropDownView: " + position);
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.spinner_row, viewGroup, false);
holder.txt_item_name = (TextView) view.findViewById(R.id.txt_item_name);
holder.chk_check_item = (CheckBox) view.findViewById(R.id.chk_item_check);
view.setTag(holder);
view.setTag(R.id.txt_item_name, holder.txt_item_name);
view.setTag(R.id.chk_item_check, holder.chk_check_item);
} else {
holder = (ViewHolder) view.getTag();
}
holder.txt_item_name.setText(modals.get(position).getListItem());
holder.chk_check_item.setChecked(modals.get(position).isCheck());
holder.chk_check_item.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(mContext, "Drop Down " + position, Toast.LENGTH_SHORT).show();
if (modals.get(position).isCheck()) {
modals.get(position).setCheck(false);
} else {
AddItemToSpinner.refreshData(NewProductSelectionActivity.type);
modals.get(position).setCheck(true);
if (mSpinner != null) {
try {
Method method = Spinner.class.getDeclaredMethod("onDetachedFromWindow");
method.setAccessible(true);
method.invoke(mSpinner);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
});
return view;
}
@Override
public void registerDataSetObserver(DataSetObserver dataSetObserver) {
}
@Override
public void unregisterDataSetObserver(DataSetObserver dataSetObserver) {
editText.getText().clear();
updateData();
}
public void updateData() {
for (Modal m : NewProductSelectionActivity.modals_2_30) {
if (m.isCheck()) {
editText.append(m.getListItem() + "\n");
}
}
for (Modal m : NewProductSelectionActivity.modals_2_80) {
if (m.isCheck()) {
editText.append(m.getListItem() + "\n");
}
}
}
@Override
public int getCount() {
return modals.size();
}
@Override
public Object getItem(int i) {
return modals.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
if (view == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.spinner_row, viewGroup, false);
holder.txt_item_name = (TextView) view.findViewById(R.id.txt_item_name);
holder.chk_check_item = (CheckBox) view.findViewById(R.id.chk_item_check);
view.setTag(holder);
view.setTag(R.id.txt_item_name, holder.txt_item_name);
view.setTag(R.id.chk_item_check, holder.chk_check_item);
} else {
holder = (ViewHolder) view.getTag();
}
holder.txt_item_name.setText(modals.get(position).getListItem());
holder.chk_check_item.setChecked(modals.get(position).isCheck());
return view;
}
@Override
public int getItemViewType(int i) {
return 0;
}
@Override
public int getViewTypeCount() {
return 1;
}
@Override
public boolean isEmpty() {
return false;
}
class ViewHolder {
TextView txt_item_name;
CheckBox chk_check_item;
}
}
// ====================添加项目到列表===================== ===
public class AddItemToSpinner {
public static ArrayList<Modal> addItem(String[] item) {
ArrayList<Modal> modals = new ArrayList<>();
for (String s : item) {
Modal m = new Modal();
m.setListItem(s);
m.setCheck(false);
modals.add(m);
}
return modals;
}
public static void refreshData(String typeList) {
if (typeList.equals("2_30")) {
for (int i = 0; i < NewProductSelectionActivity.modals_2_30.size(); i++) {
if (NewProductSelectionActivity.modals_2_30.get(i).isCheck()) {
NewProductSelectionActivity.modals_2_30.get(i).setCheck(false);
}
}
} else if (typeList.equals("2_80")) {
for (int i = 0; i < NewProductSelectionActivity.modals_2_80.size(); i++) {
if (NewProductSelectionActivity.modals_2_80.get(i).isCheck()) {
NewProductSelectionActivity.modals_2_80.get(i).setCheck(false);
}
}
} else if (typeList.equals("All")) {
for (int i = 0; i < NewProductSelectionActivity.modals_2_30.size(); i++) {
if (NewProductSelectionActivity.modals_2_30.get(i).isCheck()) {
NewProductSelectionActivity.modals_2_30.get(i).setCheck(false);
}
}
for (int i = 0; i < NewProductSelectionActivity.modals_2_80.size(); i++) {
if (NewProductSelectionActivity.modals_2_80.get(i).isCheck()) {
NewProductSelectionActivity.modals_2_80.get(i).setCheck(false);
}
}
}
}
}
// ========================= MainActivity.java ================= ==================
public class NewProductSelectionActivity extends AppCompatActivity {
private String[] item_2_30, item_2_80;
public static String type = "2_30";
private Spinner mSpinner;
private MyNewSpinnerAdapter adapter_30, adapter_80;
public static ArrayList<Modal> modals_2_30, modals_2_80;
private RadioGroup group;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSpinner = (Spinner) findViewById(R.id.myspinner);
item_2_30 = getResources().getStringArray(R.array.product_2_30);
item_2_80 = getResources().getStringArray(R.array.product_2_80);
group = (RadioGroup) findViewById(R.id.rgGroup);
editText = (EditText) findViewById(R.id.edt_itemList);
modals_2_30 = AddItemToSpinner.addItem(item_2_30);
modals_2_80 = AddItemToSpinner.addItem(item_2_80);
adapter_30 = new MyNewSpinnerAdapter(this, modals_2_30, editText,mSpinner);
adapter_80 = new MyNewSpinnerAdapter(this, modals_2_80, editText,mSpinner);
mSpinner.setAdapter(adapter_30);
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i) {
case R.id.item_2_30:
type = "2_30";
mSpinner.setAdapter(adapter_30);
break;
case R.id.item_2_80:
type = "2_80";
mSpinner.setAdapter(adapter_80);
break;
}
}
});
findViewById(R.id.btnrefresh).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
type = "All";
AddItemToSpinner.refreshData(type);
adapter_30.unregisterDataSetObserver(null);
}
});
}
public void showDropDown(View view) {
mSpinner.performClick();
}
}
现在它正在运作..:D