我使用simplecursoradapter创建了一个listview,并在使用以下代码点击任何项目时将其设置为突出显示。
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:drawable="@color/blue" />
<item
android:drawable="@color/white" />
</selector>
和选择的项目我已完成如下。
list = (ListView) view.findViewById(android.R.id.list);
adapter = new SimpleCursorAdapter(getActivity(), R.layout.title_intro_list, articleCur, FROM, TO,1);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id)
{
view.setSelected(true);
}
它工作正常,当我选择listview中的项目时,它被选中, 但问题是当我滚动列表视图时,所选项目不会保持突出显示。
答案 0 :(得分:0)
正如其他人所提到的,由于列表视图会回收您的视图,因此当您将其从页面滚动时,所选视图将被回收,这是导致问题的根本原因。
您需要使用xml属性choiceMode
或setChoiceMode()
方法将ListView上的选择模式设置为CHOICE_MODE_SINGLE。它已经有一段时间了,但我认为自动使所选项目的点击位置,但如果没有,您可以在{{1}中的ListView(setSelection(position)
参数)上调用parent
}
答案 1 :(得分:0)
您应该使用扩展BaseAdapter的内部类,例如:
private class TicketLVAdapter extends BaseAdapter{
private LayoutInflater inflater;
private List<Ticket> list;
private HashMap<String, Integer> alphaIndexer;
private String[] sections;
List<Ticket> ts;
public TicketLVAdapter(Context context) {
this.inflater = LayoutInflater.from(context);
}
public void setList(List<Ticket> ts) {
this.ts = ts;
}
@Override
public int getCount() {
return ts.size();
}
@Override
public Object getItem(int position) {
return ts.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
//the very important method:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;//use view holder;
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.pulldown_item, null);
holder.tv_id_num = (TextView) convertView.findViewById(R.id.num_id);
holder.tvConsumeTime = (TextView) convertView
.findViewById(R.id.time);
holder.tvOperator = (TextView) convertView
.findViewById(R.id.operator);
holder.tvValue = (TextView) convertView.findViewById(R.id.value);
convertView.setTag(holder);
Ticket ticket = ts.get(position);
if (rtApp.isOnUnload) {// onCreate()方法中清空掉failureTickets
ArrayList<Integer> positionList = new ArrayList<Integer>();
for (int i = 0; i < ts.size(); i++) {
if (rtApp.failureTickets.contains(ts.get(i).getTicketID())) {
positionList.add(i);
}
}
}
String ticketCT = ticket.getTicketconsumeTime();
holder.tvConsumeTime.setText(ticketCT);
holder.numberString = "位置:" + position;
String ticketOp = ticket.getTicketOperator();
holder.tvOperator.setText(ticketOp);
holder.tv_id_num.setText(position + 1 + "");
String ticketValue = ticket.getTicketPrice();
holder.tvValue.setText(ticketValue);
if (rtApp.failureTickets.contains(ticket.getTicketID())
&& rtApp.isOnUnload) {
holder.tvConsumeTime.setTextColor(Color.parseColor("#ff0000"));
holder.tvOperator.setTextColor(Color.parseColor("#ff0000"));
holder.tv_id_num.setTextColor(Color.parseColor("#ff0000"));
holder.tvValue.setTextColor(Color.parseColor("#ff0000"));
}
return convertView;
}
private class ViewHolder {
TextView tvID;
TextView tvConsumeTime;
TextView tvOperator;
TextView tvValue;
TextView tv_id_num;
String numberString;
}
@Override
public int getPositionForSection(int section) {
String later = sections[section];
return alphaIndexer.get(later);
}
@Override
public int getSectionForPosition(int position) {
return 0;
}
@Override
public Object[] getSections() {
// TODO Auto-generated method stub
return null;
}
}