Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
475 views
in Technique[技术] by (71.8m points)

android - Having a problem removing TextViews from a LinearLayout programmatically

I am programmatically adding TextViews to a LinearLayout, and deleting them on touch. It all works fine except when the last TextView is touched it doesn't get removed. If I do anything else on the screen like get rid of the keyboard or scroll down at all, the last TextView will be deleted, which makes me think it's a refresh problem, but I have no idea how to solve that.

Here's some of the code I'm using:

final TextView tv1 = new TextView(this);
tv1.setText("Test");

tv1.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        linearlayout1.removeView(tv1);

    }
});

I have also added this code in to try to solve the problem but it didn't change anything:

if (linearlayout1.getChildCount() == 1) {
    linearlayout1.removeAllViewsInLayout();
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This sounds more of a bug in Android, but one thing you could try is hiding your TextView before removal:

tv1.setVisibility(View.GONE)

Or alternatively you could add:

linearlayout1.invalidate()

after removal of the last item to trigger redrawing.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...