0

I am using the StickyListHeadersListView android library from github for my application. It was working fine. After I implement MultiChoiceMode listener for copying and deleting elements, there is a problem in highlighting of elements.

Whenever I select an element and scroll up and down, some Section Headers are highlighted automatically like shown in the image below

when scrolling up and then down, some section headers are getting selected

How to avoid this behaviour. Is there any steps I'm missing? Need some hand in resolving the issue.

My Adapter which extends the StickyListHeadersAdapter is given below

    public class MessageStickyAdapter extends BaseAdapter implements StickyListHeadersAdapter, SectionIndexer {

        private final Context mContext;
        private List<Msg> messages;
        private int[] mSectionIndices;
        private String[] mSectionDates;
        private LayoutInflater mInflater;

        public MessageStickyAdapter(Context context,List<Msg> listMessages) {
            mContext = context;
            mInflater = LayoutInflater.from(context);
            messages = listMessages;
            mSectionIndices = getSectionIndices();
            mSectionDates = getSectionDates();
        }

        private int[] getSectionIndices() {
            ArrayList<Integer> sectionIndices = new ArrayList<>();
            String lastDate = messages.get(0)._msg_date;
            sectionIndices.add(0);
            for (int i = 1; i < messages.size(); i++) {
               if (!messages.get(i)._msg_date.equalsIgnoreCase(lastDate)) {
               Log.d("LastDate,Newdate",lastDate + ',' +messages.get(i)._msg_date);
               lastDate = messages.get(i)._msg_date;
               sectionIndices.add(i);
               }
            }
            int[] sections = new int[sectionIndices.size()];
            for (int i = 0; i < sectionIndices.size(); i++) {
                sections[i] = sectionIndices.get(i);
            }
            Log.d("Sections",String.valueOf(sections.length));
            return sections;
        }

        private String[] getSectionDates() {
             String[] dates = new String[mSectionIndices.length];
             for (int i = 0; i < mSectionIndices.length; i++) {
                 dates[i] = messages.get(i)._msg_date;
                 Log.d("Dates",dates[i]);
             }
                 return dates;
        }

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

        @Override
        public Object getItem(int position) {
            return messages.get(position);
        }

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


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.right, parent, false);
                holder.text = (TextView) convertView.findViewById(R.id.msgr);
                holder.time = (TextView) convertView.findViewById(R.id.tim);
                convertView.setTag(holder);
             } else {
                holder = (ViewHolder) convertView.getTag();
             }
            try {
                holder.text.setText(URLDecoder.decode( messages.get(position)._msg_content, "UTF-8"));
                holder.text.setTag(messages.get(position).getMsgID());
                holder.time.setText(messages.get(position)._msg_time);
             } catch (UnsupportedEncodingException e) {
                 e.printStackTrace();
             }

             return convertView;
        }

        @Override
        public View getHeaderView(int position, View convertView, ViewGroup parent) {
            HeaderViewHolder holder;

            if (convertView == null) {
               holder = new HeaderViewHolder();
               convertView = mInflater.inflate(R.layout.date_separator, parent, false);
               holder.text = (TextView) convertView.findViewById(R.id.textSeparator);
               convertView.setTag(holder);
            } else {
               holder = (HeaderViewHolder) convertView.getTag();
            }
            String headerText = messages.get(position)._msg_date;
            holder.text.setText(headerText);
            return convertView;
         }

        /**
         * Remember that these have to be static, postion=1 should always return
         * the same Id that is.
         */
        @Override
        public long getHeaderId(int position) {
            // return the first character of the country as ID because this is what
            // headers are based upon
            return getSectionForPosition(position);
        }

        @Override
        public int getPositionForSection(int section) {
            if (mSectionIndices.length == 0) {
               return 0;
            }
            if (section >= mSectionIndices.length) {
                section = mSectionIndices.length - 1;
            } else if (section < 0) {
               section = 0;
            }
            return mSectionIndices[section];
        }

        @Override
        public int getSectionForPosition(int position) {
            for (int i = 0; i < mSectionIndices.length; i++) {
               if (position < mSectionIndices[i]) {
               return i - 1;
               }
            }
           return mSectionIndices.length - 1;
        }

        @Override
        public Object[] getSections() {
           return mSectionDates;
        }

        public void clear() {
           messages.clear();
           mSectionIndices = new int[0];
           mSectionDates = new String[0];
           notifyDataSetChanged();
        }

        public void restore(List<Msg> newMessages)
        {
           messages.clear();
           mSectionIndices = new int[0];
           mSectionDates = new String[0];
           messages = newMessages;
           mSectionIndices = getSectionIndices();
           mSectionDates = getSectionDates();
           notifyDataSetChanged();
        }

        public void add(Msg newMessage)
        {
           messages.add(0,newMessage);
           mSectionIndices = getSectionIndices();
           mSectionDates = getSectionDates();
        }

        class HeaderViewHolder {
           TextView text;
        }

        class ViewHolder {
           TextView text;
           TextView time;
        }
    }
gzix
  • 271
  • 3
  • 20
  • Some code (especially from the adapter) would help – Zharf Sep 25 '15 at 11:43
  • @Zharf I have added the adapter code above – gzix Sep 25 '15 at 12:21
  • Alright, how are you hilighting items? – Zharf Sep 25 '15 at 12:43
  • I might want to see that in the github library. This is the [link](https://github.com/emilsjolander/StickyListHeaders/blob/master/library/src/se/emilsjolander/stickylistheaders/StickyListHeadersListView.java) if you wanna take a look – gzix Sep 25 '15 at 12:49
  • Usually with this sort of behaviour the fault is at modifying the view contents outside of the adapter, and I don't see you doing anything in the adapter to change the background colour of a view. I'll have a closer look in a few hours when I have the time – Zharf Sep 25 '15 at 12:51

0 Answers0