【问题标题】:When is populated the listview associated to a ListFragment何时填充与 ListFragment 关联的列表视图
【发布时间】:2013-03-26 18:30:27
【问题描述】:

我在 ListFragment 中有这段代码:

TextView tv = (TextView) this.getListView().getChildAt(0);
tv.setBackgroundColor(getResources().getColor(R.color.White));
tv.setTextColor(getResources().getColor(R.color.OtherColor));

我想在ActivityCreated上写这段代码,但是tv是空的。

如果我在 onListItemClick 中编写这段代码,它会完美运行。

我想要的不可能吗?

代码:

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    String[] values = new String[] { getResources().getString(R.string.menu_partida),
              getResources().getString(R.string.menu_acciones),
              getResources().getString(R.string.menu_resultado) };
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_menu, values);
    setListAdapter(adapter);
}

【问题讨论】:

    标签: android android-listview android-listfragment


    【解决方案1】:

    视图仅在所有“创建”回调执行后才绘制,此事件没有回调。但是你可以 post() 一个 Runnable 到 UI Handler 并实现同样的事情:

    getListView().post(new Runnable() {
        public void run() {
            TextView tv = (TextView) this.getListView().getChildAt(0);
            tv.setBackgroundColor(getResources().getColor(R.color.White));
            tv.setTextColor(getResources().getColor(R.color.OtherColor));
        }
    }
    

    但实际上,此更改不会达到您的预期,因为行布局在 ListView 中被回收。 (您将在滚动时看到消除行为。)您应该创建一个自定义适配器来更改 position 0 处的行的布局仅限。由于您只有三行,您可能不会使用 Runnable,但最好的解决方案是扩展 Adapter。

    【讨论】:

    • 谢谢山姆。我想我必须找到另一种方法来做到这一点
    猜你喜欢
    • 1970-01-01
    • 2014-01-05
    • 1970-01-01
    • 1970-01-01
    • 2012-07-14
    • 1970-01-01
    • 2015-05-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多