1

I've a problem that should not be weird but I don't know . I had not used Holder in a first time but my last item was the same as the first , with a Holder my last item is ok but not its layout .

I have an ArrayList and Adapter :

listViewNews = (ListView)view.findViewById(R.id.list_news_items);
    newAdapter = new NewsListAdapter(getActivity(),
            arListNews);

    listViewNews.setOnItemClickListener(new viewNewClickListener());

    listViewNews.setAdapter(newAdapter);

And in the latter I have a specific layout for the first item in the list and another for the rest.

public class NewsListAdapter extends BaseAdapter {
 
    private Context context;
    private ArrayList<Actualite> newsItems;
    Typeface custom_font_bold, custom_font_book;
 
    public NewsListAdapter(Context context, ArrayList<Actualite> newsItems){
        this.context = context;
        this.newsItems = newsItems;
    }
 
    @Override
    public int getCount() {
        return newsItems.size();
    }
 
    @Override
    public Object getItem(int position) {
        return newsItems.get(position);
    }
 
    @Override
    public long getItemId(int position) {
        return position;
    }
 
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        NewHolder holder = null;
 
        if (view == null) {
            LayoutInflater mInflater = (LayoutInflater)
                    context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            holder = new NewHolder();
 
            if(position == 0) { // First item
                view = mInflater.inflate(R.layout.new_list_item_une, null);
                holder.resume = (TextView) view.findViewById(R.id.resume);
            } else { // The rest
                view = mInflater.inflate(R.layout.new_list_item, null);
            }
            holder.imageActu = (ImageView) view.findViewById(R.id.imageActu);
            holder.txtTitle = (TextView) view.findViewById(R.id.titreActu);
            holder.vw = (View) view.findViewById(R.id.colorActu);
            holder.thematique = (TextView) view.findViewById(R.id.thematique);
 
            view.setTag(holder);
        } else {
            holder = (NewHolder) view.getTag();
        }
        holder.imageActu.setBackgroundColor(context.getResources().getColor(R.color.grey_200_transparent));
         
        holder.txtTitle.setText(newsItems.get(position).getTitre());
 
        holder.vw.setBackgroundColor(Color.rgb(r, g, b));
 
        holder.thematique.setText(th.toUpperCase());
        holder.thematique.setTextColor(Color.rgb(r, g, b));
 
        if(position == 0) {
            holder.resume.setText(Html.fromHtml(newsItems.get(position).getResume()));
            holder.resume.setTextColor(context.getResources().getColor(R.color.grey_200_transparent));
        }
        return view;
    }
 
    private static class NewHolder {
        ImageView imageActu;
        TextView txtTitle;
        View vw;
        TextView thematique;
        TextView resume;
    }

Why is the layout of my last item is the same as the first ? From my Log, there is no position for the last item , so it uses the layout of the first element. I also note that for the last item is not within the if :

if (view == null) { }
jere
  • 57
  • 9
  • If you only want different layout for topmost item , then it is advisable to use header of listview. Otherwise see this http://stackoverflow.com/questions/4777272/android-listview-with-different-layout-for-each-row – MohK May 26 '15 at 12:47

1 Answers1

2

Adapter reuse views. For example, if first cell view is out of screen and last cell view needs rendering, adapter will use first view instead of create a new one and then last view will be the same as first one.

You must use viewType for that. Just override next methods in your adapter:

private static final int FIRST_CELL = 0;
private static final int DEFAULT_CELL = 1;

...

public int getViewTypeCount() { 
    return 2; 
}

public int getItemViewType(int position) { 
    return position == 0 ? FIRST_CELL : DEFAULT_CELL; 
}  

In your getView methods, use

if (getItemViewType(position) == FIRST_CELL)

instead of

if(position == 0)

Your adapter will now treat first cell with a different view type. You should also consider using HeaderView for this kind of problem.

Médéric
  • 938
  • 4
  • 12
  • Thx for answer, but i don't understand why the problem repeat to the 6 items. – jere May 26 '15 at 11:41
  • What do you mean? Why the 6th item is the same as the first one? – Médéric May 26 '15 at 12:08
  • Yes, in my ListView (visually in my device) the 6th item layout = first item layout. – jere May 26 '15 at 12:14
  • 6th item = 7th position, after the 7th position I no longer happening in the if (view == null) – jere May 26 '15 at 12:16
  • I guess you need to scroll your ListView to see the 6th item, is that right? – Médéric May 26 '15 at 12:18
  • My listview contains 10 items (for example) I have a specific layout for the first item and then another for the rest. except that the 7th item has the same layout as the first but not the next . – jere May 26 '15 at 12:23
  • As explain in my answer, adapter constructs view only when needed. For the 6 first item, it create a first view A and then 6 view B (depending of position index). Then when you scroll to see the 7th item, the first one move out of the visible rect. Adapter use that first view A to render the 7th item instead of create a new one: that's how an adapter works ;). Then for the 8th, it will use the second one with the expected layout B. – Médéric May 26 '15 at 12:44
  • You will not pass in the view == null condition after the 6th item because there will not need any extra view. You must use ViewType like in my answer to define first view as different than other one. Did you try like that? – Médéric May 26 '15 at 12:44
  • It's ok for the layout at 7th item with ViewType. But when i scroll down and then back up the images of empty images are normally filled with the images below. – jere May 26 '15 at 12:57
  • That was reeeeeally perfect, you saved my hours, man. I did not adapter takes arbitrarily choose item as first-positioned cell – Farid Apr 28 '16 at 17:55