1

ListView only populate those view that are visible from adapter getView() method. I want to load all items at once. If my listView show 1 item at screen, getview() called only one times even my list contain 8,9,10... etc items

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>
Priyank Patel
  • 12,244
  • 8
  • 65
  • 85
Zohaib Akram
  • 617
  • 1
  • 5
  • 16

1 Answers1

0

@Zohaib ok, so if you need to load pictures into your list without bad impact on performance i highly recommend you to use this tutorial :

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

It's in French so i'll sum up the more important for you.

You should use holder. Indeed, holder will create the object only once when the view is loaded and keep it in memory to avoid latency next time you see it. That should be perfect for you, here's the example from the tutorial :

//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;
}

Some explanations : static class ViewHolder is a member of the class. You should declare it inside your adapter.

And the getview method i provided uses the viewholder.

Give a try and tell me if it's better ;) !

Good luck

maxime1992
  • 22,502
  • 10
  • 80
  • 121
  • 1
    thanks for reply , i am already using view holder pattern :( i want to load all items at once mean when i pass adapter to listview adapter,Adapter should iterate to whole items size , its only iterate to that numbers of items that at visible to listview . – Zohaib Akram Jul 23 '14 at 07:02
  • I did a little research, maybe you could find your happiness with this : http://stackoverflow.com/questions/11696584/displaying-photos-in-a-listview-how-to-get-adapter-to-preload-before-users-can :) – maxime1992 Jul 23 '14 at 07:31