android - 我可以在不使用 ScrollView/ListView 的情况下使用 Pull to Refresh 吗?

标签 android listview android-studio pull-to-refresh

我尝试在我的应用中实现下拉刷新。

我用 android.support.v4.widget.SwipeRefreshLayout 包装了我的 xml 代码,它可以正常工作,但 GUI 看起来不太好。

当我下拉时,我应该看到带有我定义的颜色的常规动画圆圈图标,对吗?但我看到它是一个空白圆圈,而不是一个带有颜色的旋转圆圈。

此外,我无法“玩”它,甚至在我拉动它后保持一会儿,它会下降一秒钟(做它做的)然后消失。

我认为这是因为我没有使用 ListView,而是使用了 LinearLayout。

你怎么看?

代码:

XML:

<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/swipe">

    <LinearLayout
        android:layout_height="match_parent"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:background="#7B68EE"
        android:paddingBottom="@dimen/activity_vertical_margin"
        tools:context=".HomeActivity">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/loin"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="@string/welcome"
            android:textColor="#B0C4DE"
            android:id="@+id/welcomeBackID"
            android:layout_alignParentTop="true"
            android:layout_alignRight="@+id/webViewCurren"
            android:layout_alignEnd="@+id/webViewCurren" />

        ....

 </android.support.v4.widget.SwipeRefreshLayout>

Java:

public class HomeActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {

    SwipeRefreshLayout mSwipeRefreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe);
        mSwipeRefreshLayout.setColorSchemeResources(android.R.color.holo_green_light,
                android.R.color.holo_blue_bright,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
        mSwipeRefreshLayout.setOnRefreshListener(this);

    @Override
    public void onRefresh() {
        Toast.makeText(this, "Refresh", Toast.LENGTH_SHORT).show();
        sharedPrefEditor.putString("coordinateX", "34.7");
        sharedPrefEditor.putString("coordinateY", "32.5");
        sharedPrefEditor.putString("saved", "false");
        sharedPrefEditor.commit();

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mSwipeRefreshLayout.setRefreshing(false);
            }
        }, 3000);
    }
}

最佳答案

尝试更改 SwipeRefreshLayout 配色方案。

    swipeRefreshLayout.setColorSchemeColors(
    Color.RED, Color.GREEN, Color.BLUE, Color.CYAN);

此外,SwipeRefreshLayout 仅在 onRefresh() 完成执行之前可见。

关于android - 我可以在不使用 ScrollView/ListView 的情况下使用 Pull to Refresh 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32156711/

相关文章:

Android循环无限ListView?

android - 不要在自定义 View 中滚动 ListView 被触摸

java - 从 fragment 打开的 Activity 立即关闭

android 如何隐藏状态栏而不隐藏标题栏

Android ListView高亮显示多个项目

android - 无法在 Android Studio 中为 Flutter 项目添加依赖项

java - 密码和确认密码不起作用

android - 如何判断安卓设备是否有屏幕即是否是安卓机顶盒?

android - 如何在android中有视频时自动点击按钮?

java - Android AppUpdateManager 未初始化自动更新