Headertab

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Monday, 1 August 2016

PullTorefresh in android

Hello guys,
Today we are learn about how to add custom  pull to refresh in listview or recyclerview .
so lets start and enjoy


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.samset.user.pulltorefreshsample.MainActivity">

    <com.samset.user.pulltorefreshsample.pullrefresh.PullRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/refresh"
        app:progress_show_circle_backgroud="true"
        app:overlay="false"
        app:progress_backgroud_color="#FFFAFAFA"
        app:progress_colors="@array/material_colors"
        app:wave_height_type="normal"
        app:progress_show_arrow="true"
        >

        <ListView
            android:id="@+id/lv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"></ListView>


    </com.samset.user.pulltorefreshsample.pullrefresh.PullRefreshLayout>
</RelativeLayout>


ActvityMain.java


package com.samset.user.pulltorefreshsample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;

import com.samset.user.pulltorefreshsample.pullrefresh.PullRefreshLayout;
import com.samset.user.pulltorefreshsample.pullrefresh.PullRefreshListener;
import com.samset.user.pulltorefreshsample.pullrefresh.SunLayout;

public class MainActivity extends AppCompatActivity {

    String[] array;
    private PullRefreshLayout materialRefreshLayout;
    SunLayout sunLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sunLayout=new SunLayout(this);


        array = new String[100];
        for (int i = 0; i < array.length; i++) {
            array[i] = "samsetdev" + i;
        }

        final ListView listView = (ListView) findViewById(R.id.lv);
        listView.setAdapter(new android.widget.ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, array));
        materialRefreshLayout = (PullRefreshLayout) findViewById(R.id.refresh);
        materialRefreshLayout.setSunStyle(true);
        materialRefreshLayout.setMaterialRefreshListener(new PullRefreshListener() {
            @Override
            public void onRefresh(final PullRefreshLayout materialRefreshLayout) {
                materialRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        materialRefreshLayout.finishRefresh();
                        array = new String[8];
                        for (int i = 0; i < array.length; i++) {
                            array[i] = "samsetdev.blog" + i;
                        }
                        listView.setAdapter(new android.widget.ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, array));
                    }
                }, 3000);

            }

            @Override
            public void onfinish() {
                Toast.makeText(MainActivity.this, "Update", Toast.LENGTH_LONG).show();
            }


        });

}
}

string.xml

<resources>
    <string name="app_name">PullToRefreshSample</string>

    <integer-array name="material_colors">
        <item>@color/colorPrimary</item>
        <item>@color/colorAccent</item>
        <item>@color/colorPrimaryDark</item>
        <item>@color/material_red</item>
    </integer-array>
</resources>


color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
    <color name="material_red">#ffF44336</color>

</resources>


AndroidMeanifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.samset.user.pulltorefreshsample">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


Thank you

FullSource CodePullToRefresh-master

Live Sample

 






No comments:

Post a Comment