- 浏览: 41404 次
- 性别:
- 来自: 济南
文章分类
最新评论
大家看到这样的需求时,可能首先就会想到,拿两个咱们最熟悉不过的两个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()+"");
}
可是事情有的时候并不是你想的那样的,是的,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()+"");
}
发表评论
-
android shap 各种图形自定义
2018-10-19 15:41 677shape标签能定义多少种类型的Drawable? sh ... -
添加屏保解锁功能
2018-10-19 15:43 933[size=x-large]1. 系统设置,执行LockPat ... -
android 动态加载sdcard jar的方法
2016-12-13 17:19 8941.新建Android工程,封装功能java类。 2.选中需要 ... -
android6.0单独编译和调试framework源码的方法
2016-11-29 20:35 0缺省情况下,Android 6.0的编译过程会优化framew ... -
adb 下面sqlite3 操作数据库
2016-06-16 10:52 01. adb 连接上设备进入后 ... -
ViewPaper
2016-03-02 10:13 0ViewPape ... -
ssh 别名登录小技巧
2016-02-22 10:52 0vim ~/.ssh/config Host carte ... -
ssh 免密码登录设置
2016-02-22 10:51 0配置SSH无密码登录需要3步: 1.生成公钥和私钥 ... -
Git 的origin和master分析 push/diff/head
2016-02-16 14:51 0[size=large][size=large] 1、ori ...
相关推荐
listview嵌套listview简单的小案例
Android ListView嵌套ListView 功能和淘宝订单列表类似。
android listview嵌套listview实现折叠列表
利用android studio实现listview嵌套listview,技术含量不是特别高,希望能帮助大家,这里就意思一下,给1分算啦
ListView 嵌套ListView 的一个demo,可以实现二级列表。
Android ScrollView嵌套ListView嵌套GridView的上下拉以及加载更多
android 嵌套的listview示例。就是外面一层listview,这层的item展开又是一个listview。我的博客地址:http://blog.csdn.net/qq_16064871。
仿照qq空间评论,简单实现二级评论,或许对你有用,有bug处 还希望一起交流
android中ListView嵌套GridView的使用
Android ListView嵌套GridView(GridView实现横向滑动)
Android listview viewpager 滑动 跳动 冲突解决 ListView中嵌套ViewPage有或者滑动手势冲突解决 blog 地址 : http://blog.csdn.net/aaawqqq/article/details/43824631
ListView嵌套GridView解决方案
android listView嵌套GridView,在Gridview中最后一个为图标可隐藏部分view
直接两个listView 布局文件,数据直接输入,获得 嵌套ListView 的适配效果
【Android】ListView、RecyclerView、ScrollView里嵌套ListView 相对优雅的解决方案:NestFullListView 博客:http://blog.csdn.net/zxt0601/article/details/52494665
listView 嵌套gridView android ,完全显示gridView的内容,滚动不卡顿
listView嵌套横向listView,参考http://blog.csdn.net/whitley_gong/article/details/48936689 的代码的,只是希望日后自己方便,希望作者不要介意。谢谢
解决scrollView和gridView和ListView嵌套的冲突,动态测量ListView和GridView的高度
里面包含两个demo,DownLoading这个工程是关于listview下拉刷新,动态加载数据 ...然后spinnerDemo1这个工程是spinner与listview的嵌套并用,当点击spinner中的不同选项时,动态刷新listview里的数据
ListView嵌套ListView时出现的问题1.子ListView展示一条(方法,重写ListViewListViewInScrollView)2.子LsitView的出现是由点击事件触发的,故存在问题:父ListView某一Item点击出现了子ListView,然后往下滑动时,...