به روزرسانی ListView با استفاده از SwipeRefreshLayout در اندروید

چهارشنبه 30 دی 1394

در این مقاله می خواهیم برای به روز رسانی یک لیست ویو ی مورد نظر از یک swiprefreshlayout استفاده نماییم که زمانی که کاربر لیست را ببیند لیست به روز رسانی می شود، برای استفاده از این ابزار باید از کتابخانه ی android.support.v4 استفاده شود.

به روزرسانی ListView با استفاده از SwipeRefreshLayout در اندروید

برای نمایش این ابزار ما از کلاس swichrefreshlayout.onrefreshListener زمانی  از آن استفاده می نماییم که interface مورد نظر پیاده سازی می شود.

متد isrefreshing: این متد progress را نمایش می دهد.

setcolorschemacolor: رنگ progress را مشخص می نماید در موقع refresh شدن.

setrefreshing:این متد برای زمانی است که وقتی موقعیت را تغییر می دهید، این متد فراخوانی خواهد شد.

داخل activity قطعه کد زیر را قرار دهید:

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
 
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
 
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
 
            <TextView
                android:id="@+id/infoText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:gravity="center"
                android:text="Pull Down to Refresh" />
 
            <ListView
                android:id="@+id/listView"
                android:layout_width="match_parent"
                android:layout_height="424dp"
                android:layout_marginTop="10dp" >
            </ListView>
        </LinearLayout>
 
    </ScrollView>
 
</android.support.v4.widget.SwipeRefreshLayout>

داخل listview مورد نظر باید یک textview برای نمایش خود لیست ها نمایش دهید

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:textColor="@android:color/black" />

حالا نوبت به خود کلاس است و استفاده از ArrayListAdapter برای نمایش لیست ها باید از یک آرایه ی رشته ای استفاده نمایید.

زمانی که ما لیست را پایین می کشیم یک علامت refresh می آید و پیامی را به handler برای به روز رسانی لیست ارسال می نماید.بعد از به روز رسانی adapter انیمیشن false می شود.

بعد از refresh شدن برنامه متد OnRefresh صدا زده می شود.و در آخر کد MainActivity به صورت زیر خواهد بود:

package com.barnamenevisan.swip;
import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnRefreshListener {
	private SwipeRefreshLayout swipeView;
	private ListView listView;
	private ArrayAdapter<String> adapter;
	private String[] LIST_ITEM_TEXT_CITIES = { "ایران", "آلمان",
			"آمریکا", "سان فرانسیسکو", "لندن", "وین" };
	private String[] LIST_ITEM_TEXT_MORE_CITIES = { "هند", "پاکستان",
			"مراکش", "دبی", "ارمنستان", "ایتالیا" };
	private List<String> cityList;
	// variable to toggle city values on refresh
	boolean refreshToggle = true;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		swipeView = (SwipeRefreshLayout) findViewById(R.id.swipe_view);
		swipeView.setOnRefreshListener(this);
		swipeView.setColorSchemeColors(Color.GRAY, Color.GREEN, Color.BLUE,
				Color.RED, Color.CYAN);
		swipeView.setDistanceToTriggerSync(20);// in dips
		swipeView.setSize(SwipeRefreshLayout.DEFAULT);// LARGE also can be used

		cityList = Arrays.asList(LIST_ITEM_TEXT_CITIES);
		listView = (ListView) findViewById(R.id.listView);
		adapter = new ArrayAdapter<String>(getApplicationContext(),
				R.layout.list_item, cityList);
		listView.setAdapter(adapter);
		listView.requestLayout();
	}
	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (refreshToggle) {
				refreshToggle = false;
				cityList = Arrays.asList(LIST_ITEM_TEXT_MORE_CITIES);
				adapter = new ArrayAdapter<String>(getApplicationContext(),
						R.layout.list_item, cityList);
			} else {
				refreshToggle = true;
				cityList = Arrays.asList(LIST_ITEM_TEXT_CITIES);
				adapter = new ArrayAdapter<String>(getApplicationContext(),
						R.layout.list_item, cityList);
			}
			listView.setAdapter(adapter);

			swipeView.postDelayed(new Runnable() {

				@Override
				public void run() {
					Toast.makeText(getApplicationContext(),
							"city list refreshed", Toast.LENGTH_SHORT).show();
					swipeView.setRefreshing(false);
				}
			}, 1000);
		};
	};
	@Override
	public void onRefresh() {

		swipeView.postDelayed(new Runnable() {

			@Override
			public void run() {
				swipeView.setRefreshing(true);
				handler.sendEmptyMessage(0);
			}
		}, 1000);
	}
}

در آخر خروجی به صورت زیر خواهد بود:

فایل های ضمیمه

برنامه نویسان

نویسنده 3355 مقاله در برنامه نویسان

کاربرانی که از نویسنده این مقاله تشکر کرده اند

در صورتی که در رابطه با این مقاله سوالی دارید، در تاپیک های انجمن مطرح کنید