The highligted row in ListView doesn't remain highlighted after scrolling

核能气质少年 提交于 2019-12-11 04:55:35

问题


I have created a listview with simplecursoradapter and made it to Highlight when any of the item is clicked on it with the following code.

<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>

and on Item selected i have done as below.

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);
}

It's working fine ,when i select the item in listview it get selected , But the problem is when i scroll the listview the item selected doesn't remain highlighted.


回答1:


As others have mentioned, due to the list view recycling your views, the selected view is recycled when you scroll it off the page, which is the root cause of your problem.

You need to set the choice mode on your ListView to CHOICE_MODE_SINGLE using either the xml attribute choiceMode or the setChoiceMode() method. It has been awhile, but I think that automatically makes the clicked on position the selected item, but if not, you can call setSelection(position) on the ListView (which is the parent parameter) in onItemClick




回答2:


you should use the inner class that extends BaseAdapter ,for example:

    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;
    }
}


来源:https://stackoverflow.com/questions/18119755/the-highligted-row-in-listview-doesnt-remain-highlighted-after-scrolling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!