У меня есть custom adapter
extends BaseadApter
и из-за проблемы «нет работы listselector в xml
при использовании onClickListener
в адаптере», я решил реализовать поведение listselector в моем onClickListener (внутри адаптера).Проблема: когда я имитирую поведение listselector
, я теряю одиночный выбор.Я хочу сохранить один выбор в моем ListView.Некоторые части моего кода:
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
arg0.setBackground(arg0.getContext().getResources().getDrawable(R.drawable.my_selector));
}
}
})
Я нашел очень посты о селекторе, а также пост Фелипе Калдаса, но, к сожалению, он не дал четкого ответа.Я отмечаю этот пост.
и в моем xml
<ListView
android:id="@+id/requestListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="4dp"
android:choiceMode="singleChoice"
>
</ListView>
Мой полный адаптер:
public class HardwareAdapter extends BaseAdapter {
Context context;
String stringList[];
String stringReqNoList[];
int flags[];
LayoutInflater inflter;
ArrayList<HardwareObject> oData = new ArrayList<HardwareObject>();
ArrayList<HardwareObject> oDataSelectedItems = new ArrayList<HardwareObject>();
List<Drawable> oIconList = new ArrayList<Drawable>();
public HardwareAdapter(Context iApplicationContext, Collection<HardwareObject> iData , List<Drawable> iIconList ,String iMode) {
Log.e("CA2","CustomAdapter2 iData count = "+ String.valueOf(iData.size()));
//layoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//listStorage = customizedListView;
if(iIconList == null)
{
iIconList = new ArrayList<Drawable>();
}
this.context = context;
//this.stringList = iStringList;
//this.flags = iFlags;
inflter = (LayoutInflater.from(iApplicationContext));
oData = new ArrayList<HardwareObject>();
for (HardwareObject oItem : iData) {
oData.add(oItem);
}
for (Drawable oItemDrawable : iIconList) {
oIconList.add(oItemDrawable);
}
}
@Override
public int getCount() {
//return stringList.length;
return oData.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
MyViewHolder oMYviewHolder;
if(view == null) {
view = inflter.inflate(R.layout.activity_my_list_row3, null); //original line
//view = inflter.inflate(R.layout.activity_my_list_row3 , viewGroup, false);
oMYviewHolder = new MyViewHolder(view);
view.setTag(oMYviewHolder);
}
else
{
oMYviewHolder = (MyViewHolder) view.getTag();
}
view.setBackground(view.getContext().getResources().getDrawable(R.drawable.test_test));
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//arg0.setSelected(true);
//arg0.setPressed(true);
if(arg0.isPressed())
{
arg0.setSelected(true);
//arg0.setBackground(arg0.getContext().getResources().getDrawable(R.drawable.test_test));
}
else
{
arg0.setSelected(false);
//arg0.setBackground(arg0.getContext().getResources().getDrawable(R.drawable.test_test));
}
if(oIconList.size() >= 1) {
CheckBox ochk = arg0.findViewById(R.id.chkEnable);
ochk.setChecked(!ochk.isChecked());
if (ochk.isChecked()) {
//arg0.setSelected(true);
//arg0.setPressed(true);
//arg0.setBackground(arg0.getContext().getResources().getDrawable(R.drawable.test_test));
}
else
{
//arg0.setSelected(false);
//arg0.setPressed(false);
//arg0.setBackground(arg0.getContext().getResources().getDrawable(R.drawable.test_test));
}
}
}
});
////ProgressBar oPBar = (ProgressBar) view.findViewById(R.id.progressBarA);
////TextView country = (TextView) view.findViewById(R.id.textView);
////ImageView icon = (ImageView) view.findViewById(R.id.icon);
////LinearLayout oImageLayout = view.findViewById(R.id.imageLayout);
////LinearLayout oTextsLayout = view.findViewById(R.id.textsLayout);
////TextView textDetail = (TextView) view.findViewById(R.id.textViewDetail);
oMYviewHolder.country.setText(PersianDigitConverter.PersianNumber(oData.get(i).getTitle().trim()));
oMYviewHolder.textDetail.setText(PersianDigitConverter.PersianNumber(oData.get(i).getDescription().trim()));
if(oIconList.size()-1 >= i) {
oMYviewHolder.oImageLayout.setVisibility(View.VISIBLE);
oMYviewHolder.icon.setImageDrawable(oIconList.get(i));
oMYviewHolder.oTextsLayout.setOrientation(LinearLayout.HORIZONTAL);
oMYviewHolder.country.setWidth(0);
oMYviewHolder.country.setHeight(0);
//oMYviewHolder.oPBar.setVisibility(View.VISIBLE);
//oMYviewHolder.oPBar.setMax(oData.size());
//oMYviewHolder.oPBar.setProgress(50);
oMYviewHolder.textDetail.setText(PersianDigitConverter.PersianNumber(String.valueOf(i+1)+"-")+oMYviewHolder.textDetail.getText());
}
UpdateSelectedItems(view);
return view;
}
private class MyViewHolder {
TextView itemName;
TextView itemDescription;
ProgressBar oPBar ;
TextView country ;
ImageView icon ;
LinearLayout oImageLayout ;
LinearLayout oTextsLayout ;
TextView textDetail ;
public MyViewHolder(View view) {
//itemName = (TextView)view.findViewById(R.id.textView);
//itemDescription = (TextView) view.findViewById(R.id.text_view_item_description);
oPBar = (ProgressBar) view.findViewById(R.id.progressBarA);
country = (TextView) view.findViewById(R.id.textView);
icon = (ImageView) view.findViewById(R.id.icon);
oImageLayout = view.findViewById(R.id.imageLayout);
oTextsLayout = view.findViewById(R.id.textsLayout);
textDetail = (TextView) view.findViewById(R.id.textViewDetail);
if(view.isSelected())
{
((CheckBox)view.findViewById(R.id.chkEnable)).setChecked(true);
}
}
}
private void UpdateSelectedItems(View oView)
{
for (HardwareObject oItem : oData) {
}
}
}