Does anyone know how to code it to update the information from the Internet in my app with pulling down the screen? Like this
Thanks for answering!
Does anyone know how to code it to update the information from the Internet in my app with pulling down the screen? Like this
Thanks for answering!
Here is example implemantation :
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/container_items"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="match_parent"
android:background="@color/white"
>
<ListView
android:id="@+id/list_recent"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</android.support.v4.widget.SwipeRefreshLayout>
Then in your class on create :
mItemsContainer = (SwipeRefreshLayout) findViewById(R.id.container_items);
mItemsContainer.setOnRefreshListener(this);
then some where in class :
@Override
public void onRefresh() {
// after refresh code
}
Use SwipeRefreshLayout like as
step1: make xml file
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="@string/hello_world"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"/>
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
step2: in your activity do this
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
swipeLayout.setOnRefreshListener(this);
swipeLayout.setColorScheme(android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
}
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override public void run() {
swipeLayout.setRefreshing(false);
}
}, 5000);
}
Step 1: paste the code in your layout file
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:smoothScrollbar="true"
android:id="@+id/mListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
</android.support.v4.widget.SwipeRefreshLayout>
Step 2: Open your java file and add these lines
public class MainActivity extends Activity implements SwipeRefreshLayout.OnRefreshListener
private SwipeRefreshLayout swipeRefreshLayout;
public void onCreate(Bundle savedStateInstances){
....
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
swipeRefreshLayout.setOnRefreshListener(this);
}
@Override
public void onRefresh() {
//implement the logic you want to do when the pull down happen.
//after completion of your logic add this line.
swipeRefreshLayout.setRefreshing(false);
}