Android List Adapter Holder性能问题

时间:2015-10-20 06:31:13

标签: android performance listview adapter android-viewholder

我的自定义列表视图适配器性能很差。我有一个包含多个图像(带有一些文本)的布局,其中一些图像在运行时基于来自服务器的数据变得可见。我有6个线性布局,每个布局包含这些图像和一些文本。

在我的持有人中,我首先将所有这些布局设置为GONE,然后根据数据将所需的数据设置为可见。有人可以指出哪个部分导致了性能问题。

public static class ViewHolder {

    public RelativeLayout llTopSpace;
    public TextView tvStartTime;
    public TextView tvEndTime;
    public TextView tvTotalCost;
    public TextView tvTotalDuration;
    public TextView tvStops;
    //public TextView tvVia;
    public List<ImageView> ivModes ;
    public List<TextView> ivNames ;
    public List<TextView> ivScs ;
    public List<LinearLayout> ivNexts ;
    public List<ImageView>  ivRings;

    public ViewHolder() {
        init();
    }

    private void init(){
        ivModes = new ArrayList<>(6);
        ivNames = new ArrayList<>(6);
        ivScs = new ArrayList<>(6);
        ivNexts = new ArrayList<>(6);
        ivRings = new ArrayList<>(6);
    }

    public void setAllInvisible(){
        for (ImageView ivMode : ivModes) {
            ivMode.setVisibility(View.GONE);
        }

        for (TextView ivName : ivNames) {
            ivName.setVisibility(View.GONE);
        }

        for (TextView ivSc : ivScs) {
            ivSc.setVisibility(View.GONE);
        }

        for (View ivRing : ivRings) {
            ((ImageView)ivRing).setImageResource(R.drawable.hollow_circle);
        }

        for (View ivNext : ivNexts) {
            ivNext.setVisibility(View.GONE);
        }


    }

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View itemLayoutView = convertView;
    ViewHolder holder;

    if (convertView == null) {

        itemLayoutView = inflater.inflate(R.layout.itineary_list_inflator, parent, false);

        /****** View Holder Object to contain tabitem.xml file elements ******/

        holder = new ViewHolder();
        holder.llTopSpace = (RelativeLayout) itemLayoutView.findViewById(R.id.ll_top_space);
        holder.tvStartTime = (TextView) itemLayoutView.findViewById(R.id.start_time);
        holder.tvEndTime = (TextView) itemLayoutView.findViewById(R.id.end_time);
        holder.tvTotalCost = (TextView) itemLayoutView.findViewById(R.id.price);
        holder.tvTotalDuration = (TextView) itemLayoutView.findViewById(R.id.duration);
        //holder.tvStops = (TextView) itemLayoutView.findViewById(R.id.stops);
        //holder.tvVia = (TextView) itemLayoutView.findViewById(R.id.via_cities);
        holder.ivModes.add(0,(ImageView) itemLayoutView.findViewById(R.id.iv_itinerary_mode_0));
        holder.ivModes.add(1,(ImageView) itemLayoutView.findViewById(R.id.iv_itinerary_mode_1));
        holder.ivModes.add(2,(ImageView) itemLayoutView.findViewById(R.id.iv_itinerary_mode_2));
        holder.ivModes.add(3,(ImageView) itemLayoutView.findViewById(R.id.iv_itinerary_mode_3));
        holder.ivModes.add(4,(ImageView) itemLayoutView.findViewById(R.id.iv_itinerary_mode_4));
        holder.ivModes.add(5,(ImageView) itemLayoutView.findViewById(R.id.iv_itinerary_mode_5));

        holder.ivNexts.add(0,(LinearLayout) itemLayoutView.findViewById(R.id.iv_itinerary_next_0));
        holder.ivNexts.add(1,(LinearLayout) itemLayoutView.findViewById(R.id.iv_itinerary_next_1));
        holder.ivNexts.add(2,(LinearLayout) itemLayoutView.findViewById(R.id.iv_itinerary_next_2));
        holder.ivNexts.add(3,(LinearLayout) itemLayoutView.findViewById(R.id.iv_itinerary_next_3));
        holder.ivNexts.add(4,(LinearLayout) itemLayoutView.findViewById(R.id.iv_itinerary_next_4));

        holder.ivNames.add(0, (TextView) itemLayoutView.findViewById(R.id.iv_itinerary_name_0));
        holder.ivNames.add(1, (TextView) itemLayoutView.findViewById(R.id.iv_itinerary_name_1));
        holder.ivNames.add(2, (TextView) itemLayoutView.findViewById(R.id.iv_itinerary_name_2));
        holder.ivNames.add(3, (TextView) itemLayoutView.findViewById(R.id.iv_itinerary_name_3));
        holder.ivNames.add(4, (TextView) itemLayoutView.findViewById(R.id.iv_itinerary_name_4));
        holder.ivNames.add(5, (TextView) itemLayoutView.findViewById(R.id.iv_itinerary_name_5));

        holder.ivScs.add(0, (TextView) itemLayoutView.findViewById(R.id.iv_itinerary_sc_0));
        holder.ivScs.add(1, (TextView) itemLayoutView.findViewById(R.id.iv_itinerary_sc_1));
        holder.ivScs.add(2, (TextView) itemLayoutView.findViewById(R.id.iv_itinerary_sc_2));
        holder.ivScs.add(3, (TextView) itemLayoutView.findViewById(R.id.iv_itinerary_sc_3));
        holder.ivScs.add(4, (TextView) itemLayoutView.findViewById(R.id.iv_itinerary_sc_4));
        holder.ivScs.add(5, (TextView) itemLayoutView.findViewById(R.id.iv_itinerary_sc_5));

        holder.ivRings.add(0, (ImageView) itemLayoutView.findViewById(R.id.ring_0));
        holder.ivRings.add(1, (ImageView) itemLayoutView.findViewById(R.id.ring_1));
        holder.ivRings.add(2, (ImageView) itemLayoutView.findViewById(R.id.ring_2));
        holder.ivRings.add(3, (ImageView) itemLayoutView.findViewById(R.id.ring_3));
        holder.ivRings.add(4, (ImageView) itemLayoutView.findViewById(R.id.ring_4));
        holder.ivRings.add(5, (ImageView) itemLayoutView.findViewById(R.id.ring_5));


        /************  Set holder with LayoutInflater ************/
        itemLayoutView.setTag(holder);
    }
    else {
        holder = (ViewHolder) itemLayoutView.getTag();
        holder.setAllInvisible();
    }
    /************  Set Model values in Holder elements ***********/

    item = mDataList.get(position);
    holder.tvStartTime.setText(HelperFunction.convertTime(item.getStartTime()));
    holder.tvEndTime.setText(HelperFunction.convertTime(item.getEndTime()));
    holder.tvTotalCost.setText(mContext.getResources().getString(R.string.Rs) + " " + item.getPrice());
    holder.tvTotalDuration.setText(HelperFunction.convertDuration(item.getDuration()));
    //holder.tvVia.setText(mDataList.get(position).getViaString());
    //holder.tvStops.setText(mDataList.get(position).getStopString());

    // Brute method to initialize images
    ArrayList<LegDetails> allLegs = item.getDetailsList();
    ArrayList<String> modeList = new ArrayList<>();

    for (int i = 0; i < allLegs.size(); i++) {
        modeList.add(allLegs.get(i).getMode());
        if(i == 0 || i == allLegs.size() - 1 || notSameSourceDestinationInIntracityCab(allLegs.get(i)))
            fillMode(holder, allLegs, modeList, i);
    }


    return itemLayoutView;
}

private boolean notSameSourceDestinationInIntracityCab(LegDetails legDetails) {
    String source = legDetails.getStartStationName();
    String destination = legDetails.getEndStationName();
    return !source.equalsIgnoreCase(destination);
}

private void fillMode(ViewHolder holder, ArrayList<LegDetails> modes_list, ArrayList<String> modeList, int index) {
    if (index < modeList.size()) {
        if (modeList.get(index).equals("f")) {
            //add images here
            holder.ivModes.get(index).setImageResource(R.drawable.flight_grey_new);
            holder.ivNames.get(index).setText("Spice Jet");
            holder.ivScs.get(index).setText(modes_list.get(index).getStartCode());
            holder.ivNames.get(index).setVisibility(View.VISIBLE);
            holder.ivScs.get(index).setVisibility(View.VISIBLE);

        } else if (modeList.get(index).equals("c")) {
            //add images here
            holder.ivModes.get(index).setImageResource(R.drawable.cab);
            //holder.ivNames.get(index).setText("Cab");
        } else if (modeList.get(index).equals("t")) {
            //add images here
            holder.ivModes.get(index).setImageResource(R.drawable.train_grey_new);
            holder.ivNames.get(index).setText("Rajdhani");
            holder.ivScs.get(index).setText(modes_list.get(index).getStartCode());
            holder.ivNames.get(index).setVisibility(View.VISIBLE);
            holder.ivScs.get(index).setVisibility(View.VISIBLE);
            //holder.ivNames.get(index).setText("Rajdhani");
        } else if (modeList.get(index).equals("b")) {
            //add images here
            holder.ivModes.get(index).setImageResource(R.drawable.bus_grey_new);
            //holder.ivNames.get(index).setText("Bus");
        } else if (modeList.get(index).equals("cn")) {
            //add images here
            holder.ivModes.get(index).setImageResource(R.drawable.flight_grey_new);
            //holder.ivNames.get(index).setText("Connecting");
        }

        holder.ivModes.get(index).setVisibility(View.VISIBLE);
        holder.ivNexts.get(index).setVisibility(View.VISIBLE);                  
  holder.ivRings.get(index).setVisibility(View.VISIBLE);

    }

    if (modes_list.size() == index + 1) {
          holder.ivRings.get(index).setImageResource(R.drawable.solid_grey_circle);
holder.ivRings.get(index).setVisibility(View.VISIBLE);
    }       
}

0 个答案:

没有答案