Android different Row layout only for first row in

2019-09-17 09:11发布

I am making an Custom listview with Custom Adapter extending BaseAdapter. Now I want to set a different layout only for first row and another layout for all other rows. I am using this code but it sets the special layout for 1st row and also repeats it after every 5/6 rows. How can I fix it and can set it only for 1st row and another layout for all other rows.

public class NewsAdapter extends BaseAdapter {
    private Context context;
    List<News> newsList;
    private Typeface customBanglaFont;
    private LayoutInflater inflater;
    ImageLoader imageLoader = AppController.getInstance().getImageLoader();

    public NewsAdapter(Context context, List<News> newsList){
        this.context = context;
        this.newsList = newsList;
    }

    @Override
    public int getCount() {
        return newsList.size();
    }

    @Override
    public Object getItem(int arg0) {
        return newsList.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        View row;
        row = convertView;
        ViewHolder holder=null;

        if(row == null){
            if(position == 0){
                inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row =inflater.inflate(R.layout.row_single_big_view,viewGroup,false);
            }
            else{
                inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row =inflater.inflate(R.layout.row_single_default,viewGroup,false);
            }

            holder = new ViewHolder(row);
            row.setTag(holder);
        }
        else{
            holder = (ViewHolder) row.getTag();
        }

        if (imageLoader == null){
            imageLoader = AppController.getInstance().getImageLoader();
        }

        final News news = newsList.get(position);
        customBanglaFont = Typeface.createFromAsset(viewGroup.getContext().getAssets(), "fonts/SolaimanLipi.ttf");
        holder.newsTitleView.setTypeface(customBanglaFont);
        holder.newsTitleView.setText(news.getTitle());
        holder.thumbNail.setImageUrl(news.getFeaturedImgSrc(), imageLoader);

        return row;
    }
}

/**
 * Custom View Holder Class
 * @author Tonmoy
 *
 */
class ViewHolder{

    TextView newsTitleView;
    NetworkImageView thumbNail;

    public ViewHolder(View v) {
        // TODO Auto-generated constructor stub
        newsTitleView = (TextView) v.findViewById(R.id.news_title);
        thumbNail = (NetworkImageView) v.findViewById(R.id.news_image);
    }

}

2条回答
Emotional °昔
2楼-- · 2019-09-17 09:35

Why don't you use the Header functionality already built into ListView for this specific thing.

The docs: http://developer.android.com/reference/android/widget/ListView.html#addHeaderView%28android.view.View,%20java.lang.Object,%20boolean%29

or a SO question: Using ListView : How to add a header view?

查看更多
闹够了就滚
3楼-- · 2019-09-17 09:46

your attempting is wrong. Your way you will be able to inflate just one layout. In fact, after the method returns the first time, convertView is not null. To fix quickly you could override getViewTypeCount and getItemViewType. This way you will get convertViews eqaul to the return value of getViewTypeCount, or you could just add the first item as header view for the ListView

查看更多
登录 后发表回答