单击时可扩展的Listview图像更改?

时间:2011-09-17 12:01:40

标签: java android

我有以下代码运行完美。当用户点击某个组时,它会因状态的变化而扩大图像更改。 xml文件也在下面。我遇到的问题是,当我点击子元素时,它会启动一个新活动,但就在您看到图像更改状态之前,就好像该组已关闭一样。当您使用后退箭头时,即使该组仍然展开,图像确实也会回到关闭状态。任何人都可以对此有所了解吗?

private static final int[] EMPTY_STATE_SET = {};
private static final int[] GROUP_EXPANDED_STATE_SET = {android.R.attr.state_expanded};
private static final int [] [] GROUP_STATE_SETS = {
    EMPTY_STATE_SET, //0
    GROUP_EXPANDED_STATE_SET //1
};

           Cursor groupCursor = checkDB.rawQuery("SELECT * FROM TABLE", null);
            mscta = new MySimpleCursorTreeAdapter(
                                                    this,
                                                    groupCursor,
                                                    R.layout.listGroup,
                                                    new String[] {"group"},
                                                    new int[] {R.id.group},
                                                    R.layout.listChildren,
                                                    new String[] {"child"},
                                                    new int[] {R.id.Child}) {
                @Override
                public View getGroupView (int groupPosition,
                        boolean isExpanded,
                        View convertView,
                        ViewGroup parent) {

                        View v = super.getGroupView( groupPosition, isExpanded, convertView, parent);
                        View ind = v.findViewById(R.id.explist_indicator);
                        if(ind != null){
                            ImageView indicator = (ImageView) ind;
                            indicator.setVisibility( View.VISIBLE );
                            int stateSetIndex = ( isExpanded ? 1:0);
                            Drawable drawable = indicator.getDrawable();
                            drawable.setState(GROUP_STATE_SETS[stateSetIndex]);
                        }
                        return v;
                    }
            };

XML explist_indicator.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_expanded="true"
        android:drawable="@drawable/minus" />
    <item
        android:drawable="@drawable/plus" />
</selector>

1 个答案:

答案 0 :(得分:0)

您可以通过调用onRestart()来更新列表视图,当您再次进入活动时它将被调用,因此您可以更新列表视图,但您会发现组视图已关闭。