编程语言
首页 > 编程语言> > java – 添加视图后无法刷新布局(仅在CustomAdapter中发生)

java – 添加视图后无法刷新布局(仅在CustomAdapter中发生)

作者:互联网

注意:这不应该与库有任何关系,我只是包含它们以获取详细信息

问题:这个库https://github.com/Diolor/Swipecards中有一个名为FlingContainer的布局.它需要另一个布局作为参数(卡片要被剔除),在卡片内部我有一个按钮来向卡片添加标签.在用于Fling Container的CustomAdapter中,我在GetView方法中使用了此代码.我知道它有效,因为我可以看到电视在调试模式下被添加,它们只是没有出现.

我尝试了什么:我尝试在没有flingContainer的活动中执行此操作,并立即添加视图,没有任何问题.我尝试从mainActivity搜索R.id.addTag然而我得到一个nullPointer异常,我认为这是因为addTag嵌入在另一个布局中

结论:知道这里出了什么问题吗?我怎样才能让addView在卡中工作?

谢谢

编辑:这是整个getView

    public View getView(int position, View convertView, final ViewGroup parent) {
    final View vi = inflater.inflate(layoutResource, parent, false);
    TextView tv = (TextView) vi.findViewById(R.id.card_one_line);
    flowLayout = (FlowLayout) vi.findViewById(R.id.flow_container);

    final TextView typeTag = new TextView(getContext());
    final TextView typeTag2 = new TextView(getContext());
    TextView addTag = (TextView) vi.findViewById(R.id.addTag);


    typeTag.setText(lines.get(position).getType());

    typeTag.setBackgroundColor(getContext().getResources().getColor(R.color.nice_blue));
    typeTag.setTextColor(getContext().getResources().getColor(R.color.lightening_yellow));
    typeTag.setTextSize(25);
    typeTag.setPadding(5, 5, 5, 5);


    flowLayout.addView(typeTag);


    addTag.setOnClickListener(new View.OnClickListener() {
        int i = 0;
        @Override
        public void onClick(View v) {
            TextView tv = new TextView(getContext());
            tv.setBackgroundColor(getContext().getResources().getColor(R.color.nice_blue));
            tv.setTextColor(getContext().getResources().getColor(R.color.lightening_yellow));
            String text = "Goofy"+i++;
            tv.setText(text);
            tv.setLayoutParams(new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));
            flowLayout.addView(tv);
            parent.invalidate();
            parent.requestLayout();
            Toast.makeText(getContext(), "heyheyhey", Toast.LENGTH_SHORT).show();

        }
    });


    tv.setText((lines.get(position).getLine()));
    return vi;
}

在这张图片中,我正在点击flingcontainer中的按钮,g是在onClick之外添加的默认类型,试图让onclick在这里工作

解决方法:

的LayoutParams

无论何时以编程方式创建View,都不要忘记.只需插入WRAP_CONTENT,生活就好

也用

Parent.invalidate();
Parent.requestLayout();

这里的父级是您要添加的ViewGroup

编辑

做一些调整addTag.setOnClickListener()是一个不依赖于你的getView()方法的方法,所以硬编码引用并不是很好

 addTag.setClickable(true);
 addTag.setOnClickListener(new View.OnClickListener() {
    int i = 0;
    @Override
    public void onClick(View v) {
        TextView tv = new TextView(getContext());
        tv.setBackgroundColor(getContext().getResources().getColor(R.color.nice_blue));
        tv.setTextColor(getContext().getResources().getColor(R.color.lightening_yellow));
        String text = "Goofy"+i++;
        tv.setText(text);
        tv.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        ((ViewGroup)v.getParent()).addView(tv)
        parent.invalidate();// do you know this "parent" guy?
        //i feel it is suppose to be flowlayout
        parent.requestLayout();
        Toast.makeText(getContext(), "heyheyhey", Toast.LENGTH_SHORT).show();

    }
});

标签:java,android,flowlayout
来源: https://codeday.me/bug/20190528/1169049.html