gpt4 book ai didi

android - 如何使用 SwipeRefreshLayout?

转载 作者:IT老高 更新时间:2023-10-28 23:17:35 41 4
gpt4 key购买 nike

背景

Google 最近发布了 an update to its support library ,现在有一个新的“SwipeRefreshLayout” View 。

View 允许包装另一个 View ,同时支持向下滑动以执行刷新操作。

截图:

enter image description here

问题

Google 还没有提供样本(至少我还没有找到),所以我自己尝试使用它。

一开始我每次刷卡都会崩溃 (NPE),但后来我发现那是因为我没有为它提供“OnRefreshListener”。

但是我还是不知道怎么用,更别说自定义了

这是布局文件的 XML:

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.swiperefreshlayouttest.MainActivity"
tools:ignore="MergeRootFrame" >

<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TTT"
android:textSize="40sp" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TTT"
android:textSize="40sp" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TTT"
android:textSize="40sp" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TTT"
android:textSize="40sp" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TTT"
android:textSize="40sp" />
</LinearLayout>
</ScrollView>

</android.support.v4.widget.SwipeRefreshLayout>

代码,虽然它根本没有做任何特别的事情:

public class MainActivity extends ActionBarActivity
{
@Override
protected void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SwipeRefreshLayout swipeRefreshLayout=(SwipeRefreshLayout)findViewById(R.id.container);
swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener()
{
@Override
public void onRefresh()
{
// do nothing
}
});
}
}

问题

使用此 View 的正确方法是什么?

如何自定义它?目前它只是一条黑线......

最佳答案

我不知道您要扩展的 ActionBarActivity 类是什么,但我使用 FragmentActivity 让它工作得很好

public class ActivityMain extends FragmentActivity implements OnRefreshListener {

private SwipeRefreshLayout mSwipeRefreshLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.container);
mSwipeRefreshLayout.setOnRefreshListener(this);

super.onCreate(savedInstanceState);
}

@Override
public void onRefresh() {
Toast.makeText(this, "Refresh", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mSwipeRefreshLayout.setRefreshing(false);
}
}, 2000);
}
}

值得指出的是,我完全按照原样复制粘贴了您的 xml 布局

在自定义方面,除了调用 setColorScheme(int colorResId, int colorResId, int colorResId, int colorResId); 来改变彩条的颜色之外,你真的没什么可做的了

例如

<?xml version="1.0" encoding="utf-8"?>
<resources>

<color name="blue">#0099CC</color>
<color name="purple">#9933CC</color>
<color name="green">#669900</color>
<color name="orange">#FF8800</color>

</resources>

mSwipeRefreshLayout.setColorScheme(R.color.blue, R.color.purple, R.color.green, R.color.orange);

这确实是一个令人失望的补充。刷新的灵敏度相当高,没有设置可以改变它

编辑

我在这个类(和 ActionBarActivity 类)刚刚添加到 sdk 时写了这个。因此,与我写此答案时相比,有些事情发生了变化。此外,您使用的 Activity 类型不应影响此解决方案。

setColorScheme 现在已弃用,应改为使用 setColorSchemeResources(int... colorResIds)。 (您可以在其中放置任意数量的颜色 ID)。

setDistanceToTriggerSync(int distance) 还可用于设置用户需要向下滑动多远才能触发刷新。

我建议查看 official documentation看看这门课还能提供什么。

关于android - 如何使用 SwipeRefreshLayout?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23014846/

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