简体   繁体   English

单击可扩展列表视图 android 更改布局

[英]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.我有一个 android 项目,我必须在其中更改最后一个扩展组(将折叠)和单击的组的布局。 onGroupClick method i have :我有 onGroupClick 方法:

   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它会更改单击组的布局(将展开),但不会更改组的布局,即崩溃:( 谁能帮我

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你不应该使用这个方法,因为它只用于图片,颜色,布局要使用inflater,它有一个非常有趣的方法

inflate(R.layout.childLayout, ParentView, true);膨胀(R.layout.childLayout,ParentView,真);

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

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM