StickyListHeadersListView MultiChoiceMode问题

时间:2015-09-25 11:37:05

标签: android android-listview sticky sectionheader

我正在使用github的StickyListHeadersListView安卓库为我的应用程序。它工作正常。在我实现MultiChoiceMode侦听器以复制和删除元素之后,突出显示元素时出现问题。

每当我选择一个元素并向上和向下滚动时,某些章节标题会自动突出显示,如下图所示

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

如何避免此行为。我有什么步骤吗?需要一些解决问题的方法。

我的适配器扩展了StickyListHeadersAdapter,如下所示

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

0 个答案:

没有答案