Android不同的行布局仅适用于BaseAdapter中的第一行

时间:2014-09-08 12:51:55

标签: java android android-listview baseadapter layout-inflater

我正在使用自定义适配器扩展 BaseAdapter 来制作自定义列表视图。现在,我想为第一行设置不同的布局,为所有其他行设置另一个布局。我正在使用此代码,但它为第1行设置特殊布局,并在每5/6行后重复它。我该如何修复它,只能为第一行设置它,为其他所有行设置另一个布局。

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 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

你的尝试是错的。按照您的方式,您只需一个布局即可充气。实际上,在方法第一次返回convertView is not null之后。要快速修复,您可以覆盖getViewTypeCountgetItemViewType。通过这种方式,您可以convertView获得getViewTypeCount的返回值,或者您可以将第一项添加为ListView的标题视图