各位大牛,你们做过平板电脑,左右滑动切换界面的吗?
不知你们怎么做的,说说你们的方法啊,
我自己最近也在做, 我自己自定义了一个继承于ViewGroup的组件,然后想让让作为父View,再循环的加载多个子View,
最后发现老是报“The specified child already has a parent. You must call removeView() on the child's parent first.”这样的错,如下代码:
sLayout = (ScrollLayout)findViewById(R.id.ScrollLayoutTest);
for (int i = 0; i < 4; i++) {
if(foodLayout1 == null){
food = new Food();
Log.e("p->>", "i=="+i);
foodLayout1 = LayoutInflater.from(this).inflate(R.layout.food_layout_1, null);
food.foodImage = (ImageView)foodLayout1.findViewById(R.id.food_layout_1_image);
food.foodIntroduce =(TextView)foodLayout1.findViewById(R.id.food_layout_1_introdu);
food.foodName = (TextView)foodLayout1.findViewById(R.id.food_layout_1_name);
food.foodType = (TextView)foodLayout1.findViewById(R.id.food_layout_1_foottype);
food.foodPrice = (TextView)foodLayout1.findViewById(R.id.food_layout_1_price);
foodLayout1.setTag(food);
}else{
food = (Food)foodLayout1.getTag();
}
food.foodImage.setImageResource(R.drawable.mainfoot2);
food.foodIntroduce.setText("");
food.foodName.setText(FOOD_NAMES[i]);
food.foodType.setText("大");
food.foodPrice.setText(FOOD_INTRODUCE[i]);
sLayout.addView(foodLayout1);
}
我在sLayout.addView(foodLayout1)前加removeView()的话最后只有一个view,却达不到我的目的,纠缠了很久啊,往高手指点指点
不知你们怎么做的,说说你们的方法啊,
我自己最近也在做, 我自己自定义了一个继承于ViewGroup的组件,然后想让让作为父View,再循环的加载多个子View,
最后发现老是报“The specified child already has a parent. You must call removeView() on the child's parent first.”这样的错,如下代码:
sLayout = (ScrollLayout)findViewById(R.id.ScrollLayoutTest);
for (int i = 0; i < 4; i++) {
if(foodLayout1 == null){
food = new Food();
Log.e("p->>", "i=="+i);
foodLayout1 = LayoutInflater.from(this).inflate(R.layout.food_layout_1, null);
food.foodImage = (ImageView)foodLayout1.findViewById(R.id.food_layout_1_image);
food.foodIntroduce =(TextView)foodLayout1.findViewById(R.id.food_layout_1_introdu);
food.foodName = (TextView)foodLayout1.findViewById(R.id.food_layout_1_name);
food.foodType = (TextView)foodLayout1.findViewById(R.id.food_layout_1_foottype);
food.foodPrice = (TextView)foodLayout1.findViewById(R.id.food_layout_1_price);
foodLayout1.setTag(food);
}else{
food = (Food)foodLayout1.getTag();
}
food.foodImage.setImageResource(R.drawable.mainfoot2);
food.foodIntroduce.setText("");
food.foodName.setText(FOOD_NAMES[i]);
food.foodType.setText("大");
food.foodPrice.setText(FOOD_INTRODUCE[i]);
sLayout.addView(foodLayout1);
}
我在sLayout.addView(foodLayout1)前加removeView()的话最后只有一个view,却达不到我的目的,纠缠了很久啊,往高手指点指点
上xml文件,在xml文件里定义了父layout吗?
你关掉重用就好了. 其实也没必要在这里重用, 开销不很大,毕竟才4个组件.