您的位置:首页 > 其它

不同布局的Item在listView中显示

2015-10-19 15:12 441 查看
如果是单一布局在ListView中显示,那么在可以直接
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
//执行代码
return view;
}
<span style="font-size:18px;">但是,如果多个布局的Item就很麻烦,因为convertView中存的总是一种布局,当出现第二种布局的时候就会出现cannot cast 第一种布局 to 第二种布局的错误。有一种解决方法是把两种布局都写在一起,通过调整不同布局的visibility来做到显示不同的布局,缺点在于不好维护。</span>
<span style="font-size:18px;">之前的一种错误的做法是加一个try catch,当得到的布局不能cast的时候就重新inflate一下新的布局(T_T)。这当然是不行滴。
下面是正确的解决方法,主要用到了getItemViewType,getViewTypeCount。</span>
<span style="font-size:18px;">首先是接口:</span>
<span style="font-size:18px;"></span><pre name="code" class="java">public abstract class IItemInterface {
protected Context context;
protected BaseFlashModel mdata;
protected int pos;
protected Handler mhandler;

public abstract ItemViewInterface newView(Context context, ViewGroup parent);

protected static ItemViewInterface createCellFromXml(Context context, int layoutID, ViewGroup parent) {
return (ItemViewInterface) LayoutInflater.from(context).inflate(layoutID, parent, false);
}

public abstract Context getContext();
public abstract BaseFlashModel getData();
public abstract Handler getHandler();
public abstract void getInformation(BaseFlashModel data , Handler mhandler);
}
下面是接口的实现:
<pre name="code" class="java">
public class UnitItem extends  IItemInterface {

public  Context mContext;
public String mPat1 = "yyyy-MM-dd";
private String mCurrentTime = "test";
private String mYesterdayTime = "test";
public TextView unitNum;
public TextView unitName;
public View main;
public View contentLayout;

public UnitItem(Context context) {
this.context = context;
}

public UnitItem(Context context,BaseFlashModel data,Handler mHandler) {
this.context = context;
this.mdata = data;
this.mhandler = mHandler;
}

@Override
public Context getContext(){
return this.context;
}
@Override
public BaseFlashModel getData(){
return this.mdata;
}
@Override
public Handler getHandler(){
return this.mhandler;
}

@Override
public ItemViewInterface newView(Context context, ViewGroup parent) {
// TODO Auto-generated method stub
return createCellFromXml(context, R.layout.flash_list_item_unit, parent);
}

@Override
public void getInformation(BaseFlashModel data,Handler mhandler) {
// TODO Auto-generated method stub
this.mdata = data;
this.mhandler = mhandler;
}
}
这是Item类族,其作用是保存数据。
接下来是接口:
<pre name="code" class="java">public interface ItemViewInterface {

void prepareItemView();

void setObject(IItemInterface item , int pos);
<pre name="code" class="java">
}
其实现
public class UnitItemView extends RelativeLayout implements ItemViewInterface {<span style="white-space:pre">	</span>public  Context mContext;<span style="white-space:pre">	</span>public String mPat1 = "yyyy-MM-dd";<span style="white-space:pre">	</span>private String mCurrentTime = "test";<span style="white-space:pre">	</span>private String mYesterdayTime = "test";<span style="white-space:pre">	</span>public TextView unitNum;<span style="white-space:pre">	</span>public TextView unitName;<span style="white-space:pre">	</span>public View main;<span style="white-space:pre">	</span>public View contentLayout;<span style="white-space:pre">	</span>public int position;<span style="white-space:pre">	</span><span style="white-space:pre">	</span>public UnitItemView(Context context) {        this(context, null);    }    public UnitItemView(Context context, AttributeSet attrs) {        this(context, attrs, 0);    }    public UnitItemView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);    }<span style="white-space:pre">	</span>@Override<span style="white-space:pre">	</span>public void prepareItemView() {<span style="white-space:pre">		</span>main = this;<span style="white-space:pre">		</span>unitNum = (TextView) this.findViewById(R.id.unit_num);<span style="white-space:pre">		</span>unitName = (TextView) this.findViewById(R.id.unit_name);<span style="white-space:pre">		</span>contentLayout = this.findViewById(R.id.unit_layout);<span style="white-space:pre">	</span>}<span style="white-space:pre">	</span>@Override<span style="white-space:pre">	</span>public void setObject(IItemInterface item, int pos) {<span style="white-space:pre">		</span>// TODO Auto-generated method stub<span style="white-space:pre">		</span>this.position = pos;<span style="white-space:pre">		</span>bindItemView(item.getContext(), item.getData(), item.getHandler());<span style="white-space:pre">		</span>}<span style="white-space:pre">	</span><span style="white-space:pre">	</span>public void bindItemView(Context activity, BaseFlashModel data, Handler handler) {<span style="white-space:pre">		</span>if ( data == null) {<span style="white-space:pre">			</span>return;<span style="white-space:pre">		</span>}<span style="white-space:pre">		</span>HomeItemInfo info = (HomeItemInfo) data;<span style="white-space:pre">		</span>unitName.setText(info.unit_name);<span style="white-space:pre">		</span>unitNum.setText(info.unit_num);<span style="white-space:pre">		</span>//bindLayout(usedHolder);<span style="white-space:pre">	</span>}<span style="white-space:pre">	</span>
<span style="font-size:18px;"></span>
<span style="font-size:18px;"></span><pre name="code" class="java">

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: