Android 一个ListView用于两个布局,第二个布局不更新

Android 一个ListView用于两个布局,第二个布局不更新,android,listview,layout,user-interface,adapter,Android,Listview,Layout,User Interface,Adapter,我有一个主要活动,它使用框架布局,屏幕分为左侧导航和右侧内容面板。左侧面板用于导航,将不同的版面加载到右侧面板(称为contentLayout)中,右侧面板可以将多个版面粘在一起。我在左侧导航面板中有一个ListView,其中有onItemClickListeners。在它们各自的onItemClick方法中,我使用contentLayout.removeallview()然后像这样膨胀它们的布局: private final OnItemClickListener redListener =

我有一个主要活动,它使用
框架布局
,屏幕分为左侧导航和右侧内容面板。左侧面板用于导航,将不同的版面加载到右侧面板(称为
contentLayout
)中,右侧面板可以将多个版面粘在一起。我在左侧导航面板中有一个
ListView
,其中有
onItemClickListener
s。在它们各自的
onItemClick
方法中,我使用
contentLayout.removeallview()
然后像这样膨胀它们的布局:

private final OnItemClickListener redListener = new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        contentLayout.removeAllViews();
        if (myFirstClassLayout == null) {
            myFirstClassLayout = FirstClassLayout.getLayout(
                                layoutInflater, 
                                MyActivity.this, 
                                resources, 
                                new ArrayList<String>(),
                                new ArrayList<Desc>()
                            );
        }
        CommonClass.updateListeners();
        if (FirstClassLayout != null) contentLayout.addView(myFirstClassLayout);
    }
};

private final OnItemClickListener blueListener = new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        contentLayout.removeAllViews();
        if (mySecondClassLayout== null) {
            mySecondClassLayout = SecondClassLayout.getLayout(
                                layoutInflater, 
                                MyActivity.this, 
                                resources, 
                                new ArrayList<String>(),
                                textList,
                                new ArrayList<Desc>(),
                                hash
                            );
        }
        CommonClass.updateListeners();
        if (SecondClassLayout != null) contentLayout.addView(mySecondClassLayout);
    }
};
由于这是从最后一个布局触发到第一个布局触发的,我想这一定与UI刷新问题有关,但我到处都在寻找一个好的解决方案,并尝试了所有建议。对于触发的第一个或最后一个布局,
Log.i
语句在
LogCat
中正确打印,因此我知道它正确地命中了代码。我只是不明白为什么第一个不能控制最后一个。我试过:

 commonListView.invalidate();
 ((BaseAdapter) commonListView.getAdapter()).notifyDataSetChanged(); 

AsyncTask
以及其他一些布局,但是似乎没有任何东西能够刷新第一个布局,使其与第二个布局匹配。有没有人遇到过这样的问题?有人知道如何解决这个问题吗?

看看添加视图后contentLayout.requestLayout()是否能解决您的问题。

我找到了导致第二个视图取代第一个视图的原因。我继承了很多这段代码,所以我不熟悉所有的部分。涉及其他几件物品。我最终找到了代码的一部分,在该部分中,原始作者使用了一个实现自定义侦听器的自定义AdapterRapper。ThirdClassLayout只创建了一次新的ArrayAdapter和一次新的AdapterRapper,方法是检查静态ArrayAdapter是否为null。删除此检查允许为两个布局创建新的适配器对象。创建了它们自己的侦听器后,两个布局现在都会响应。

我只是在相同的if块中的contentLayout.addView之后添加了它。在if区外也试过了。第二个布局仍然存在同样的问题,没有刷新。
 commonListView.invalidate();
 ((BaseAdapter) commonListView.getAdapter()).notifyDataSetChanged();