Android different Row layout only for first row in BaseAdapter

对着背影说爱祢 提交于 2019-12-11 22:15:06

问题


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

}

回答1:


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?




回答2:


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



来源:https://stackoverflow.com/questions/25724730/android-different-row-layout-only-for-first-row-in-baseadapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!