先道有一個這樣的需求,先不多說,先來一張求意圖吧.圖如下所示

大家看到這樣的需求時,可能首先就會想到,拿兩個咱們最熟悉不過的兩個ListView來實現就可以解決問題了。就是分為兩層,內層與外層。(即嵌套listview的使用)
可是事情有的時候並不是你想的那樣的,是的,android布局上面可以實現,我起初的想法也是以listview的嵌套使用的,可是在實施的過程中,出現了一些問題,今天就一起來分享一下怎么實現,與解決這一類的問題。也當作是給自己的一個項目記錄吧。
為方便,我就將listview分為兩個,一個是listview_in (內層)與listview_out(外層)
外層的listview_out 布局如下所示:
<!-- 此次訂單詳細列表 -->
<ListView
android:id="@+id/list_item"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_below="@id/first_linear"
android:layout_above="@id/bottom"
style="@style/mylistview_style">
</ListView>
其填充條目的View是一個自定義的View----->
order_item_complete_view
/**
* 按照商家進行分組的訂單視圖
* @author wsf
*
*/
public class order_item_complete_view implements IBaseView {
private View myview;
private ShopModel myshopModel;
private Context mycontext;
private DG_ListView mylistview;
private order_cplt_adapter adapter;
private TextView res_name;
private TextView total;
public order_item_complete_view(Context context,ShopModel shopmodel) {
// TODO Auto-generated constructor stub
mycontext=context;
myshopModel=shopmodel;
adapter=new order_cplt_adapter(mycontext, shopmodel.getList_ordered());
InitView();
}
private void InitView() {
// TODO Auto-generated method stub
myview=LayoutInflater.from(mycontext).inflate(R.layout.order_item_complete_view, null);
mylistview=(DG_ListView)myview.findViewById(R.id.list_item);
mylistview.setAdapter(adapter);
res_name=(TextView)myview.findViewById(R.id.res_name);
total=(TextView)myview.findViewById(R.id.total_spend);
res_name.setText(myshopModel.getName());
total.setText(myshopModel.getTotal()+"");
}
@Override
public View getView() {
// TODO Auto-generated method stub
return myview;
}
}
而在這個視圖中,也有一個listview,這個就是我們的重頭戲了,這個listview是我們自定義的DG_ListView
public class DG_ListView extends ListView {
public DG_ListView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public DG_ListView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public DG_ListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
這個listview所使用的條目視圖布局如下所示;
(order_item_complete_view.xml)布局代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" ><!--內層的最外部需要是linearlayout ,如果不是會報錯,比如說是realativelayout,因為其沒有omeasure方法-->
<!-- title -->
<TextView
android:id="@+id/res_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/menu_name" />
<!-- 列表 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<com.doget.dingsong.wekit.DG_ListView
android:id="@+id/list_item"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</com.doget.dingsong.wekit.DG_ListView>
</LinearLayout>
<!-- bottom -->
<RelativeLayout
android:id="@+id/bottom"
android:layout_width="match_parent"
android:layout_height="@dimen/twenty"
android:layout_alignParentBottom="true" >
<TextView
android:id="@+id/total_spend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="@string/total" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
內部listview的適配器 order_cplt_adapter
public class order_cplt_adapter extends baseMyAdapter<OrderItemModel> {
public order_cplt_adapter(Context context,List<OrderItemModel> lists)
{
mycontext=context;
mylist=lists;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder vh=null;
OrderItemModel m=mylist.get(position);
if(convertView==null)
{
convertView=LayoutInflater.from(mycontext).inflate(R.layout.order_item_complete, null);
vh=new ViewHolder();
vh.count_order=(TextView)convertView.findViewById(R.id.count_order);
vh.price_order=(TextView)convertView.findViewById(R.id.price_order);
vh.food_name_order=(TextView)convertView.findViewById(R.id.food_name_order);
vh.icon_order=(DG_ImageView)convertView.findViewById(R.id.icon_order);
convertView.setTag(vh);
}
else
{
vh=(ViewHolder)convertView.getTag();
}
vh.icon_order.setImage(m.getImgUrl());
vh.food_name_order.setText(m.getOrder_name());
vh.price_order.setText(m.getPrice()+"");
vh.count_order.setText(m.getOrder_count()+"");
vh.icon_order.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(mycontext, "點擊我了哦!!!", 1000).show();
}
});
return convertView;
}
class ViewHolder
{
DG_ImageView icon_order;
TextView food_name_order;
TextView price_order;
TextView count_order;
}
}
一些數據結構,大家可以自己組織一下.以上就是內部的listview已經完成.
那么,實現嵌套的話,外部調用內部視圖,最重要的是改寫外部的適配器.
外部的適配器如下所示:
order_from_adapter
public class order_from_adapter extends baseMyAdapter<ShopModel> {
public order_from_adapter(Context context,List<ShopModel> lists)
{
mycontext=context;
mylist=lists;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
order_item_complete_view v=null;
if(convertView==null)
{
v=new order_item_complete_view(mycontext, mylist.get(position));
convertView=v.getView();
convertView.setTag(v.getView());
}
else
{
v=new order_item_complete_view(mycontext, mylist.get(position));
convertView=v.getView();
}
return convertView;
}
}
兩個listview實現的交互,調用方法。
public class order_form_act extends AbActivity {
OrderFormModel m=new OrderFormModel();
order_from_adapter adapter;
order_cplt_adapter adapter_2;
ListView list_item;
//用戶相關信息
TextView username;
TextView Address;
TextView Credit_card;
TextView coupon;
Button btn_order_his;
Button btn_check_sheet;
public order_form_act() {
// TODO Auto-generated constructor stub
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setAbContentView(R.layout.order_form_act);
getTitleBar().setVisibility(View.GONE);
list_item=(ListView)findViewById(R.id.list_item);
username=(TextView)findViewById(R.id.username);
Address=(TextView)findViewById(R.id.Address);
Credit_card=(TextView)findViewById(R.id.Credit_card);
coupon=(TextView)findViewById(R.id.coupon);
initData();
}
public void initData()
{
List<ShopModel> shops=new ArrayList<ShopModel>();
for(int i=0;i<4;i++)
{
List<OrderItemModel> ods=new ArrayList<OrderItemModel>();
for(int j=0;j<5;j++)
{
OrderItemModel od=new OrderItemModel(j+"", "order--->"+j, i+"", j, "2013-11-12 11:35 PM",3,getString(R.string.img_src_1));
ods.add(od);
}
ShopModel sp=new ShopModel(i, i+"", getString(R.string.img_src_1), "東方餃子", ods);
shops.add(sp);
}
m=new OrderFormModel("wsf", "望京西街融科橄欖城西區", 12.3f, 2, 3, "2013-11-7 12:33", shops, 4f);
adapter=new order_from_adapter(order_form_act.this,shops);
adapter_2=new order_cplt_adapter(order_form_act.this, m.getShop_ordered().get(0).getList_ordered());
list_item.setAdapter(adapter);
username.setText(m.getCustomer_name());
Address.setText(m.getAddress());
Credit_card.setText(m.getCreditcard()+"");
coupon.setText(m.getCoupon_count()+"");
}
最終實現的效果:
這樣的話,就不會說現出只顯示一部份的情況了。代碼不好分離,所以,只把重要的地方標紅了,以及一些主要的類.
