Misha Akopov
Misha Akopov

Reputation: 13057

expandable listview android change layout on click

I have android project where I have to change layout of last exxpanded group (which will collapse) and the group which is clicked. onGroupClick method i have :

   int cnt=list.getChildCount();                    
                //set layout to all groups (for closed group)
                for(int i = 0; i < cnt; i`enter code here`++){
                    View group = adapter.getGroupView(i, false,
                            null, list);
                       LinearLayout childLayout = (LinearLayout) group.findViewById(R.id.newsLayout);                          childLayout.setBackgroundResource(R.layout.group_layout_shape);              
                }
                // set layout of clicked group
                LinearLayout groupLayout = (LinearLayout) v.findViewById(R.id.newsLayout);
                groupLayout.setBackgroundResource(R.layout.group_layout_opened_shape);`enter code here`

it changes layout of clicked group(which will expand), but doesn't change layout of group, that colapses :( can ayone help me

Upvotes: 3

Views: 10160

Answers (1)

You should not use this method because it is used only for the pictures, colors, layouts to be used inflater, which has a very interesting method

inflate(R.layout.childLayout, ParentView, true);

where you put first the layer you want to inflate, the second - a parent that will contain child and the third - a boolean variable must be true to inflate held.

Example in Activity:

public class MainActivity extends ActionBarActivity {

LayoutInflater inflater;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
    RelativeLayout parentView= (RelativeLayout) findViewById(R.id.mainLayout);
    View v = (View) inflater.inflate(R.layout.child_layout, parentView, true);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

Upvotes: 0

Related Questions