我正在为我的列表视图使用customadapter。在我的活动中,我有一些侦听器来标记选择的项目(第一次使用日志点击,然后短暂点击) 在列表变得足够大以至于无法滚动之前,它一直有效。在这种情况下,如果我标记一个项目并向下滚动,那么也会标记其他项目,如果我快速滚动,标记的项目也会改变。我听说,ListView正在以某种方式回收这些位置,但我认为该位置应保持不变。
这是我的CustomAdapter :(如果您也看到性能改进的一些可能性,请提及它)
public class CustomAdapterMain extends BaseAdapter {
private ArrayList<Kontakt> listItems;
private LayoutInflater layoutInflater;
private Kontakt kontakt;
private int position;
private ViewHolder viewHolder;
private Integer Kalenderjahr=Calendar.getInstance().get(Calendar.YEAR);
private Integer Kalendermonat=Calendar.getInstance().get(Calendar.MONTH);
private Integer Kalendertag=Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
TinyDB tinyDB;
public CustomAdapterMain(Context context, ArrayList<Kontakt> arrayList){
listItems=arrayList;
//layoutInflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater=LayoutInflater.from(context);
tinyDB= new TinyDB(context);
}
@Override
public int getCount() {
return listItems.size();
}
@Override
public Kontakt getItem(int position) {
return listItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup parent){
if(view==null){
viewHolder=new ViewHolder();
view=layoutInflater.inflate(R.layout.chat_list_view_item, parent, false);
viewHolder.listView=view.findViewById(R.id.Chats);
viewHolder.Kontaktname=view.findViewById(R.id.kontakt_item_name_chat);
viewHolder.Chatsnippet=view.findViewById(R.id.kontakt_item_chatsnippet);
viewHolder.Uhrzeit=view.findViewById(R.id.kontakt_item_uhrzeit);
view.setTag(viewHolder);
}
else{
viewHolder=(ViewHolder)view.getTag();
}
Kontakt kontaktItem=listItems.get(position);
if(kontakt!=null){
viewHolder.kontakt.setKontakt(kontaktItem);
}
else{
//viewHolder.kontakt=kontaktItem.getKontakt();
}
String last_massage=tinyDB.getString(kontaktItem.getNummer()+"text");
String last_massage_time=tinyDB.getString(kontaktItem.getNummer()+"time");
String[] Uhrzeit=last_massage_time.split(" ");
String[] Datum=Uhrzeit[0].split("-");
String[] Stunden=Uhrzeit[1].split(":");
Integer Jahr=Integer.parseInt(Datum[0]);
Integer Monat=Integer.parseInt(Datum[1]);
Integer Tag=Integer.parseInt(Datum[2]);
if(Jahr<Kalenderjahr||Monat<Kalendermonat||Tag+1<Kalendertag){
viewHolder.Uhrzeit.setText(Tag+"."+Monat+"."+Jahr%1000);
}
else if(Tag<Kalendertag){
viewHolder.Uhrzeit.setText(R.string.gestern);
}
else{
viewHolder.Uhrzeit.setText(Stunden[0]+":"+Stunden[1]);
}
viewHolder.Kontaktname.setText(kontaktItem.getName());
viewHolder.Chatsnippet.setText(last_massage);
//set Image from server
return view;
}
}
我的onClickListeners :(也尝试过parent.getChildAt(position).setBackgroundColor(getResources()。getColor(R.color.orange));)
Chats=findViewById(R.id.Chats);
final CustomAdapterMain customadapter=new CustomAdapterMain(YouniChat_MainActivity.this,ArrayListChats);
Chats.setAdapter(customadapter);
Chats.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView parent, View view, int position, long id) {
hasClicked.put(position,false);
view.setBackgroundColor(getResources().getColor(R.color.orange));
marked++;
Chats.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
if(hasClicked.get(position,true)) {
view.setBackgroundColor(getResources().getColor(R.color.orange));
marked++;
hasClicked.put(position, false);
}
else{
view.setBackgroundColor(0);
marked--;
hasClicked.put(position, true);
}
if(marked>0){
findViewById(R.id.delete).setVisibility(View.VISIBLE);
}
else{
findViewById(R.id.delete).setVisibility(View.INVISIBLE);
onClickListener();
}
}
});
if(marked>0) {
findViewById(R.id.delete).setVisibility(View.VISIBLE);
}
else{
findViewById(R.id.delete).setVisibility(View.INVISIBLE);
onClickListener();
}
return true; //damit nicht der OnItemClickListener reagiert
}
});