ListView突出显示所选项目

时间:2016-07-12 07:03:19

标签: android listview custom-adapter

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

这是我的CustomAdapter

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

2 个答案:

答案 0 :(得分:1)

您可以通过以下代码执行此操作,而不是在适配器类中应用, 检查以下代码,

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

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

答案 1 :(得分:0)

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