Android StickyListHeadersAdapter - 滚动延迟

时间:2016-07-29 05:40:51

标签: android performance android-layout listview android-fragments

我通过在StickyList(https://github.com/emilsjolander/StickyListHeaders)中集成列表来解决下一个问题,但滚动时会有相当大的延迟。

Mock StickyList

适配器填充StickyList转动适配器ListView(橙色),这是适配器代码:

public class VisitAdapter extends BaseAdapter implements StickyListHeadersAdapter {

    private final Context mContext;
    private List<Visit> mVisitList;
    private List<Cuestionario> surveysArray;
    static ViewHolder holder;

    public VisitAdapter(Context context, List<Visit> visits) {
        this.mContext = context;
        this.mVisitList = visits;
    }

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

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_visit, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final Visit visit = mVisitList.get(position);

        holder.addressTextView.setText(visit.getName());
        holder.contactTextView.setText(visit.getPearsonKey());
        holder.prospectingkey.setText(visit.getKey());
        holder.rentpocket.setText(visit.getC12());

        holder.navigateLayout.setTag(visit);
        holder.navigateLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().postSticky(new EventAddressDetails((Visit) v.getTag()));
                mContext.startActivity(new Intent(mContext, LocationDialog.class));

            }
        });

        holder.positionTextView.setText(
                visit.getOrder() < 10 ? "0" + String.valueOf(visit.getOrder()) :
                        String.valueOf(visit.getOrder()));

        holder.checkLayout.setTag(visit);
        if (visit.getStatus() == Constants.STATUS_ENABLED) {
            holder.checkImg.setImageResource(R.drawable.ic_check_gray);
            holder.checkTxt.setText(mContext.getResources().getString(R.string.adapter_option_img_start));
            holder.checkLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    EventBus.getDefault().post(new EventOpenSurvey((Visit) view.getTag(), false));
                }
            });

        } else if (visit.getStatus() == Constants.STATUS_START) {

            holder.checkImg.setImageResource(R.drawable.ic_check_green);
            holder.checkTxt.setText(mContext.getResources().getString(R.string.adapter_option_img_start));
            holder.checkLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    EventBus.getDefault().post(new EventOpenCheck((Visit) view.getTag()));
                }
            });

        } else if (visit.getStatus() == Constants.STATUS_FINISH) {

            holder.checkImg.setImageResource(R.drawable.ic_check_red);
            holder.checkTxt.setText(mContext.getResources().getString(R.string.adapter_option_img_finish));
            holder.checkLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    EventBus.getDefault().post(new EventOpenCheck((Visit) view.getTag()));
                }
            });

            populateProcessList(visit.getCheckPointKey(), visit, visit.getRutaid(), surveysArray);
            Utils.setListViewHeightBasedOnChildren(holder.processContainer);
        }
        return convertView;
    }

    private void populateProcessList(
            String cvePDVGeneric, Visit visitGeneric,
            int rutaIdGeneric, List<Cuestionario> surveysArrayGeneric) {

        surveysArray = GreenDao.getSurveys(mContext);

        if (surveysArrayGeneric != null && surveysArrayGeneric.size() > 0) {

            holder.processContainer.setAdapter(
                    new SurveyTypeAdapter(
                            mContext, R.layout.item_survey_type, surveysArrayGeneric,
                            cvePDVGeneric, rutaIdGeneric, visitGeneric));
        } else {
            holder.processContainer.setAdapter(
                    new SurveyTypeAdapter(
                            mContext, R.layout.item_survey_type, null, cvePDVGeneric, rutaIdGeneric, visitGeneric));

            Alerts.showToastMessage(mContext, mContext.getResources().getString(R.string.msg_error_empty_survey));
        }
    }

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

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_header, null);
            holder = new HeaderViewHolder(convertView);
            convertView.setTag(holder);

        } else {
            holder = (HeaderViewHolder) convertView.getTag();
        }

        holder.countText.setText( getCount() + " PDV");

        if (mVisitList.get(position).getDate() != null && !mVisitList.get(position).getDate().isEmpty()) {
            holder.textItemHeader.setText(Utils.getDayDateString(mVisitList.get(position).getDate()));
        }

        holder.countText.setVisibility(View.VISIBLE);
        return convertView;
    }

    @Override
    public long getHeaderId(int position) {
        long sum = 0;
        try {
            if (mVisitList.get(position).getDate() != null && !mVisitList.get(position).getDate().isEmpty()) {
                char[] charArray = Utils.getDayDateString(mVisitList.get(position).getDate())
                        .toCharArray();
                for (char aCharArray : charArray) {
                    sum += aCharArray;
                }
            }
        } catch (Exception ignored) {
            Crashlytics.logException(ignored);
        }
        return sum;
    }

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


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


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

    public void setVisits(List<Visit> visits) {
        this.mVisitList = visits;
    }

    static class ViewHolder {

        @Bind(R.id.item_prospecting_contact)
        TextView contactTextView;
        @Bind(R.id.item_prospecting_address)
        TextView addressTextView;
        @Bind(R.id.item_prospecting_navigate)
        LinearLayout navigateLayout;
        @Bind(R.id.item_prospecting_check)
        LinearLayout checkLayout;
        @Bind(R.id.item_prospecting_position)
        TextView positionTextView;
        @Bind(R.id.item_visit_img_check)
        ImageView checkImg;
        @Bind(R.id.item_visit_txt_check)
        TextView checkTxt;
        @Bind(R.id.item_prospecting_key)
        TextView prospectingkey;
        @Bind(R.id.item_rent_pocket)
        TextView rentpocket;
        @Bind(R.id.item_select_survey_list)
        ListView processContainer;
        @Bind(R.id.item_prospecting_header_container)
        RelativeLayout mHeaderContainer;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    static class HeaderViewHolder {
        @Bind(R.id.item_header_text)
        TextView textItemHeader;
        @Bind(R.id.item_header_counter)
        TextView countText;

        public HeaderViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}

提前感谢您的帮助,墨西哥的问候。

0 个答案:

没有答案