我正在尝试在具有id容器的LinearLayout中填充R.layout。正如我在Android Studio的“布局检查器”中看到的那样,布局是膨胀的,但在应用运行时未在模拟器中显示。
单击按钮会发生充气。该按钮位于cardView内部。所有卡都在适配器内部生成。我正在发布适配器的getView方法。
@Override
public View getView(final int position,View convertView, final ViewGroup parent) {
LayoutInflater inflater = null;
if (convertView == null) {
inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.card_draft, parent, false);
}
Button show = convertView.findViewById(R.id.button);
final LinearLayout container = convertView.findViewById(R.id.container);
final LayoutInflater infl = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
infl.inflate(R.layout.card_text, container,true);
v.setVisibility(View.GONE);
TextView text = (TextView) ((SwipeStack) parent).getTopView().findViewById(R.id.text);
text.setText("dsfsfsf");
}
});
return convertView;
}
如果我移动此行
infl.inflate(R.layout.card_text, container,true);
在OnClickListener之外(在主函数的主体中),它工作正常。
我正在使用此库 https://github.com/flschweiger/SwipeStack
和此适配器
public class SwipeStackAdapter extends ArrayAdapter<Item>{
private List<Item> mData;
private Context ctx;
public SwipeStackAdapter(Context ctx,List<Item> data) {
super(ctx, R.layout.card,data);
this.ctx = ctx;
this.mData = data;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Word getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position,View convertView, final ViewGroup parent) {
LayoutInflater inflater = null;
if (convertView == null) {
inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.card_draft, parent, false);
}
Button show = convertView.findViewById(R.id.button);
final LinearLayout container = convertView.findViewById(R.id.container);
final LayoutInflater infl = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
infl.inflate(R.layout.card_text, container,true);
v.setVisibility(View.GONE);
TextView text = (TextView) ((SwipeStack) parent).getTopView().findViewById(R.id.text);
text.setText("dsfsfsf");
}
});
return convertView;
}
}
为什么会这样? 有什么建议吗?
答案 0 :(得分:0)
您需要将新视图附加到现有视图。我看不到您将膨胀视图添加到另一个视图中。如果要将膨胀视图添加到LinearLayout中,则需要执行以下操作:
LinearLayout containerLy = findViewById(R.id.container);
View inflatedView = getLayoutInflater().inflate(R.layout.my_layout_to_be_inflated, null);
//manipulate here your inflated view
containerLy.add(inflatedView);