gpt4 book ai didi

android - 如何在 android 中给 ListView 下拉刷新功能

转载 作者:太空狗 更新时间:2023-10-29 12:47:04 25 4
gpt4 key购买 nike

如何在 android 中创建下拉刷新列表?

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pull_to_refresh);

// Set a listener to be invoked when the list should be refreshed.
((PullToRefreshListView) getListView()).setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh() {
// Do work to refresh the list here.
new GetDataTask().execute();
}
});

mListItems = new LinkedList<String>();
mListItems.addAll(Arrays.asList(mStrings));

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, mListItems);

setListAdapter(adapter);
}`

最佳答案

这不是安卓设计模式。然而,this优秀的库让你轻松做到。看看例子。

希望我有所帮助。

编辑 -- 12/06/2015 -- 忽略之前的声明:

现在,Android 上的 SDK 完全支持这种设计模式。

很简单,你需要用到一个SwipeRefreshLayout作为列表的父 View (或您可能想要刷新的其他数据)。您可以将任何 View 作为 subview ,它将为该 View 创建一个下拉刷新动画。

之后,你只需要执行SwipeRefreshLayout.OnRefreshListener处理实际数据刷新的网络代码:

public class MainActivity extends FragmentActivity implements OnRefreshListener {

private SwipeRefreshLayout _pullToRefreshLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);

_pullToRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_layout);
_pullToRefreshLayout.setOnRefreshListener(this);

super.onCreate(savedInstanceState);
}

@Override
public void onRefresh() {
//When this is called, your view has a little loader showing to show the user that a network call is in progress
Log.i("SO17065814", "Starting refresh...");

new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mSwipeRefreshLayout.setRefreshing(false); //This stops the refresh animation
Log.i("SO17065814", "Ending refresh...");
}
}, 5000);
}

关于android - 如何在 android 中给 ListView 下拉刷新功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17065681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com