【问题标题】:Custom First Row ListView with LazyAdapter使用 LazyAdapter 自定义第一行 ListView
【发布时间】:2014-04-14 23:45:27
【问题描述】:

我有一个 ListView 显示带有图像的新闻,并设置何时 (position == 0) 更改布局,然后为以下新闻设置一个普通的 listview_row,但这仅适用于屏幕上显示的 3 个项目,然后是位置返回 0 并再次更改视图,这里有什么帮助吗?

这是我的lazyAdapter代码,

        vi = convertView;

        Log.d("NOTICIAS", "P0sition: " + position);
        // set Layout for 1rst item
        if (convertView==null && position == 0) {
            vi = inflater.inflate(R.layout.noticias_list_item_first, null);     
        }
        // set layout for the next items
        else if(convertView==null  && position != 0){
            vi = inflater.inflate(R.layout.noticias_list_item, null);
        }

        TextView news_id = (TextView)vi.findViewById(R.id.news_id); // news_id
        TextView news_titulo = (TextView)vi.findViewById(R.id.news_titulo); // news_titulo
        TextView news_desc = (TextView)vi.findViewById(R.id.news_desc); // news_desc
        //TextView news_fecha = (TextView)vi.findViewById(R.id.news_fecha); // news_fecha
        ImageView news_img = (ImageView)vi.findViewById(R.id.news_img); // news_img

        HashMap<String, String> news = new HashMap<String, String>();
        news = data.get(position);

        // Setting all values in listview
        news_id.setText(news.get(NoticiasActivity.TAG_NEWS_ID));
        news_titulo.setText(news.get(NoticiasActivity.TAG_NEWS_TITULO));
        news_desc.setText(news.get(NoticiasActivity.TAG_NEWS_DESC));
        //news_fecha.setText(song.get(NoticiasActivity.TAG_NEWS_FECHA));
        imageLoader.DisplayImage(news.get(NoticiasActivity.TAG_NEWS_IMG), news_img);

            return vi;

【问题讨论】:

    标签: android listview android-lazyadapter


    【解决方案1】:

    ListView 出于性能目的回收列表项,因此在您的情况下,它尝试对 0,3,6... 索引使用相同的视图。

    您需要 Override getViewTypeCount 并返回 2 因为,您有两种不同的布局。

     @Override
    public int getViewTypeCount() {
        return 2; // return the view type
    }
    

    另外,OverridegetItemViewType 并为 position 0 返回一个唯一类型,并为其他类似的位置返回相同类型。

    @Override
    public int getItemViewType(int position){
        // return a unique number
        if(position==0){
            return 0;
        }
        else {
            return 1;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-11
      • 1970-01-01
      • 2016-07-23
      相关资源
      最近更新 更多