StickyListHeadersListView MultiChoiceMode issue

♀尐吖头ヾ 提交于 2019-12-25 06:07:24

问题


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

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

来源:https://stackoverflow.com/questions/32781326/stickylistheaderslistview-multichoicemode-issue

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