Failed to update layout after adding view (only appears in CustomAdapter)

Note: this should have nothing to do with libraries, I just include them for details

Problem: There is a flag in this library called FlingContainer https://github.com/Diolor/Swipecards . As a parameter, another layout is required (the map that needs to be deployed). Inside the map, I have a button for adding labels to the map. In the CustomAdapter used for the Fling container, I have this code in the GetView method. I know this works because I see how TVs are added to debug mode, they just don't appear.

What I tried: I tried to do this only in action without flingContainer, and views are instantly added without any problems. I tried to find R.id.addTag from mainActivity, but I got a nullPointer exception, I think this is because addTag is embedded in another layout

Conclusion: Any idea what is going wrong here? How can I get addView to work on the map?

thanks

EDIT: here is the whole 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;
}

In this pic, I click the + button inside the flingcontainer, g is the default type added outside of onClick, trying to get onclick to work here

+4
source share
2 answers

LayoutParams

anytime you create Viewprogrammatically, don't forget about it. just insert WRAP_CONTENTand life is good

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

- ViewGroup,

addTag.setOnClickListener() - , getView(), hardcoding .

 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();

    }
});
+3

,

+3

Source: https://habr.com/ru/post/1608747/


All Articles