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

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

本文介绍了Android的不同行的布局仅适用于BaseAdapter第一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我作出自定义列表视图自定义适配器延长 BaseAdapter 。现在,我想设置一个不同的布局仅适用于第一行和其他的布局作为所有其他行。我用这code,但它设置在特殊布局对于第1行​​并每隔5/6行后还重复了。我怎样才能解决这个问题,可以设置只适用于第一行和其他布局的所有其他行。

 公共类NewsAdapter延伸BaseAdapter {
    私人上下文的背景下;
    清单<新闻>新闻列表;
    私人字样customBanglaFont;
    私人LayoutInflater吹气;
    ImageLoader的ImageLoader的= AppController.getInstance()getImageLoader()。    公共NewsAdapter(上下文的背景下,列表与LT;新闻>新闻列表){
        this.context =背景;
        this.newsList =新闻列表;
    }    @覆盖
    公众诠释的getCount(){
        返回newsList.size();
    }    @覆盖
    公共对象的getItem(INT为arg0){
        返回newsList.get(为arg0);
    }    @覆盖
    众长getItemId(INT为arg0){
        返回将arg0;
    }    @覆盖
    公共查看getView(INT位置,查看convertView,ViewGroup中的ViewGroup){
        查看排;
        排= convertView;
        ViewHolder支架=无效;        如果(行== NULL){
            如果(位置== 0){
                吹气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                行= inflater.inflate(R.layout.row_single_big_view,ViewGroup中,FALSE);
            }
            其他{
                吹气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                行= inflater.inflate(R.layout.row_single_default,ViewGroup中,FALSE);
            }            持有人=新ViewHolder(行);
            row.setTag(保持器);
        }
        其他{
            支架=(ViewHolder)row.getTag();
        }        如果(ImageLoader的== NULL){
            ImageLoader的= AppController.getInstance()getImageLoader()。
        }        最后新闻新闻= newsList.get(位置);
        customBanglaFont = Typeface.createFromAsset(viewGroup.getContext()getAssets(),字体/ SolaimanLipi.ttf);
        holder.newsTitleView.setTypeface(customBanglaFont);
        holder.newsTitleView.setText(news.getTitle());
        holder.thumbNail.setImageUrl(news.getFeaturedImgSrc(),ImageLoader的);        返回行;
    }
}/ **
 *自定义视图Holder类
 * @author Tonmoy
 *
 * /
类ViewHolder {    TextView的newsTitleView;
    NetworkImageView缩略图;    公共ViewHolder(视图v){
        // TODO自动生成构造函数存根
        newsTitleView =(TextView中)v.findViewById(R.id.news_title);
        缩略图=(NetworkImageView)v.findViewById(R.id.news_image);
    }}


解决方案

你为什么不使用已经内置的L​​istView这个具体的事情的头功能。

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

或SO问题:

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

}
解决方案

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?

这篇关于Android的不同行的布局仅适用于BaseAdapter第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:10