BaseAdapter的RelativeLayout addRule效果不佳

问题描述 投票:0回答:1

将规则之一添加到RelativeLayout后,得到的结果不是很正确。 ID不适用于元素/被错误地应用。这会影响必须绑定到小部件的规则。

作为容器,我使用ListView并通过BaseAdapter向其添加对象。显然,这不会产生期望的结果,但是在重用返回到适配器并重用的相同小部件之后(保证元素不会再次更改),规则开始正确运行。

@Override
protected void onCreate(Bundle state) {
    super.onCreate(state);
    ListView view = new ListView(this);
    view.setBackgroundColor(Color.BLACK);
    view.setId("files_list".hashCode());
    view.setAdapter(new TestAdapter(this));
...
private class TestAdapter extends BaseAdapter {
...
    @Override
    public View getView(int position, View convertView, View parent) {
        if(convertView == null) convertView = inflateView();
        // only changes text & pictures, doesn't affecting display
        manipulateItem(position, convertView);
        return convertView;
...
private View inflateView() {
    RelativeLayout layout = new RelativeLayout(context);
    layout.setLayoutParams(new ViewGroup.LayoutParams(-1, -2));

    ImageView icon = new ImageView(context);
    icon.setBackgroundColor(Color.BLACK);
    icon.setPadding(20, 20, 20, 20);
    icon.setId("file_icon".hashCode());

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(110, -1);
    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    params.rightMargin = 30;
    layout.addView(icon, params);

    ...

    LinearLayout additional = new LinearLayout(context);
    additional.setOrientation(LinearLayout.VERTICAL);
    additional.setGravity(Gravity.RIGHT);
    additional.setBackgroundColor(Color.RED);
    additional.setPadding(30, 0, 30, 0);
    additional.setId(java.lang.String("additional_info").hashCode());

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(-2, -2);
    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    params.addRule(RelativeLayout.CENTER_IN_PARENT);
    layout.addView(additional, params);

    ...

    LinearLayout uniqal = new LinearLayout(context);
    uniqal.setOrientation(LinearLayout.VERTICAL);
    uniqal.setBackgroundColor(Color.BLUE);
    uniqal.setId("uniqal_info".hashCode());

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(-2, -2);
    // problem occurs here
    params.addRule(RelativeLayout.LEFT_OF, additional.getId());
    params.addRule(RelativeLayout.RIGHT_OF, icon.getId());
    params.addRule(RelativeLayout.CENTER_IN_PARENT);
    layout.addView(uniqal, params);

    ...

    return layout;
}

这是第一次滚动后的小部件外观:elements after first drawing screen

因此,在滚动并重新使用相同的小部件之后:working screen

首先经过5次使用(很多东西进入了我的测试屏幕),一切都变好了。第一次,视图不希望通过ID以任何方式附加到另一个窗口小部件。有没有解决的办法?

从应用程序.xml扩展布局的选项不适合我

java android android-listview android-relativelayout
1个回答
0
投票

手动更新版面对我有帮助,但是这无法立即生效,我的问题仍然很重要。这里是修改后的inflateView代码:

private View inflateView() {
    ...

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(-2, -2);
    params.addRule(RelativeLayout.LEFT_OF, additional.getId());
    params.addRule(RelativeLayout.RIGHT_OF, icon.getId());
    params.addRule(RelativeLayout.CENTER_IN_PARENT);
    uniqal.post(new Runnable() {
        public void run() {
            uniqal.requestLayout();
        }
    });
    layout.addView(uniqal, params);

    ...

    return layout;
}
© www.soinside.com 2019 - 2024. All rights reserved.