高级下拉刷新将帮助您在应用中提供modern approach。
实现起来非常smooth and easy。它还将支持较低版本(从 v2.3 开始)。下面的屏幕截图会让人想起新的刷新。
界面部分
从您的 Eclipse 或 Android Studio 创建一个 Android。
在您的活动布局中添加 SwipeRefreshLayout。
<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>
注意:布局内只有视图。在 SwipeRefresh 布局内添加 scrollView 以支持拉动刷新。对于 ListView 和 GridView,无需在 SwipeRefreshLayout 中添加 ScrollView。
代码部分
在 Activity 的 onCreate 方法中添加以下行。
公共类 SwipeActivity 扩展 Activity 实现 OnRefreshListener {
SwipeRefreshLayout swipeLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_swipe);
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() {
// TODO Auto-generated method stub
new Handler().postDelayed(new Runnable() {
@Override public void run() {
swipeLayout.setRefreshing(false);
}
}, 5000);
}
}
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);
To stop the loading progress swipeLayout.setRefreshing(false);.
就是这样。运行你的应用程序,你也做了新的拉动来刷新。
使用列表视图滑动刷新
这里我添加了一个使用 listview 拉取刷新的示例。然后你就会知道集成是多么容易。
在您的应用程序中创建一个新的 Activity,在您的 SwipeRefreshLayout 中添加列表视图。
<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" >
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>
为列表视图、适配器、列表项的字符串数组声明和初始化变量。当您拉动刷新时,下面的代码将添加数组中的列表项。
public class SwipeActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_swipe);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.swipe, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment implements OnRefreshListener {
SwipeRefreshLayout swipeLayout;
ListView listView;
ArrayAdapter adapter;
ArrayList< String> arrayList;
String [] array = new String[]{"Apple","Batman","captain America","darkknight"};
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_swipe, container, false);
swipeLayout = (SwipeRefreshLayout) rootView.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);
listView= (ListView) rootView.findViewById(R.id.listview);
adapter = new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1, appendData());
listView.setAdapter(adapter);
return rootView;
}
@Override
public void onRefresh() {
// TODO Auto-generated method stub
new Handler().postDelayed(new Runnable() {
@Override public void run() {
appendData();
adapter.notifyDataSetChanged();
swipeLayout.setRefreshing(false);
}
}, 5000);
}
private ArrayList appendData(){
if(arrayList==null)
arrayList = new ArrayList();
for (String items : array) {
arrayList.add(items);
}
return arrayList;
}
}
}
注意:我使用了 appcompat 库来支持较低版本的操作栏。所以我的活动扩展了 ActionBarActivity。在 eclipse 和 studio 的更新 ADT 版本中,您的应用程序将使用片段概念创建。因此,应用程序是使用即时片段概念创建的。
我猜你喜欢这个例子。自己尝试并练习。
编码愉快:-)。
结果屏幕,看这个:
对于您现有的程序,请使用this 链接