4

Note: this shouldn't have anything to do with libraries, I just include them for details

Problem: There is a layout called FlingContainer from this library https://github.com/Diolor/Swipecards. It takes another layout as a parameter(the card to be flinged), Inside the card I have a button to add tags to the card. In the CustomAdapter used for the Fling Container I have this code in the GetView method. I know it works because I can see the TVs getting added in debug mode, they just arent showing up.

What I've tried: I tried doing this in just an activity with no flingContainer and the views are added instantly with no problems. I tried searching for R.id.addTag from the mainActivity however I get 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 get addView to work in the card?

Thanks

EDIT: here is entire 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 am hitting the + button inside the flingcontainer, the g is the default type added outside of the onClick, trying to get onclick to work here

Tintinabulator Zea
  • 2,617
  • 5
  • 18
  • 32

2 Answers2

4

LayoutParams

anytime you create a View programmatically do not forget that. just insert WRAP_CONTENT and life is good

also use

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

the parent here is the ViewGroup you are adding to

EDIT

make a few adjustment addTag.setOnClickListener() is a method which is not dependent on your getView() method,so hardcoding reference is not pretty good do this

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

    }
});
Elltz
  • 10,730
  • 4
  • 31
  • 59
  • Hey i tried your advice and it didnt work, it works when the view is added to a flowlayout in a plain old activity, but not when the flowlayout is inside the fling conatiner. I know the button works because the toast message pops up. I've also added pictures and updated code – Tintinabulator Zea Sep 30 '15 at 02:49
  • also are you sure textView dosent automatically have layoutParams? when i add it in an activity it works without layoutparams – Tintinabulator Zea Sep 30 '15 at 02:56
  • in my initial code i said something about parent and i see parent in your code, so if i may ask Sir, who is "parent" because to me it is suppose to be your `flowlayout` well check my edit and see if that solves it. also layoutparams tells the parent how a child wants to be treated, so if you are using a custom Viewgroup it is required. also instead of using `ViewGroup.Layoutparams` check if that library has `FlowLayout.LayoutParams` if not stick with what you have @TintinabulatorZea – Elltz Sep 30 '15 at 03:16
  • parent is passed into the customAdapter like this --- public View getView(int position, View convertView, final ViewGroup parent) – Tintinabulator Zea Sep 30 '15 at 06:57
  • it still didnt work. Its very interesting that it works in a plain activity. I even tried adding a button outside the FlingContainer that did flingContainer.invalidate() and it didnt work:( I think it is just impossible with this library, i will have to recreate the activity everytime – Tintinabulator Zea Sep 30 '15 at 07:49
0

I met the same issue recently. I've tried a lot of things, and to fix this I called requestLayout() to that child that I just added instead of the parent. In my case, I am adding a child at a certain index. parent.addView(child, index) parent.getChildAt(index).requestLayout()

user3576118
  • 375
  • 1
  • 5
  • 24