SwipeRefreshLayout 不再起作用,如果我更改 activity,然后再回来
SwipeRefreshLayout does not work anymore, if I change activity, and then come back
这是我的 xml:
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshLayout"
android:layout_below="@id/id_bar"
android:layout_marginBottom="0.5dp"
android:background="@color/blue"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/list"
android:dividerHeight="0dp"
android:layout_gravity="top"
android:divider="@null"
android:background="@color/yellow"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<com.bartoszlipinski.recyclerviewheader2.RecyclerViewHeader
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="70dp"
android:visibility="gone"
android:layout_gravity="center_horizontal|top">
<include
android:id="@+id/header_profile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/rounded_top_green"
layout="@layout/header_profile"/>
</com.bartoszlipinski.recyclerviewheader2.RecyclerViewHeader>
<RelativeLayout
android:id="@+id/placeholderContainer"
android:layout_width="match_parent"
android:visibility="gone"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:layout_height="match_parent">
<ImageView
android:id="@+id/placeholderPic"
android:layout_alignParentTop="true"
android:layout_marginTop="100dp"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/pin_newtrips"/>
<TextView
android:id="@+id/placeholderText"
android:layout_below="@id/placeholderPic"
android:padding="10dp"
android:textSize="28dp"
android:textColor="@color/gray32"
android:text="@string/home.status.heading.setup"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RelativeLayout
android:id="@+id/placeholderAddItems"
android:layout_marginTop="10dp"
android:layout_below="@id/placeholderText"
android:layout_width="match_parent"
android:layout_height="wrap_content"></RelativeLayout>
</RelativeLayout>
</FrameLayout>
</android.support.v4.widget.SwipeRefreshLayout>
我通过 butterknife 启动它:
@InjectView(R.id.swipeRefreshLayout) SwipeRefreshLayout mSwipeRefreshLayout;
这是我的 refreshListener:
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
// Refresh items
if(timeLineAdapter!= null)
timeLineAdapter.setLastAnimatedPosition(0);
refresh(false);
}
});
我在 onResume 上调用它,这样当我返回它时它就会起作用。
现在最初它可以工作,如果我按下主页按钮然后回来,它就可以工作。
如果我将 activity 更改为另一个,然后再返回,它就不再起作用了。如何解决?
编辑:如您所见,SwipeRefreshLayout 的主要子项是一个 FrameLayout,它包含 RecyclerView、我的 RecyclerViewHeader 和列表的占位符(在这里,因为在占位符上向下滑动也应该滑动刷新。
我在这 2 个视图之间添加了一个 ScrollView,现在滑动刷新有效。但是,在 scrollview 中有 recyclerview 会破坏我的 recyclerView。我可以向下滚动但不能向上滚动。还有其他想法吗?
当我检查列表中是否有要显示的对象时,代码中有 timelineList.setVisibility(View.Invisible)。
我删除了它并改为设置它:
if(timeLineAdapter != null) {
timeLineAdapter.clearItems();
timeLineAdapter.notifyDataSetChanged();
}
现在列表没有设置为无处不可见。
我在 SwipeRefreshLayout 中添加了我的占位符和列表,以为它对两者都有效,但实际上它只对列表有效,所以如果列表消失了,刷新的拉动就消失了
这是我的 xml:
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshLayout"
android:layout_below="@id/id_bar"
android:layout_marginBottom="0.5dp"
android:background="@color/blue"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/list"
android:dividerHeight="0dp"
android:layout_gravity="top"
android:divider="@null"
android:background="@color/yellow"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<com.bartoszlipinski.recyclerviewheader2.RecyclerViewHeader
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="70dp"
android:visibility="gone"
android:layout_gravity="center_horizontal|top">
<include
android:id="@+id/header_profile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/rounded_top_green"
layout="@layout/header_profile"/>
</com.bartoszlipinski.recyclerviewheader2.RecyclerViewHeader>
<RelativeLayout
android:id="@+id/placeholderContainer"
android:layout_width="match_parent"
android:visibility="gone"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:layout_height="match_parent">
<ImageView
android:id="@+id/placeholderPic"
android:layout_alignParentTop="true"
android:layout_marginTop="100dp"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/pin_newtrips"/>
<TextView
android:id="@+id/placeholderText"
android:layout_below="@id/placeholderPic"
android:padding="10dp"
android:textSize="28dp"
android:textColor="@color/gray32"
android:text="@string/home.status.heading.setup"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RelativeLayout
android:id="@+id/placeholderAddItems"
android:layout_marginTop="10dp"
android:layout_below="@id/placeholderText"
android:layout_width="match_parent"
android:layout_height="wrap_content"></RelativeLayout>
</RelativeLayout>
</FrameLayout>
</android.support.v4.widget.SwipeRefreshLayout>
我通过 butterknife 启动它:
@InjectView(R.id.swipeRefreshLayout) SwipeRefreshLayout mSwipeRefreshLayout;
这是我的 refreshListener:
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
// Refresh items
if(timeLineAdapter!= null)
timeLineAdapter.setLastAnimatedPosition(0);
refresh(false);
}
});
我在 onResume 上调用它,这样当我返回它时它就会起作用。 现在最初它可以工作,如果我按下主页按钮然后回来,它就可以工作。 如果我将 activity 更改为另一个,然后再返回,它就不再起作用了。如何解决?
编辑:如您所见,SwipeRefreshLayout 的主要子项是一个 FrameLayout,它包含 RecyclerView、我的 RecyclerViewHeader 和列表的占位符(在这里,因为在占位符上向下滑动也应该滑动刷新。 我在这 2 个视图之间添加了一个 ScrollView,现在滑动刷新有效。但是,在 scrollview 中有 recyclerview 会破坏我的 recyclerView。我可以向下滚动但不能向上滚动。还有其他想法吗?
当我检查列表中是否有要显示的对象时,代码中有 timelineList.setVisibility(View.Invisible)。 我删除了它并改为设置它:
if(timeLineAdapter != null) {
timeLineAdapter.clearItems();
timeLineAdapter.notifyDataSetChanged();
}
现在列表没有设置为无处不可见。 我在 SwipeRefreshLayout 中添加了我的占位符和列表,以为它对两者都有效,但实际上它只对列表有效,所以如果列表消失了,刷新的拉动就消失了