Can I use Pull to Refresh without using ScrollView/ListView?
Asked Answered
A

1

8

I tried to implement Pull to Refresh in my app.

I wrapped my xml code with android.support.v4.widget.SwipeRefreshLayout and it's working, but the GUI doesn't look good.

When I pull down, I should see the regular animated circle icon with my defined colors, right?, but I see it as a blank circle and not a turning one with colors as it should.

Furthermore, I can't "play" with it and even hold it for a while after I pull it, it goes down for one second (do what it does) and then disappears.

I think it's because I'm not using ListView but LinearLayout instead.

What do you think?

Code:

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);
    }
}
Abisia answered 22/8, 2015 at 13:31 Comment(0)
C
0

Try changing the SwipeRefreshLayout color scheme.

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

Also, the SwipeRefreshLayout will be visible only till onRefresh() completes its execution.

Cowgirl answered 22/8, 2015 at 13:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.