【问题标题】:Android: set empty view to a list viewAndroid:将空视图设置为列表视图
【发布时间】:2013-04-11 13:18:03
【问题描述】:
 <TextView
            android:id="@android:id/empty"
            style="@style/FacebookFont"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="@string/no_result_found"
            android:textColor="@color/white" />

这是空视图的 xml 代码。我将空视图设置如下:

    list.setAdapter(adapter);
    list.setEmptyView(findViewById(android.R.id.empty));

即使在填充列表视图之前,我在列表视图中也有项目,但会显示空视图。 当列表视图包含某些项目时,我不希望显示该空视图。

请帮忙。

【问题讨论】:

    标签: android android-listview empty-list


    【解决方案1】:

    如果您使用ListActivity,则无需手动设置空视图。从您的代码中删除 list.setEmptyView(findViewById(android.R.id.empty));

    如下改变你的布局,如果列表没有项目,它会自动添加EmptyView

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingLeft="8dp"
            android:paddingRight="8dp">
    
        <ListView android:id="@android:id/list"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#00FF00"
                android:layout_weight="1"
                android:drawSelectorOnTop="false"/>
    
         <TextView android:id="@android:id/empty"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#FF0000"
                android:text="No data"/>
    
    </LinearLayout>
    

    【讨论】:

    • 你能告诉我你完整的活动代码和布局文件代码吗???将这些都添加到您的问题中
    • 当然,但是如果你需要对它做一些事情,你如何获得那个 TextView(或任何视图),没有与之关联的真实 ID,你以编程方式遍历布局?跨度>
    • 好的提示,如果您使用 ListFragment,它也可以工作。虽然不需要超大字体:P
    【解决方案2】:

    如果您的MainActivity 没有扩展ListActivity,您必须在设置适配器之前设置空视图。

    试试这个

    list.setEmptyView(findViewById(android.R.id.empty));
    list.setAdapter(adapter);
    

    抱歉回复晚了!

    替代方法:使您的活动从 ListActivity 扩展。并将textview 添加到您的列表布局中,id 为android.R.id.empty

    【讨论】:

      【解决方案3】:

      试试这个

          View view = getLayoutInflater() .inflate(<resource id of the empty view>, null);
          ViewGroup viewGroup= ( ViewGroup)list.getParent();
          viewGroup.addView(view);
          list.setEmptyView(view); 
          list.setAdapter(adapter);
      

      【讨论】:

        【解决方案4】:

        有一些正确的答案,但是,我认为这是我找到的最好的方法:

        View emptyView = getLayoutInflater().inflate(R.layout.empty_list_cart, null);
        addContentView(emptyView, listView.getLayoutParams()); // You're using the same params as listView
        listView.setEmptyView(emptyView);
        listView.setAdapter(adapter);
        

        【讨论】:

          【解决方案5】:

          @dhawalSodhaParmar 所解释的一切都是正确的。但是当您查看答案的方式时会感到困惑。

              <?xml version="1.0" encoding="utf-8"?>
          
          <RelativeLayout
              xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              tools:context=".SampleActivity">
          
              <ListView
                  android:id="@+id/list"
                  android:orientation="vertical"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:paddingBottom="@dimen/activity_vertical_margin"
                  android:paddingLeft="@dimen/activity_horizontal_margin"
                  android:paddingRight="@dimen/activity_horizontal_margin"
                  android:paddingTop="@dimen/activity_vertical_margin"
                  android:divider="@null"
                  android:dividerHeight="0dp" />
          
              <!-- Empty view is only visible when the list has no items. -->
              <TextView
                  android:id="@+id/empty_text_view"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_centerInParent="true"
                  android:text="NO DOCUMENTS AVAILABLE!"
                  android:textColor="#525252"
                  android:textAppearance="?android:textAppearanceMedium"
                  android:visibility="gone"/>
          
          </RelativeLayout>
          

          假设您已经有一个列表视图和适配器支持相同的数据。假设一切正常,任何想要实现这个空列表视图屏幕的人都必须像下面这样开始: 第1步: 使用我在此处使用的列表视图修改现有的 Activity XML 文件。您可以使用线性布局或相对布局,但最好使用相对布局,因为它具有灵活性。和我写的一样的 textView。

          第 2 步: 转到您的 Activity Java 文件,然后在您的 setContentView 之后

          ListView emptyListView = (ListView) findViewById(R.id.list);
          // set your adapter here
          // set your click listener here
          // or whatever else
          emptyListView.setEmptyView(findViewById(R.id.empty_text_view));
          

          就是这样,现在运行,一切就绪!

          【讨论】:

            【解决方案6】:
            // first of all create a global variable of Text view 
            // which is displayed when the list is empty
            
            private TextView mEmptyStateTextView;
                
            //then in onCreate(Bundle savedInstanceState)
            /Setting empty text view
            
            mEmptyStateTextView=(TextView) findViewById(R.id.emptyview);
            
            view.setEmptyView(mEmptyStateTextView);  
            
            //then in onLoadfinished method
            
            mEmptyStateTextView.setText(R.string.no_earthquakes);
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-12-17
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-10-20
              • 2015-10-23
              相关资源
              最近更新 更多