How can I make a cell in a ListView in Android expand and contract vertically when it's touched?

前端 未结 6 1086
别那么骄傲
别那么骄傲 2020-12-01 08:20

I have a cell in a ListView that has a bunch of text in it. I show the first two rows of text and then end it with a \"...\" if it goes beyond. I want a user to be able to t

6条回答
  •  遥遥无期
    2020-12-01 09:10

    What you want to do is have a flag that knows if the row is expanded or not. If it is, then run an animation that shrinks it, and vice versa.

    public void onListItemClicked(int position)
    {
        View v = listView.getView(position);
        if(expanded[position])
          v.startAnimation(shrinkAnimation);
        else
          v.startAnimation(growAnimation);  
        expanded[position] = !expanded[position];
    }
    

    Simple.

    However if what you are doing is something similar to the screen shot you implemented, I would advise against doing this with a ListView. The views are each different enough to warrant just doing this manually with LinearLayouts. Only use the ListView if the number of rows is unknown or very large.

提交回复
热议问题