0

伙计们。

我的列表视图有问题。我使用 ViewHolder 来优化性能,但我仍然得到一些列表视图滞后。你能建议吗?

这是我的 ListView XML

<ListView
                        android:id="@+id/list1"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:divider="@android:color/transparent"
                        android:dividerHeight="10dp"
                        android:fadingEdge="none"
                        android:fastScrollEnabled="true"
                        android:headerDividersEnabled="false"
                        android:listSelector="@android:color/transparent"
                        android:scrollingCache="false" />

这也是我为回收而实施的带有 ViewHolder 的自定义 SimplecursorAdapter

public class AlternateRowCursorAdapter extends SimpleCursorAdapter {

int layoutn;
LayoutInflater mInflater;
SQLiteDatabase mDb;
MyDbHelper mHelper;
private final Cursor mCursor;
private final int mLayout;
private final LayoutInflater mLayoutInflater;
private final Context mContext;

public static final String TABLE_NAME = "MSGS";


public AlternateRowCursorAdapter(Context context, int layout, Cursor c,
        String[] from, int[] to) {

    super(context, R.layout.listtype, c, from, to);

    this.mContext = context;
    this.mCursor = c;
    this.mLayout = layout;
    this.mLayoutInflater = LayoutInflater.from(context);

}

private final class ViewHolder {

    public TextView Title;
    public ImageView one, two, three, four, five, six, seven, zina;

}

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

    if (mCursor.moveToPosition(position)) {

         final ViewHolder viewHolder;

        if (convertView == null) {


            convertView = mLayoutInflater.inflate(mLayout, parent, false);

            viewHolder = new ViewHolder();

            viewHolder.Title = (TextView) convertView
                    .findViewById(R.id.Sandtit);
            viewHolder.one = (ImageView) convertView
                    .findViewById(R.id.imageView5);
            viewHolder.two = (ImageView) convertView
                    .findViewById(R.id.imageView4);
            viewHolder.three = (ImageView) convertView
                    .findViewById(R.id.ImageView03);
            viewHolder.four = (ImageView) convertView
                    .findViewById(R.id.ImageView02);
            viewHolder.five = (ImageView) convertView
                    .findViewById(R.id.imageView1);
            viewHolder.six = (ImageView) convertView
                    .findViewById(R.id.ImageView06);
            viewHolder.seven = (ImageView) convertView
                    .findViewById(R.id.ImageView01);
            viewHolder.zina = (ImageView) convertView
                    .findViewById(R.id.imageView2);

            convertView.setTag(viewHolder);
        }

        else {

            viewHolder = (ViewHolder) convertView.getTag();

        }

        final String Title, SandID, MsgID, MsgFav;

        Title = mCursor.getString(2);
        SandID = mCursor.getString(1);
        MsgID = mCursor.getString(0);
        MsgFav = mCursor.getString(4);

        Typeface tf = Typeface.createFromAsset(mContext.getAssets(),
            "fonts/khalaadsara.ttf");

        viewHolder.Title.setText(Title);
        viewHolder.Title.setTypeface(tf);


        try {
            mHelper = new MyDbHelper(mContext);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        if (MsgFav.contentEquals("YES")) {
            viewHolder.one.setImageResource(R.drawable.favorpress);
        } else {
            viewHolder.one.setImageResource(R.drawable.favornorm);
        }

        mDb = mHelper.getWritableDatabase();

        viewHolder.one.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                if (MsgFav.contentEquals("NO")) {
                    ContentValues args = new ContentValues();
                    args.put(mHelper.COL_MsgFavor, "YES");
                    mDb.update(TABLE_NAME, args, "_id = ?",
                            new String[] { MsgID });

                    viewHolder.one.setImageResource(R.drawable.favorpress);
                    Toast.makeText(
                            mContext,
                            "تم إضافة الرسالة إلي الرسائل المفضلة بنجاح...",
                            9000).show();

                    notifyDataSetChanged();
                    mCursor.requery();

                }

                else {
                    ContentValues args = new ContentValues();
                    args.put(mHelper.COL_MsgFavor, "NO");
                    mDb.update(TABLE_NAME, args, "_id = ?",
                            new String[] { MsgID });

                    viewHolder.one.setImageResource(R.drawable.favornorm);
                    Toast.makeText(mContext,
                            "تم حذف الرسالة من الرسائل المفضلة بنجاح...",
                            9000).show();

                    notifyDataSetChanged();
                    mCursor.requery();

                }

            }

        });


        viewHolder.two.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                Intent emailIntentt = new Intent(
                        android.content.Intent.ACTION_VIEW);

                emailIntentt.setData(Uri.parse("sms:"));

                String breaks = "\n";

                emailIntentt.putExtra("sms_body", Title);

                mContext.startActivity(emailIntentt);

            }
        });

        viewHolder.three.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                viewHolder.Title.setTextSize(TypedValue.COMPLEX_UNIT_PX,
                        (viewHolder.Title.getTextSize() - 1f));

            }

        });

        viewHolder.four.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                Intent emailIntentt = new Intent(
                        android.content.Intent.ACTION_SEND);

                String breaks = "\n";

                String[] recipientss = new String[] {
                        "من فضلك...أدخل بريدك الأليكتروني", "", };

                emailIntentt.putExtra(android.content.Intent.EXTRA_EMAIL,
                        recipientss);

                emailIntentt.putExtra(android.content.Intent.EXTRA_SUBJECT,
                        "مسجات رمضان 2013");

                emailIntentt.putExtra(android.content.Intent.EXTRA_TEXT,
                        breaks + Title);

                emailIntentt.setType("text/plain");

                mContext.startActivity(Intent.createChooser(emailIntentt,
                        "مشاركة بواسطة..."));


            }
        });

        viewHolder.five.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                ClipboardManager ClipMan = (ClipboardManager) mContext
                        .getSystemService(Context.CLIPBOARD_SERVICE);
                ClipMan.setText(Title);

                Toast.makeText(mContext, "تم نسخ الرسالة بنجاح....", 9000)
                        .show();
            }
        });

        viewHolder.six.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                viewHolder.Title.setTextSize(TypedValue.COMPLEX_UNIT_PX,
                        (viewHolder.Title.getTextSize() + 1f));
            }
        });

        viewHolder.seven.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                Intent waIntent = new Intent(Intent.ACTION_SEND);

                if (waIntent != null)

                {

                    waIntent.setType("text/plain");
                    String text = Title;
                    waIntent.setPackage("com.whatsapp");

                    waIntent.putExtra(Intent.EXTRA_TEXT, text);//
                    mContext.startActivity(Intent.createChooser(waIntent,
                            "مشاركة بواسطة..."));
                }

            }
        });

    }

    return convertView;
}

}

非常感谢您的帮助!

4

1 回答 1

1

getView()每次需要填充列表中的新项目时,都会调用该方法。您在该方法中做了很多工作以获得流畅的性能。我会重构任何不需要设置布局并将其移动到其他地方的东西,这样它就不会一直被调用。尤其是监听器和数据库调用。他们真的不需要在那里。每次创建一个新的列表项时,都会创建 7 个新的侦听器对象……哎呀

您可以onListItemClick()ActivityFragment您的使用中覆盖,因此当用户单击列表项时,您可以执行所需的工作。然后您可以使用单击的项目的位置来确定用户单击的内容。这是我建议这样做的方式。

或者

您可以尝试为您需要的每个侦听器创建一个内部类,并将它们存储在一个实例变量中,这样每次显示新列表项时都不需要实例化它们。但是,我不建议这样做。它有点臭,它仍然可能无法完全摆脱性能问题。

于 2013-06-12T01:21:34.533 回答