简体   繁体   English

如何从ListFragment显示/隐藏Activity视图

[英]How to show/hide Activity views from a ListFragment

I have the classic ListActivity - ListFragment design with the ListFragment fetching data via a cursor Loader. 我有经典的ListActivity - ListFragment设计,其中ListFragment通过游标加载程序获取数据。

There are 2 possible outcomes in the basic arrangement - 基本安排有两种可能的结果-

the cursor is either empty or has data. 光标为空或有数据。

With data the list_row.xml is populated with items from the database. 对于数据, list_row.xml中填充了数据库中的项目。

If empty, I get a blank screen with no information for the user. 如果为空,我将得到一个黑屏,没有用户信息。

I have setup a TextView <i>"No records found message"</i> that can be be made Visible or Invisible from the ListFragment. 我已经设置了TextView <i>"No records found message"</i> ,可以将其从ListFragment设为可见或不可见。 The problem is: 问题是:

  1. ListFragment cannot access Views on the ListActivity. ListFragment无法访问ListActivity上的视图。
  2. How do I call the following ListActivity method from the ListFragment? 如何从ListFragment调用以下ListActivity方法?
public void updateView(boolean data) {

        if (data) {         
             noRecordsFound.setVisibility(View.GONE);       
        } else {            
             noRecordsFound.setVisibility(View.VISIBLE);
        }
    }

I have tried to access from the onLoadFinished of the ListFragment , but this is not accessble. 我试图从ListFragmentonLoadFinished访问,但这不是可访问的。 Any help? 有什么帮助吗?

Thanks to MH for the alternative approach, but I have bumped onto an answer to my own question. 感谢MH提供了另一种方法,但是我遇到了自己的问题的答案。 While I think MH's answer is the most suitable for my case, I include a solution to what I originally wanted to do just in case someone needs to use it for things other than listviews. 虽然我认为MH的答案最适合我的情况,但我提供了一个本来可以做的解决方案,以防万一有人需要将其用于除列表视图之外的其他事情。

Well, I included the if (cursor.getCount() < 1) block inside the onLoadFinished() of the Cursor Loader, kike so: 好吧,我在游标加载程序的onLoadFinished()中包含了if (cursor.getCount() < 1)块,就像这样:

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {

    if (cursor.getCount() < 1) {

        TextView noNotes    = (TextView) getActivity().findViewById(R.id.noNotes);
        noNotes.setVisibility(View.GONE);
    }
    adapter.swapCursor(cursor);
}

As per earlier comment: 根据之前的评论:

I don't really understand your first issue, since the view should be part of your fragment's layout, not the activity's, right? 我不太了解您的第一个问题,因为视图应该是片段布局的一部分,而不是活动的一部分,对吧?

With respect to toggling the 'empty' message: why don't you simply use the built-in functionality of setEmptyView(...) of ListView or setEmptyText(...) of ListFragment (also available in the support libary)? 对于切换“空”的消息:你为什么不干脆用的内置功能setEmptyView(...)ListViewsetEmptyText(...)ListFragment (在支持libary也可)? The first allows you to display a more complex view hierarchy for the empty message, in case you need to 第一个允许您为空消息显示更复杂的视图层次结构,以防您需要

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

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