在ListView中highligted行不继续滚动后高亮 [英] The highligted row in ListView doesn't remain highlighted after scrolling

查看:118
本文介绍了在ListView中highligted行不继续滚动后高亮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个simplecursoradapter ListView和取得被点击时任该项目上具有以下code以高亮显示。

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.

推荐答案

您应该使用扩展BaseAdapter内部类,例如:

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

这篇关于在ListView中highligted行不继续滚动后高亮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆