为什么ListView的适配器不能立即加载列表中的项目?

时间:2014-07-23 06:39:06

标签: android listview android-listview listadapter getview

ListView仅填充从适配器getView()方法可见的视图。我想一次加载所有项目。如果我的listView在屏幕上显示1项,getview()只调用一次,即使我的列表包含8,9,10 ...等项目

public class TrackAdapter extends BaseAdapter {

public static ArrayList<TrackSection> mlist;
Context mContext;
private LayoutInflater inflater;
private ImageLoader imageLoader;
private DisplayImageOptions options;
String foldername;
ListView mListView;
public static Species species;
public static final String POSITION = "position";
public static final String TYPE = "type";
int h2;
int h1;
int s;
int listPosition;
int viewPosition;
int offset;
private View view;
int previousPosition;
private Display display;
private boolean isFirstTime = true;
ArrayList<Bitmap> bmp;

public TrackAdapter(Context context, ArrayList<TrackSection> list,

Species species, ListView listView) {
    TrackAdapter.species = species;
    bmp = new ArrayList<Bitmap>();
    mContext = context;
    mListView = listView;
    mlist = list;

    inflater = LayoutInflater.from(mContext);
    imageLoader = ImageLoader.getInstance();
    WindowManager wm = (WindowManager) context
            .getSystemService(Context.WINDOW_SERVICE);
    display = wm.getDefaultDisplay();
    this.foldername = TrackAdapter.species.getCommanName();
    // /////////////////Create a option////////////////
    options = new DisplayImageOptions.Builder()
            .cacheInMemory(true)
            .showImageOnLoading(R.drawable.holder)
            .showImageOnFail(new ColorDrawable(Color.parseColor("#e6e3d2")))
            .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
            .bitmapConfig(Bitmap.Config.RGB_565).build();

}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return mlist.size();
}

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return arg0;
}

@Override
public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return arg0;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    Log.e("TrackAdapter", "postion"+position);
    view = convertView;
    ViewHolder mHolder = null;

    if (view == null) {
        mHolder = new ViewHolder();
        view = inflater.inflate(R.layout.inflate_track_adapter, null);
        mHolder.heading = (TextView) view.findViewById(R.id.trackHeading);
        mHolder.item = (TextView) view.findViewById(R.id.trackItems);
        mHolder.image = (ImageView) view.findViewById(R.id.trackImage);
        view.setTag(mHolder);
    } else {
        mHolder = (ViewHolder) view.getTag();
    }
    imageLoader.displayImage("assets://image/"
            + mlist.get(position).getMainTrackImage(), mHolder.image,
            options);

    h2 = mHolder.image.getHeight();
    h1 = mListView.getHeight();

    mHolder.heading.setText(mlist.get(position).getHeader());
    mHolder.heading.setTypeface(((DetailViewActivity) mContext).mFontBold);
    mHolder.item.setText(mlist.get(position).getfooter());
    mHolder.item.setTypeface(((DetailViewActivity) mContext).mFont);

    mHolder.image.setOnClickListener(new View.OnClickListener() {

        @SuppressLint("NewApi")
        @Override
        public void onClick(View arg0) {
            // open pageview from here
            // mListView.smoothScrollToPosition(position);
            // mListView.smoothScrollToPositionFromTop(position, h1 / 2 -
            // h2/ 2);
            int size = ((DetailViewActivity) mContext).mActionBar
                    .getHeight();
            listPosition = mListView.getHeight();
            viewPosition = view.getHeight();
            if (viewPosition > listPosition) {
                listPosition = display.getHeight();
                Log.v("Offset", "Greater Positon");
                Log.v("Offset", "" + offset + "View positon  "
                        + viewPosition);
                mListView.smoothScrollToPosition(position);
            } else {
                Log.v("Offset", "normal Positon");

                offset = ((listPosition / 2) - (viewPosition / 2));
                previousPosition = viewPosition / 2;
                Log.v("Offset", "" + offset + "View positon  "
                        + viewPosition);
                mListView.smoothScrollToPositionFromTop(position,
                        Math.abs(offset));
            }

            new CountDownTimer(300, 300) {

                @Override
                public void onTick(long millisUntilFinished) {

                }

                @Override
                public void onFinish() {
                    Intent intent = new Intent(mContext,
                            DetailPagerActivity.class);
                    intent.putExtra(POSITION, position);
                    intent.putExtra(TYPE, "track");
                    mContext.startActivity(intent);
                    ((DetailViewActivity) mContext)
                            .overridePendingTransition(R.anim.fade_in2,
                                    R.anim.fade_out2);
                }
            }.start();

        }
    });
    return view;
}

private static class ViewHolder {
    public TextView heading, item;
    public ImageView image;

}

public void SetItemAgain(ArrayList<TrackSection> list) {
    mlist = list;
    notifyDataSetChanged();
}

@SuppressLint("NewApi")
public void setItemLocation(int position) {
    listPosition = mListView.getHeight();
    viewPosition = view.getHeight();
    if (viewPosition > listPosition) {
        listPosition = display.getHeight();
        Log.v("Offset", "Greater Positon");
        Log.v("Offset", "" + offset + "View positon  " + viewPosition);
        mListView.smoothScrollToPosition(position);
    } else {
        Log.v("Offset", "normal Positon");

        offset = ((listPosition / 2) - (viewPosition / 2));
        previousPosition = viewPosition / 2;
        Log.v("Offset", "" + offset + "View positon  " + viewPosition);
        mListView.smoothScrollToPositionFromTop(position, Math.abs(offset));
    }
}
}

R.layout.inflate_track_adapter

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/lite_brown" >

    <TextView
        android:id="@+id/trackItems"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/trackImage"
        android:layout_marginTop="5dp"
        android:paddingBottom="5dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/trackHeading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/trackTop_Layout"
        android:layout_marginLeft="16dp"
        android:paddingBottom="5dp"
        android:paddingTop="10dp"
        android:textSize="18sp"
        android:textStyle="bold" />


    <ImageView
        android:id="@+id/trackImage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/trackHeading"
        android:layout_marginTop="5dp" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

@Zohaib好的,所以如果您需要将图片加载到列表中而不会对性能产生不良影响,我强烈建议您使用本教程:

http://dahliascherr.blogspot.fr/2012/04/tutorial-android-les-listes.html

用法语,所以我总结一下对你来说更重要。

你应该使用持有人。实际上,持有者只会在加载视图时创建一次对象,并将其保留在内存中以避免下次看到时出现延迟。这对你来说应该是完美的,这是教程中的例子:

//membre de classe (ne dépend pas de l'instance de la classe contenante)
// utilisée pour stocker des références
static class ViewHolder {
    public TextView nameView;
    public TextView sizeView;
    public ImageView iconeView;
}

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

    ViewHolder holder;

    View rowView = convertView;

    if (rowView == null) {
        //création de l’objet View à partir de la ressource Layout
        rowView = mInflater.inflate(R.layout.row_list, null);

        holder = new ViewHolder();
        holder.nameView = (TextView) rowView.findViewById(R.id.name);
        holder.sizeView = (TextView) rowView.findViewById(R.id.size);
        holder.iconeView = (ImageView) rowView.findViewById(R.id.icon);
        rowView.setTag(holder);
    }

    else {
        holder = (ViewHolder) rowView.getTag();
    }

    //récupération du document identifié par sa position dans la liste
    Document document = mDocuments.get(position);

    //Valorisation du Texte1 de la Vue avec le nom du document
    holder.nameView.setText(document.getName());

    //Valorisation du Texte2 avec la taille du document
    holder.sizeView.setText(String.valueOf(document.getSize()) + "Ko");

    //Affichage de l’icône correspondante
    holder.iconeView.setImageResource(document.getIcone());

    return rowView;
}

一些解释: 静态类ViewHolder是该类的成员。 你应该在你的适配器中声明它。

我提供的getview方法使用了视图。

尝试告诉我它是否更好;)!

祝你好运