4

我有一个带有 CustomCursorAdapter 的 ListView。Listview 滚动缓慢。这是我使用的代码。

public class CustomCursorAdapter extends CursorAdapter {

private ViewHolder holder=null;
private Activity mContext=null;
private ArrayList<Boolean> mCheckedList=null;
private Button mCancel=null;
private Cursor mCursor = null;
private DatabaseHandler mDbHandler;
private int dStatus;
private LayoutInflater inflater;

public CustomCursorAdapter(Activity context) {
    super(context, cursor);
    this.mCursor = cursor;
    this.mContext = context;
    this.mDbHandler = new DatabaseHandler(context);
    this.inflater = LayoutInflater.from(context);
}


@Override
public void bindView(View view, Context arg1, Cursor c) {
    holder=(ViewHolder) view.getTag();
    long duration = c.getLong(c.getColumnIndex(DatabaseHandler.DURATION));

    holder.checkToSend.setTag(c.getPosition());
    holder.editDitn.setTag(c.getPosition());
    holder.pendingLayout.setTag(c.getPosition());
    holder.name.setText(c.getString(c.getColumnIndex(DatabaseHandler.NAME)));

    holder.worktype.setText(c.getString(c.getColumnIndex(DatabaseHandler.WORKTYPE)));
    holder.comment.setText(c.getString(c.getColumnIndex(DatabaseHandler.COMMENT)));
    int prio = c.getInt(c.getColumnIndex(DatabaseHandler.PRIORITY));
    if(prio == 1)
        holder.priority.setVisibility(View.VISIBLE);
    else if(prio == 0)
        holder.priority.setVisibility(View.INVISIBLE);
    boolean check = mCheckedList.get(c.getPosition());
    holder.checkToSend.setChecked(check);
    onEnableOrDisable(true);
    int dictStat = c.getInt(c.getColumnIndex(DatabaseHandler.STATUS));
    if(c.getInt(c.getColumnIndex(DatabaseHandler.ISACTIVE))==1 || ((dictStat == 2 || dictStat == 3)&&!DMApplication.isEditMode()))
        holder.checkToSend.setVisibility(View.GONE);
    else
        holder.checkToSend.setVisibility(View.VISIBLE);

    switch (dictStat) {
    case 1:
        holder.status.setText("");
        if(DMApplication.isOnEditState()&&DMApplication.getTabPos()==1)
            holder.checkToSend.setVisibility(View.GONE);
        else
            holder.checkToSend.setVisibility(View.VISIBLE);
        onEnableOrDisable(true);
        break;
    case 111:
        holder.status.setTextColor(mContext.getResources().getColor(R.color.status_blue));
        onEnableOrDisable(false);
        holder.status.setText(mContext.getResources().getString(R.string.Property_sending));
        break;
    case 222:
        holder.status.setTextColor(mContext.getResources().getColor(R.color.status_red));
        holder.status.setText(mContext.getResources().getString(R.string.Property_Timeout));
        onEnableOrDisable(true);
        break;
    case 333:
        holder.status.setTextColor(mContext.getResources().getColor(R.color.status_blue));
        holder.status.setText(mContext.getResources().getString(R.string.Property_Retrying));
        onEnableOrDisable(false);
        break;
    case 444:
        holder.status.setTextColor(mContext.getResources().getColor(R.color.status_red));
        holder.status.setText(mContext.getResources().getString(R.string.Property_Conversion_Failed));
        onEnableOrDisable(true);
        break;
    case 555:
        holder.status.setTextColor(mContext.getResources().getColor(R.color.status_blue));
        onEnableOrDisable(false);
        holder.status.setText(mContext.getResources().getString(R.string.Property_Waiting_Send));
        break;
    case 20:
        holder.status.setTextColor(mContext.getResources().getColor(R.color.status_red));
        holder.status.setText(mContext.getResources().getString(R.string.property_sending_failed));
        onEnableOrDisable(true);
        break;
    case 3:
        holder.status.setText(mContext.getResources().getString(R.string.Property_Via_Email));
        onEnableOrDisable(true);
        break;

    default:
        holder.status.setText("");
        onEnableOrDisable(true);
        break;
    }
    int isflashair = c.getInt(c.getColumnIndex(DatabaseHandler.ISFLASHAIR));
    String formattedDur = Utilities.getDurationInTimerFormat(duration);
    if(isflashair == 0){
        holder.imgflash.setVisibility(View.GONE);
        holder.editDitn.setVisibility(View.VISIBLE);
        holder.pendingLayout.setClickable(true);
        if(dictStat == 2 || dictStat == 3){
            holder.date.setText(DMApplication.getLocalizedDateAndTime(c.getString(c.getColumnIndex(DatabaseHandler.SENT_DATE)))+"  "
                +formattedDur);
        }else{
            holder.date.setText(DMApplication.getLocalizedDateAndTime(c.getString(c.getColumnIndex(DatabaseHandler.REC_END_DATE)))+"  "
                    +formattedDur);
        }
    }else{
        holder.imgflash.setVisibility(View.VISIBLE);
        holder.editDitn.setVisibility(View.GONE);
        holder.pendingLayout.setClickable(false);
        if(dictStat == 2 || dictStat == 3){
            holder.date.setText(DMApplication.getLocalizedDateAndTime(c.getString(c.getColumnIndex(DatabaseHandler.SENT_DATE))));
        }else{

            holder.date.setText(DMApplication.getLocalizedDateAndTime(c.getString(c.getColumnIndex(DatabaseHandler.REC_END_DATE))));
        }

    }

    notifyDataSetChanged();
}
private void  onEnableOrDisable(boolean enable)
{
    if(DMApplication.getTabPos()==1)
    {
        if(enable)
        {
            holder.checkToSend.setEnabled(true);
            holder.pendingLayout.setEnabled(true);
            holder.editDitn.setEnabled(true);
        }
        else
        {
            holder.checkToSend.setVisibility(View.GONE);
            holder.checkToSend.setEnabled(false);
            holder.pendingLayout.setEnabled(false);
            holder.editDitn.setEnabled(false);
        }
    }
}

@Override
public View newView(Context arg0,final Cursor cursor, ViewGroup parent) {
    View retView = inflater.inflate(R.layout.tab_list_layout, parent, false);
    holder = new ViewHolder();
    holder.name = (TextView) retView.findViewById(R.id.text_tab_list_dictation_name);
    holder.worktype = (TextView) retView.findViewById(R.id.text_tab_list_worktype);
    holder.comment = (TextView) retView.findViewById(R.id.text_tab_list_comment);
    holder.date = (TextView) retView.findViewById(R.id.text_tab_list_dictation_date);
    holder.status = (TextView) retView.findViewById(R.id.text_tab_list_dictation_status);
    holder.checkToSend = (CheckBox) retView.findViewById(R.id.chkToSend);
    holder.priority = (ImageView) retView.findViewById(R.id.img_tab_list_priority);
    holder.editDitn = (ImageButton) retView.findViewById(R.id.img_tab_list_edit_dictation);
    holder.imgflash = (ImageView) retView.findViewById(R.id.img_is_flashair);
    holder.pendingLayout=(RelativeLayout)retView.findViewById(R.id.relativeTabDictateDetails);
    holder.pendingLayout.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            int pos = (Integer) v.getTag();             
            Cursor c = (Cursor) getItem(pos);
            // some code

        }
    });
    holder.checkToSend.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            int pos = (Integer) v.getTag();
            CheckBox chk = (CheckBox) v;
            // some code
        }
    });
    holder.editDitn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            int pos = (Integer) v.getTag();
            Cursor c = (Cursor) getItem(pos);
            // some code
        }
    });
    retView.setTag(holder);
    return retView;
}

static class ViewHolder{
    TextView name = null;
    TextView date = null;
    TextView worktype = null;
    TextView comment = null;
    TextView status = null;
    CheckBox checkToSend = null;
    ImageView priority = null;
    RelativeLayout pendingLayout=null;
    ImageButton editDitn = null;
    ImageView imgflash = null;
}
}

请让我知道我的代码是否有问题。

4

2 回答 2

1

最好使用getView()而不是使用它,newView()因为它为您提供了一个回收的 View ( convertView),这是您滚动时从屏幕中弹出的 View,因此这将减少使用此回收的 View 创建新 View 所需的时间并减少retView每次创建视图时删除视图 ( ) 所需的垃圾收集。

于 2013-07-17T09:14:30.917 回答
1

是的,您可以使用 getView 代替 newView。因为它为您提供了回收的视图作为反视图。

@Override
    public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder;
    View retView = convertView;

    if (convertView == null) {
        retView = inflater.inflate(R.layout.tab_list_layout, parent, false);
        holder = new ViewHolder();
        holder.name = (TextView) retView.findViewById(R.id.text_tab_list_dictation_name);
        holder.worktype = (TextView) retView.findViewById(R.id.text_tab_list_worktype);
        holder.comment = (TextView) retView.findViewById(R.id.text_tab_list_comment);
        holder.date = (TextView) retView.findViewById(R.id.text_tab_list_dictation_date);
        holder.status = (TextView) retView.findViewById(R.id.text_tab_list_dictation_status);
        holder.checkToSend = (CheckBox) retView.findViewById(R.id.chkToSend);
        holder.priority = (ImageView) retView.findViewById(R.id.img_tab_list_priority);
        holder.editDitn = (ImageButton) retView.findViewById(R.id.img_tab_list_edit_dictation);
        holder.imgflash = (ImageView) retView.findViewById(R.id.img_is_flashair);
        holder.pendingLayout=(RelativeLayout)retView.findViewById(R.id.relativeTabDictateDetails);
        holder.pendingLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                int pos = (Integer) v.getTag();             
                Cursor c = (Cursor) getItem(pos);
                // some code

            }
        });
        holder.checkToSend.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                int pos = (Integer) v.getTag();
                CheckBox chk = (CheckBox) v;
                // some code
            }
        });
        holder.editDitn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                int pos = (Integer) v.getTag();
                Cursor c = (Cursor) getItem(pos);
                // some code
            }
        });
        retView.setTag(holder);
       }

     else {

    holder = (ViewHolder) retView.getTag();
            }
    return retView ;
    }
于 2013-07-17T09:24:56.853 回答