ListView 突出显示所选项目

ListView Highlight selected items

我无法突出显示所有选定的项目。一次只突出显示一项。如果单击下一个项目,前一个突出显示的项目将恢复正常。

这是我的自定义适配器

private class CustomAdapter extends ArrayAdapter<HashMap<String, Object>> {

        public CustomAdapter(Context context, int textViewResourceId, ArrayList<HashMap<String, Object>> Strings) {

            //let android do the initializing :)
            super(context, textViewResourceId, Strings);
        }

        public void setSelectedIndex(int ind)
        {
            selectedIndex = ind;
            notifyDataSetChanged();
        }

        @Override
        public int getCount()
        {
            return dataList.size();
        }

        @Override
        public HashMap<String, Object> getItem(int position)
        {
            return dataList.get(position);
        }

        @Override
        public long getItemId(int position)
        {
            return position;
        }

        //class for caching the views in a row
        private class ViewHolder {

            TextView not, isRead;
            LinearLayout noti_linear;
        }

        //Initialise
        ViewHolder viewHolder;

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            if (convertView == null) {

                //inflate the custom layout
                convertView = inflater.from(parent.getContext()).inflate(R.layout.notification_layout, parent, false);
                viewHolder = new ViewHolder();

                //cache the views
                viewHolder.noti_linear = (LinearLayout) convertView.findViewById(R.id.not_layout);
                viewHolder.not = (TextView) convertView.findViewById(R.id.textview_noti);
                viewHolder.isRead = (TextView) convertView.findViewById(R.id.textview_isRead);

                //link the cached views to the convertview
                convertView.setTag(viewHolder);
            } else
                viewHolder = (ViewHolder) convertView.getTag();

            //set the data to be displayed
            viewHolder.not.setText(dataList.get(position).get("CheckList").toString());

            if(selectedIndex!= -1 && position == selectedIndex)
            {
                viewHolder.noti_linear.setBackgroundColor(Color.TRANSPARENT);
            }
            else
            {
                viewHolder.noti_linear.setBackgroundColor(Color.LTGRAY);
            }


//            if (getItemId(position) == StringHolder.mSelectedItem) {
//                viewHolder.noti_linear.setBackgroundColor(Color.LTGRAY);
//
//            } else {
//                viewHolder.noti_linear.setBackgroundColor(Color.TRANSPARENT);
//            }

            return convertView;
        }
    }

这是我的 onItemClick

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                cardAdapter.setSelectedIndex(position);
            }
        });
OnItemClickListener listViewOnItemClick = new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> adapter, View arg1, int position, long id) {
            mSelectedItem = position;
            mAdapter.notifyDataSetChanged();
    }
};


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final View view = View.inflate(context, R.layout.item_list, null);

    if (position == mSelectedItem) {
        // set your color
    }

    return view;
}  

你可以通过下面的代码来完成,而不是在适配器中应用 class, 检查下面的代码,

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            parent.getChildAt(position).setBackgroundColor(Color.TRANSPARENT);
            //cardAdapter.setSelectedIndex(position);
        }
    });