To add Swipe Refresh we need to add android SwipeRefreshLayout widget under
activity_main.xml
file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context="com.example.sagorsarker.addingswiprefresh.MainActivity"> | |
<!--refresh layout--> | |
<android.support.v4.widget.SwipeRefreshLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/swiperefresh" | |
android:layout_width="368dp" | |
android:layout_height="495dp" | |
tools:layout_editor_absoluteY="8dp" | |
tools:layout_editor_absoluteX="8dp"> | |
<!-- <ListView | |
android:id="@android:id/list" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" />--> | |
<TextView | |
android:id="@+id/text_edit" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_weight="1" | |
android:text="My Text" /> | |
</android.support.v4.widget.SwipeRefreshLayout> | |
</LinearLayout> |
Inside
SwipeRefreshLayout
we need to put our refresh widget such as ListView,TextViewThen we need to add menu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<menu xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:android="http://schemas.android.com/apk/res/android"> | |
<item | |
android:id="@+id/menu_refresh" | |
android:title="@string/menu_refresh" | |
app:showAsAction="never" | |
/> | |
</menu> |
Now we need to add some code to our
MainActivity.java
classFirst refer our SwipeRefreshLayout
SwipeRefreshLayout SRL;
inside
onCreate()
Method we need to add this simple code to hadle swiperefresh onClick listener
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SRL = (SwipeRefreshLayout) findViewById(R.id.swiperefresh); | |
SRL.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener(){ | |
@Override | |
public void onRefresh() { | |
Log.i(TAG, "onRefresh called from SwipeRefreshLayout"); | |
// This method performs the actual data-refresh operation. | |
// The method calls setRefreshing(false) when it's finished. | |
myUpdateOperation(); | |
} | |
}); |
myUpdateOperation()
is a method to handle our refresh thing and calls setRefreshing(false)
when it’s finished We need to add same code under
OnOptionsItemSelected
method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case R.id.menu_refresh: | |
Log.i(TAG, "Refresh menu item selected"); | |
// Signal SwipeRefreshLayout to start the progress indicator | |
SRL.setRefreshing(true); | |
// Start the refresh background task. | |
// This method calls setRefreshing(false) when it's finished. | |
myUpdateOperation(); | |
return true; |
No comments:
Post a Comment