在我的Android应用的一个特定页面中,根据用户选项,显示gridview或listview。当在应用程序中激活滑动时,无论显示什么视图,我都想执行一些任意操作。
我尝试了各种配置,通常会导致一个列表无法正常显示或列表无法滚动或响应触摸事件。
xml布局如下所示:
@Override
public void onResume() {
super.onResume();
SharedPreferences settings = getActivity().getApplicationContext().getSharedPreferences("settings", 0);
boolean layout = settings.getBoolean("Layout", false);
if (layout == true) {
listview.setVisibility(View.VISIBLE);
gridview.setVisibility(View.GONE);
} else {
listview.setVisibility(View.GONE);
gridview.setVisibility(View.VISIBLE);
}
}
我使用此逻辑显示页面
swipeRefreshLayout = (SwipeRefreshLayout)list_view.findViewById(R.id.list_swipe_refresh_layout);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
if (!Updater.updateInProgress) {
Toast.makeText(getActivity().getApplicationContext(), "Update started...", Toast.LENGTH_LONG).show();
Updater.updateAll();
swipeRefreshLayout.setRefreshing(false);
} else {
Toast.makeText(getActivity().getApplicationContext(), "An update is already in progress.",Toast.LENGTH_LONG).show();
}
}
});
我像这样配置SwipeRefreshLayout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid_swipe_refresh_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<GridView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dip"
android:numColumns="auto_fit"
android:verticalSpacing="1dp"
android:horizontalSpacing="1dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:id="@+id/gridview"/>
</android.support.v4.widget.SwipeRefreshLayout>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_swipe_refresh_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:visibility="gone"
android:id="@+id/listview"/>
</android.support.v4.widget.SwipeRefreshLayout>
使用此特定配置,如果应用程序配置为显示列表,则不会显示任何内容,但如果我在xml文件中切换GridView和ListView的顺序,则listview将正确显示,但不会显示为GridView。
如何将SwipeRefreshLayout附加到多个列表/网格视图?
编辑:要回复其中一条评论,当我尝试在FrameLayout中放置两个SwipeRefreshLayouts时,定义的第一个列表/网格视图将无法响应滑动和点击。这是我尝试过的布局:
/* for screens 480px or larger */
@media screen and (min-width: 480px) {
#header-icon-div {
display:initial; /* you may want this to be block or inline-block */
}
#footer-icon-div {
display:none;
}
}
/* For screens less than 480px wide */
@media screen and (max-width: 480px) {
#header-icon-div {
display:none;
}
#footer-icon-div {
display:initial; /* you may want this to be block or inline-block */
}
}
在上面的情况下,gridview对触摸事件完全没有反应。如果我将列表视图换成了gridview,那么listview就会没有响应。
答案 0 :(得分:0)
这是因为SwipeRefreshLayout只能容纳一个scrollabel视图。添加两个SwipeRefreshLayout