解决使用SwipeRefreshLayout ListView使用EmptyView冲突
来源:互联网 发布:东方卫视网络电视 编辑:程序博客网 时间:2024/06/02 21:51
ListView使用EmptyView的必要条件
listView.setEmptyView(view)
方法中的view必须和listView属于同一个ViewGroup。负责即使添加了也不会起作用。
SwipeRefreshLayout下添加EmptyView,不能实现效果的原因
SwipeRefreshLayout(android.support.v4.widget.SwipeRefreshLayout)
虽然是ViewGroup的子类,但是他只允许有一个子控件,所以在它下面同时添加ListView和EmptyView,EmptyView不能注册到SwipeRefreshLayout。
private View mTarget; // the target of the gesture...private void ensureTarget() { // Don't bother getting the parent height if the parent hasn't been laid // out yet. if (mTarget == null) { for (int i = 0; i < getChildCount(); i++) { View child = getChildAt(i); if (!child.equals(mCircleView)) { mTarget = child; break; } } } }
SwipeRefreshLayout中添加FramLayout包含ListView和EmptyView存在的问题
这个方案能够在ListView为空的时候显示EmptyView,但是影响ListView的下拉(与下拉刷新空间冲突)。
通过代码添加EmptyView的问题
通过
listView.getParent()
可以获取到ListView的父控件,通过它的addView()
可以添加EmptyView。但是当列表为空的时候就不能再下拉刷新了。
完善的解决方法
通过ViewGroup(不能是LinearLayout)下添加两个SwipeRefreshLayout,一个包含ListView,另外一个包含被ScrollView包含的EmptyView。第二个SwipeRefreshLayout作为EmptyView,需要ScrollView是因为需要他来接受下拉事件传递。
代码中,两个SwipeRefreshLayout最好是设置一样的样式和下拉监听。
布局文件和代码如下:
layout.xml
<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="com.tobrun.example.swipetorefresh.MainActivity"> <ImageView android:id="@+id/igHeader" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/transparent"/> <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swipeRefreshLayout_listView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/igProgress"> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="wrap_content" /> </android.support.v4.widget.SwipeRefreshLayout> <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swipeRefreshLayout_emptyView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/igProgress"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/emptyView" android:text="@string/empty" android:layout_width="match_parent" android:layout_height="0dp" android:layout_gravity="center" android:gravity="center" /> </ScrollView> </android.support.v4.widget.SwipeRefreshLayout></RelativeLayout>
activity.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ……此处省去findViewById()以及部分初始化 initSwipeLayout(swipeLayout); initSwipeLayout(swipeLayoutEmpty); lv.setEmptyView(swipeLayoutEmpty); lv.setAdapter(adapter);} private void initSwipeLayout(SwipeRefreshLayout layout) { layout.setColorSchemeResources(R.color.ics_blue_bright, R.color.ics_green_light, R.color.ics_orange_light, R.color.ics_red_light); layout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { reloadData(); } }); } //此处使用的是AndroidAnnotations,意义就是使用了非UI线程 @Background void reloadData() { // getData methods ...... bindData(); } //此处使用的是AndroidAnnotations,意义就是使用了UI线程,同runOnUIThread @UIThread void bindData() { // bindDataMethods ....... swipeLayout.setRefreshing(false); swipeLayoutEmpty.setRefreshing(false); }
0 0
- 解决使用SwipeRefreshLayout ListView使用EmptyView冲突
- 解决SwipeRefreshLayout结合ListView EmptyView使用不起作用的问题
- SwipeRefreshLayout嵌套listview 设置EmptyView 冲突
- ListView+EmptyView与SwipeRefreshLayout滑动冲突问题
- 判断Listview滑到顶部的最精准方案,解决Listview设置EmptyView与SwipeRefreshLayout冲突
- 优雅的解决SwipeRefreshLayout和ListView的EmptyView共存冲突的问题(全网独创)
- ListView RecyclerView使用EmptyView
- SwipeRefreshLayout和ListView的EmptyView共存冲突的问题
- SwipeRefreshLayout和ListView的EmptyView共存冲突的问题
- SwipeRefreshLayout嵌套Listview冲突解决
- SwipeRefreshLayout基本使用和冲突解决机制
- Android之SwipeRefreshLayout使用和冲突解决
- 使用Android Supportv4 SwipeRefreshLayout有关ListView滑动冲突问题
- SwipeRefreshLayout结合ListView使用的时候有时候存在下拉冲突
- 解决listview与SwipeRefreshLayout滑动冲突问题
- SwipeRefreshLayout 与ListView滑动冲突的解决
- ListView与SwipeRefreshLayout滑动冲突解决方式
- 解决listview与SwipeRefreshLayout滑动冲突问题
- 第三周 项目2-三角形类2
- 在VC2008下将32位C++内嵌汇编迁移到64位
- 第三周 项目二 三角形类2
- java排序算法测试及分析
- 第3周项目3-程序的多文件组织
- 解决使用SwipeRefreshLayout ListView使用EmptyView冲突
- 自己动手实现一个队列LGQueue
- Notepad++ 快捷键 大全
- 游戏简单控制逻辑 一个轻量级的状态机
- Objective-C内存管理
- 第三周【项目3-程序的多文件组织】
- 黑马程序员_java常用知识——排序
- int数组快速排序
- weblogic 向Tomcat迁移的问题