0

I am using a normal ListView (mItemsList) with expandable animation (from this tutorial). It works, when I click on list item it expands and shows details for this item.

 mItemsList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {

            View details = view.findViewById(R.id.details);

            // Creating the expand animation for the item
            ExpandAnimation expandAni = new ExpandAnimation(details, 500);

            // Start the animation on the toolbar
            details.startAnimation(expandAni);
        }
    });

I created a showDetails button and I want to expand all list items after clicking the button, but I am completely lost. Code below doesn't work

  mShowDetailsButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            for (int i = 0; i < mItemsList.getAdapter().getCount(); i++) {
                View details= v.findViewById(R.id.details);
                details.startAnimation(new ExpandAnimation(details, 500));
            }
        }
    });

Could you help me?

Here's my list_item xml file

Anna
  • 17
  • 6

1 Answers1

0

Maybe do the similar approach like the following to automatically click item one by one to open all listciew.

mShowDetailsButton.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        for (int i = 0; i < mItemsList.getAdapter().getCount(); i++) {
            listView.performItemClick(
                getViewByPosition(i),
                i,
                listView.getAdapter().getItemId(i));            
        }
    }
});
//the listview getChildAt only return the view(item) that is visible, 
//therefore add a function to get invisible view together
public View getViewByPosition(int position) {
    int firstItemPosition = listView.getFirstVisiblePosition();
    int lastItemPosition = firstItemPosition + listView.getChildCount() - 1;

    if (position < firstItemPosition || position > lastItemPosition ) {//is invisible
        return listView.getAdapter().getView(position, null, listView);
    } else {
        int childIndex = position - firstItemPosition;//is visible
        return listView.getChildAt(childIndex);
    }
}

The one you do is not work as the view is refering the mShowDetailsButton ,but not mItemsList. Thus, you cannot findViewof id R.id.details.

mShowDetailsButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {//<-- this view not referring to mItemsList 
    }
});
CbL
  • 734
  • 5
  • 22
  • Thanks! I tried this, but I get NullPointerException – Anna Feb 25 '17 at 10:36
  • may you post the error log so we can figure out which line is problem – CbL Feb 25 '17 at 11:10
  • Process: pm.anna.takecare, PID: 17940 java.lang.NullPointerException at pm.anna.takecare.CareActivity$11.onClick(CareActivity.java:276) at android.view.View.performClick(View.java:4496) at android.view.View$PerformClick.run(View.java:18603) ... etc here's my [activity file](https://github.com/anna-wro/takecare/blob/master/app/src/main/java/pm/anna/takecare/CareActivity.java#L276) – Anna Feb 25 '17 at 11:42
  • @Anna I checked the problem is that the getChildAt only return visible view, so there is chance get null obj returned. I tried to run at a test app, should be alright now. FYR: [link](http://stackoverflow.com/questions/18463101/get-listview-children-that-are-not-in-view) – CbL Feb 25 '17 at 12:54