Android可扩展列表视图:点击更改布局。

3

我有一个Android项目,需要更改最后一个展开的组(将折叠)和被单击的组的布局。在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`

当点击一个组以展开时,它会更改布局,但不会更改折叠的组的布局:( 有人能帮助我吗?

1个回答

0
你不应该使用这个方法,因为它仅用于在inflater中使用的图片、颜色和布局,而inflater有一个非常有趣的方法:
inflate(R.layout.childLayout, ParentView, true);
在这里,你首先放置要膨胀的层,第二个是包含子元素的父元素,第三个是一个布尔变量,必须为true才能膨胀。
在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);
}

}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接