-4

I created listView with CustomAdapter. I am trying set another layout for first item of list view. I tried by position but its not helped. any suggestions?

public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHolder holder;

    Log.d("Position: ",String.valueOf(position));
    if (convertView == null) {
        if(position==0)
            convertView = inflater.inflate(R.layout.row1, null);
        else
            convertView = inflater.inflate(R.layout.row, null);

        holder = new ViewHolder();
        holder.nTitle = (TextView) convertView.findViewById(R.id.listTitle);
        holder.nDate = (TextView) convertView.findViewById(R.id.listDate);
        holder.nCategory = (TextView) convertView.findViewById(R.id.listCategory);
        holder.nImage = (ImageView) convertView.findViewById(R.id.listImage);
        convertView.setTag(holder);
    } else
        holder = (ViewHolder) convertView.getTag();

    if (newsList.get(position) != null) {
        holder.imageUrl = newsList.get(position).image;
        holder.nTitle.setText(newsList.get(position).title);
        holder.nCategory.setText(newsList.get(position).category);
        holder.nDate.setText(Base.getInstance(aContext).getDateString(newsList.get(position).date));
        holder.nImage.setImageResource(R.drawable.photo_bg);
    }

    return convertView;
}

My template: enter image description here

iProgrammer
  • 107
  • 1
  • 12

2 Answers2

3

You can do that

Have the below in the adapter

private static final int TYPE_ITEM1 = 0;
private static final int TYPE_ITEM2 = 1;

Override getItemViewType

int type;
@Override
public int getItemViewType(int position) {

    if (position== 0){ // position 0 
        type = TYPE_ITEM1;
    } else {
        type = TYPE_ITEM2;
    }

    return type;
}

And Override getViewTypeCount

 @Override
 public int getViewTypeCount() {
        return 2; // we have 2 types
 }

In getView

  int type = getItemViewType(position);
    // instead of if else you can use a case
   if (convertView == null)  {
    if (type == TYPE_ITEM1) { 
            //inflate layout of type1
      }
    if (type == TYPE_ITEM2) {
            //inflate layout of type2
    }  
   // Your rest of the code

Docs :

https://developer.android.com/reference/android/widget/Adapter.html#getItemViewType%28int%29

https://developer.android.com/reference/android/widget/Adapter.html#getViewTypeCount%28%29

Also go through this video. Most of the concepts are covered in the talk

https://www.youtube.com/watch?v=wDBM6wVEO70

Also using RecyclerView instead of ListView would be better

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • FIRST_TYPE? SECOND_TYPE? You mean TYPE_ITEM1? – iProgrammer Aug 07 '15 at 06:40
  • its working but not helped. Its same of my code. I wrote your code and in result every time when position is 0 its loading my first layout. – iProgrammer Aug 07 '15 at 06:50
  • 1
    @iProgrammer you want a different layout @ position 0. Is that what you want. . Is that your requirement am i understanding your problem right?. The item index starts @ 0. So you want to have a layout `row1.xml` inflated at that position then the above works. – Raghunandan Aug 07 '15 at 06:51
  • yes you are right. I just want set another layout only for first item. I uploaded my template image to question. – iProgrammer Aug 07 '15 at 06:57
  • @iProgrammer would it not be simple to design your layout holding your image at the top and listview below it. Also the above solution should work. You can also add a header to the listview ( image would scroll along with list). – Raghunandan Aug 07 '15 at 06:58
  • you know I am new in android and I dont know how to use header. Can you explain how to use? – iProgrammer Aug 07 '15 at 07:00
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/85386/discussion-between-raghunandan-and-iprogrammer). – Raghunandan Aug 07 '15 at 07:01
  • Here: http://applidium.github.io/HeaderListView/ I found some example. But its not what I want. – iProgrammer Aug 07 '15 at 07:01
  • @Raghunandan is it possible to set horizontal lisview in first item position and third item position based on this answer? – Stephen Aug 13 '16 at 10:58
  • 1
    @Naruto looking for something similar http://android-pratap.blogspot.in/2015/12/horizontal-recyclerview-in-vertical.html. Can be done with recyclerview – Raghunandan Aug 13 '16 at 17:56
  • @Raghunandan just now looked at your comment.surely it will helpful to me.thank you. – Stephen Aug 25 '16 at 08:20
  • @Raghunandan Thank you for your suggestion.with the help of that horizonatal recyclerview inside vertical recyclerview is working good. – Stephen Aug 31 '16 at 11:56
1

If only the first item is different than other items in the ListView, it's advisable to use addHeaderView instead of inflate it in the getView. First inflate that view in your Activity or Fragment then update add it to that listview.

Follow this.

ImMathan
  • 3,911
  • 4
  • 29
  • 45