重用意见Android的列表视图有2种不同的布局 [英] Reusing views in Android Listview with 2 different layouts

查看:104
本文介绍了重用意见Android的列表视图有2种不同的布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经了解到,以最大限度地提高效率,Android的列表视图需要,以适应在屏幕上,你只能有尽可能多的虚增行的看法。一旦视图已经离开屏幕,你应该重新使用它在你的 getView 的方法,检查是否 convertView 为空或不是。

但是,你怎么能当你需要2个不同的布局列表实现这个想法?可以说,它的订单列表和1布局的完成订单和其他的布局是在处理订单。

这是我的code使用的是观念的一个实例教程。就我而言,我有2个排的布局: R.layout.listview_item_product_complete R.layout.listview_item_product_inprocess

 公开查看getView(INT位置,查看convertView,ViewGroup中父){

ViewHolder支架=无效;

如果(convertView == NULL){
    持有人=新ViewHolder();
    如果(getItemViewType(位置)== COMPLETE_TYPE_INDEX){
        convertView = mInflator.inflate(R.layout.listview_item_product_complete,NULL);
        holder.mNameTextView =(TextView中)convertView.findViewById(R.list.text_complete);
        holder.mImgImageView =(ImageView的)convertView.findViewById(R.list.img_complete);
    }
    其他{//必须INPROCESS_TYPE_INDEX
        convertView = mInflator.inflate(R.layout.listview_item_product_inprocess,NULL);
        holder.mNameTextView =(TextView中)convertView.findViewById(R.list.text_inprocess);
        holder.mImgImageView =(ImageView的)convertView.findViewById(R.list.img_inprocess);
    }
    convertView.setTag(保持器);
} 其他 {
    支架=(ViewHolder)convertView.getTag();
}
    thisOrder =(订单)myOrders.getOrderList()得到(位置)。
    //如果使用不同的视图为每个类型,使用if语句来测试的类型,比如上面的
    holder.mNameTextView.setText(thisOrder.getNameValue());
    holder.mImgImageView.setImageResource(thisOrder.getIconValue());
    返回convertView;
}

公共静态类ViewHolder {
    公众的TextView mNameTextView;
    公共ImageView的mImgImageView;
}
 

解决方案

您需要让适配器的看法回收知道,有一个以上的布局,以及如何在两个为每行之间进行区分。简单地重写这些方法:

  @覆盖
公众诠释getItemViewType(INT位置){
    //定义一个方法来确定要使用的布局,在这里它只是唇上和赔率。
    返回位置%2;
}

@覆盖
公众诠释getViewTypeCount(){
    返回2; //计数不同布局
}
 


一体化 getItemViewType() getView(),像这样的:

 如果(convertView == NULL){
    //你可以移动这一行到你的构造,充气服务将不会改变。
    mInflater =(LayoutInflater)mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
    如果(getItemViewType(位置)== 0)
        convertView = mInflater.inflate(R.layout.listview_item_product_complete,NULL);
    其他
        convertView = mInflater.inflate(R.layout.listview_item_product_inprocess,NULL);
    //等等,等等...
 

关注Android的罗曼盖伊讨论的观点回收在谷歌举行了会谈。

I've learned that to maximize efficiency with Android listviews, you should only have as many inflated 'row' views as are needed to fit on the screen. Once a view has moved off the screen, you should reuse it in your getView method, checking if convertView is null or not.

However, how can you implement this idea when you need 2 different layouts for the list? Lets say its a list of orders and 1 layout is for completed orders and the other layout is for in process orders.

This is an example tutorial of the idea my code is using. In my case, I would have 2 row layouts: R.layout.listview_item_product_complete and R.layout.listview_item_product_inprocess

public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder = null;

if (convertView == null) {
    holder = new ViewHolder();
    if(getItemViewType(position) == COMPLETE_TYPE_INDEX) {
        convertView = mInflator.inflate(R.layout.listview_item_product_complete, null);
        holder.mNameTextView = (TextView) convertView.findViewById(R.list.text_complete);
        holder.mImgImageView = (ImageView) convertView.findViewById(R.list.img_complete);
    }
    else { // must be INPROCESS_TYPE_INDEX
        convertView = mInflator.inflate(R.layout.listview_item_product_inprocess, null);
        holder.mNameTextView = (TextView) convertView.findViewById(R.list.text_inprocess);
        holder.mImgImageView = (ImageView) convertView.findViewById(R.list.img_inprocess);
    }
    convertView.setTag(holder);
} else {
    holder = (ViewHolder) convertView.getTag();
}
    thisOrder = (Order) myOrders.getOrderList().get(position);
    // If using different views for each type, use an if statement to test for type, like above
    holder.mNameTextView.setText(thisOrder.getNameValue());
    holder.mImgImageView.setImageResource(thisOrder.getIconValue());
    return convertView;
}

public static class ViewHolder {
    public TextView mNameTextView;
    public ImageView mImgImageView;
}

解决方案

You need to let the adapter's view recycler know that there is more than one layout and how to distinguish between the two for each row. Simply override these methods:

@Override
public int getItemViewType(int position) {
    // Define a way to determine which layout to use, here it's just evens and odds.
    return position % 2;
}

@Override
public int getViewTypeCount() {
    return 2; // Count of different layouts
}


Incorporate getItemViewType() inside getView(), like this:

if (convertView == null) {
    // You can move this line into your constructor, the inflater service won't change.
    mInflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
    if(getItemViewType(position) == 0)
        convertView = mInflater.inflate(R.layout.listview_item_product_complete, null);
    else
        convertView = mInflater.inflate(R.layout.listview_item_product_inprocess, null);
    // etc, etc...

Watch Android's Romain Guy discuss the view recycler at Google Talks.

这篇关于重用意见Android的列表视图有2种不同的布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆