viewpager里不显示listview


代码如下:

   
  public class MainActivity extends Activity {
  

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager vp=(ViewPager) findViewById(R.id.vp);

//viewpager
//初始化views数据
List<View>views=new ArrayList<View>();
LayoutInflater inflater=LayoutInflater.from(this);
views.add(inflater.inflate(R.layout.viewpager_menu, null));
views.add(inflater.inflate(R.layout.viewpager_order, null));

List<String>titles=new ArrayList<String>();
titles.add("菜单界面");
titles.add("订单界面");

EasyEatAdapter adapter=new EasyEatAdapter(views,titles);
vp.setAdapter(adapter);


//listView

View orderView=inflater.inflate(R.layout.viewpager_order,null);
ListView lv=(ListView) orderView.findViewById(R.id.lv_order);
List<Order>data=new ArrayList<Order>();
data.add(new Order(R.drawable.img, "张三1", "18812345678", 3, "18:20", ""));
data.add(new Order(R.drawable.img, "张三2", "18812345678", 3, "18:20", ""));
data.add(new Order(R.drawable.img, "张三3", "18812345678", 3, "18:20", ""));
data.add(new Order(R.drawable.img, "张三4", "18812345678", 3, "18:20", ""));
data.add(new Order(R.drawable.img, "张三5", "18812345678", 3, "18:20", ""));
data.add(new Order(R.drawable.img, "张三6", "18812345678", 3, "18:20", ""));
data.add(new Order(R.drawable.img, "张三7", "18812345678", 3, "18:20", ""));
data.add(new Order(R.drawable.img, "张三8", "18812345678", 3, "18:20", ""));
OrderAdapter adapter2=new OrderAdapter(data);
lv.setAdapter(adapter2);
}


//viewpager
class EasyEatAdapter extends PagerAdapter{

private List<View>views;
private List<String>titles;

public EasyEatAdapter (List<View>views,List<String>titles) {
this.views=views;
this.titles=titles;
}


//得到标题
@Override
public CharSequence getPageTitle(int position) {
return titles.get(position);
}

//得到数量
@Override
public int getCount() {
return views.size();
}

//滑动后页面
@Override
public Object instantiateItem(ViewGroup container, int position) {
View view=views.get(position);
container.addView(view);
return view;
}

//销毁页面
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
View view=views.get(position);
container.removeView(view);

}

@Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
}

}


//listView
class OrderAdapter extends BaseAdapter{
private List<Order>data;

public OrderAdapter(List<Order>data){
this.data=data;
}

//说明listview有多少个条目
@Override
public int getCount() {
return data.size();
}

//得到指定position 的条目
@Override
public Object getItem(int position) {
return data.get(position);
}

//得到条目的ID
@Override
public long getItemId(int position) {
return position;
}



//说明每个条目的布局
//convertView:缓存的条目
//parent:ListView
//返回值作为ListView的一个条目
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Order order=data.get(position);
if(convertView==null){
LayoutInflater inflater=LayoutInflater.from(MainActivity.this);
convertView=inflater.inflate(R.layout.listview_item_order, null);
}

TextView tvName=(TextView) convertView.findViewById(R.id.tv_name);
TextView tvNumber=(TextView) convertView.findViewById(R.id.tv_number);
TextView tvPhone=(TextView) convertView.findViewById(R.id.tv_phone);
TextView tvTime=(TextView) convertView.findViewById(R.id.tv_time);
TextView tvId=(TextView) convertView.findViewById(R.id.tv_id);
TextView tvRemark=(TextView) convertView.findViewById(R.id.tv_remark);
ImageView ivPic=(ImageView) convertView.findViewById(R.id.imageView1);


//设置数据
tvName.setText(order.getName());
tvNumber.setText(String.valueOf(order.getNumber()));
tvPhone.setText(order.getPhone());
tvTime.setText(order.getTime());
tvId.setText(String.valueOf(order.getId()));
tvRemark.setText(order.getRemarkString());
ivPic.setImageResource(order.getId());
return convertView;
}

}
}



相关链接

Android

某科学的空间使 10 years, 1 month ago

Your Answer