如何在 Android 中实现向下滑动刷新
某些应用程序向用户显示实时数据,例如股票价格、在线商店中产品的可用性等。显示实时数据需要应用程序的持续同步,并且可以通过实现诸如线程之类的程序来实现.线程可以由应用程序启动并隐式或显式更新实时信息。这里的交易是以设备的额外 RAM、缓存和电池为代价不断更新数据(可能来自服务器),从而导致性能低下,因为永远运行的线程会占用一些空间并需要电力。为了避免使用此类程序,开发人员明确开发了刷新应用程序的功能,以便用户可以在需要时执行它。这使我们得出结论,手动刷新具有以下优点:
- 内存优化
- 缓存内存优化
- 电池寿命优化
- 避免不必要的回调。
例如在下图中,当用户向下滑动屏幕时,字符串“Swipe to refresh”将更改为“Refreshed”。
方法:
第 1 步:在开始编写代码之前,必须将Swipe Refresh Layout 依赖项添加到应用程序的 build.Gradle 中以启用 swipe 布局。这种依赖是:
implementation “androidx.swiperefreshlayout:swiperefreshlayout:1.1.0”
第 2 步:从前端“ activity_main.xml ”开始很重要。创建一个SwipeRefreshLayout 刷新布局 并添加一个 TextView 以在屏幕上显示字符串并为它们提供某些 ID。
XML
Kotlin
import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import org.w3c.dom.Text
class MainActivity : AppCompatActivity() {
@SuppressLint("SetTextI18n")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Declaring a layout (changes are to be made to this)
// Declaring a textview (which is inside the layout)
val swipeRefreshLayout = findViewById(R.id.refreshLayout)
val textView = findViewById(R.id.tv1)
// Refresh function for the layout
swipeRefreshLayout.setOnRefreshListener{
// Your code goes here
// In this code, we are just changing the text in the
// textbox
textView.text = "Refreshed"
// This line is important as it explicitly refreshes only once
// If "true" it implicitly refreshes forever
swipeRefreshLayout.isRefreshing = false
}
}
}
Java
import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import org.w3c.dom.Text
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Declaring a layout (changes are to be made to this)
// Declaring a textview (which is inside the layout)
SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.refreshLayout);
TextView textView = (TextView)findViewById(R.id.tv1);
// Refresh the layout
swipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
// Your code goes here
// In this code, we are just
// changing the text in the textbox
textView.text = "Refreshed"
// This line is important as it explicitly
// refreshes only once
// If "true" it implicitly refreshes forever
swipeRefreshLayout.setRefreshing(false);
}
}
);
}
}
第 3 步:来到“ MainActivity ”文件,下面提供了相同的预览。在此文件中,使用findViewById()方法将 swipeRefreshLayout 和 textView 连接到其 XML 文件。并且还调用setOnRefreshListener()在用户向下滑动屏幕后更改文本。用户也可以在此方法中根据自己的需要编写所需的代码。
科特林
import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import org.w3c.dom.Text
class MainActivity : AppCompatActivity() {
@SuppressLint("SetTextI18n")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Declaring a layout (changes are to be made to this)
// Declaring a textview (which is inside the layout)
val swipeRefreshLayout = findViewById(R.id.refreshLayout)
val textView = findViewById(R.id.tv1)
// Refresh function for the layout
swipeRefreshLayout.setOnRefreshListener{
// Your code goes here
// In this code, we are just changing the text in the
// textbox
textView.text = "Refreshed"
// This line is important as it explicitly refreshes only once
// If "true" it implicitly refreshes forever
swipeRefreshLayout.isRefreshing = false
}
}
}
Java
import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import org.w3c.dom.Text
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Declaring a layout (changes are to be made to this)
// Declaring a textview (which is inside the layout)
SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.refreshLayout);
TextView textView = (TextView)findViewById(R.id.tv1);
// Refresh the layout
swipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
// Your code goes here
// In this code, we are just
// changing the text in the textbox
textView.text = "Refreshed"
// This line is important as it explicitly
// refreshes only once
// If "true" it implicitly refreshes forever
swipeRefreshLayout.setRefreshing(false);
}
}
);
}
}
输出:在模拟器上运行
好处
当然,受益的不仅仅是用户。假设有一个应用程序,其中信息是直接从云存储库中获取的。对于每个回调请求(向云),拥有此类存储库的开发人员向服务支付最低金额,可能是谷歌云平台 (GCP)、亚马逊网络服务 (AWS) 或任何其他东西。