android - Android Viewpager Fragment 网络调用处理
问题描述
在 viewpager 片段内部有一个网络调用来加载数据。由于这个网络调用,它给用户造成了延迟。我如何处理驻留在 viewpager 内的片段内的网络调用。我只是不希望我的用户看到延迟,因为加载片段需要 4-7 秒。什么是通过网络调用 Viewpager 加载片段的有效方法
以下是活动代码:
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import android.os.Bundle;
import com.test.androidtest20202.R;
import com.test.androidtest20202.adapter.ViewPagerAdapter;
import java.util.ArrayList;
import java.util.List;
public class ViewPagerActivity extends AppCompatActivity {
ViewPagerAdapter adapter;
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pager);
viewPager = findViewById(R.id.viewpager);
ArrayList<Integer> task_ids = new ArrayList<>();
for(int i=0;i<5;i++){
task_ids.add(i);
}
adapter = new ViewPagerAdapter(this, getSupportFragmentManager(),task_ids);
viewPager.setAdapter(adapter);
}
}
下面的活动 xml 布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.ViewPagerActivity">
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewpager"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/white"
android:largeHeap="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
下面是我的 Viewpager 适配器:
import android.content.Context;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import com.test.androidtest20202.fragments.ViewPagerFragment;
import java.util.ArrayList;
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
private ArrayList<Integer> taskid;
private SparseArray<Fragment> registeredFragments = new SparseArray<>();
public ViewPagerAdapter(Context context, @NonNull FragmentManager fm, ArrayList<Integer> taskid) {
super(fm);
this.taskid = taskid;
}
@NonNull
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
Bundle bundle = new Bundle();
bundle.putInt("COMPLETED_TASKID", taskid.get(position));
fragment = new ViewPagerFragment();
registeredFragments.put(position, fragment);
fragment.setArguments(bundle);
return fragment;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
registeredFragments.remove(position);
super.destroyItem(container, position, object);
}
@Override
public int getCount() {
return taskid.size();
}
}
以下是我的 Viewpager 片段代码:
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.test.androidtest20202.R;
import com.test.androidtest20202.pojo.SaleskenResponse;
import com.test.androidtest20202.util.RestApiClient;
import com.test.androidtest20202.util.RestUrlInterface;
import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class ViewPagerFragment extends Fragment {
private static final String TAG = "ViewPagerFragment";
private ViewGroup container;
private LayoutInflater inflater;
@BindView(R.id.textView2)
TextView textView;
private AsyncTask mAsyncTask;
public RestUrlInterface restUrlInterface;
Integer taskid = -1;
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
this.container = container;
this.inflater = inflater;
return initializeView();
}
private View initializeView() {
final View view;
view = inflater.inflate(
R.layout.fragment_laout, container, false);
ButterKnife.bind(this, view);
restUrlInterface = RestApiClient.getClient(getContext()).create(RestUrlInterface.class);
if (getArguments() != null) {
taskid = getArguments().getInt("COMPLETED_TASKID");
}
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Call<SaleskenResponse> task_details = restUrlInterface.getTaskDetail(getString(R.string.token), taskid );
task_details.enqueue(new Callback<SaleskenResponse>() {
@Override
public void onResponse(Call<SaleskenResponse> call, Response<SaleskenResponse> response) {
switch (response.code()) {
case 200:
textView.setText( response.body().toString());
}
}
@Override
public void onFailure(Call<SaleskenResponse> call, Throwable t) {
}
});
}
}
下面是片段布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
解决方案
onCreate
因此,只需在您创建task_ids
ArrayList之后在活动中执行此操作后,只需加载一次数据。
您似乎正在使用自定义 RestAPI 包,但大多数包异步执行网络请求,因此当您收到每个请求的响应时,您将使用唯一的 task_id 作为索引将它们存储在共享视图模型中。
然后可以从片段中观察共享视图模型,当数据可用时,片段将被通知使用它。
因此,基本上只要 Activity 启动就开始网络加载,以便在设置 viewpager 和加载第一个 Fragment 之前先启动它,并独立于 Activity 和 Fragment 存储数据。
请注意,这并不能保证 viewpager 中的第一个片段在显示时将具有可用的数据,但至少它应该是使其可用以及用户切换到 viewpager 中的第二页时的一部分该数据应该已经可用。
如何使用共享视图模型在片段之间共享数据示例位于https://blog.mindorks.com/shared-viewmodel-in-android-shared-between-fragments(此示例在 2 个片段之间,但可以使用相同的概念用于 Activity 中的后台 RestAPI 调用和 viewpager 中的每个 Fragment 之间的通信。
在并行请求所有 task_id 的数据与按顺序从第一页开始,然后一旦加载请求下一个,等等,在第一页加载时间上可能需要权衡。
抱歉,没有代码示例(除了关于如何使用共享视图模型的链接),但更多的是架构答案,因为您似乎正在使用我不熟悉的自定义 RestAPI 包。
这基本上是在活动开始时缓存每个片段的数据,并且片段只显示缓存的数据。
如果您可以在每次启动 Activity 之间进行缓存,您可以使用像 Android Rooms 这样的数据库,而不是共享视图模型,那么当它启动 Activity 时,它具有上次启动 Activity 的数据,实际上它可以在后台运行使用 RestAPI 请求检查存储在数据库中的数据的新鲜度。
推荐阅读
- netlogo - 在 Netlogo 中,您可以将列表添加到一起吗?
- firebase - Flutter 上传一批图像并获取所有这些 URL 以存储在 Firestore 中
- python - ndarray 到表(从阶跃函数结果)
- python - 如何将 JSON 转换为格式化的嵌套数组?
- rust - rust 中事实上的字节类型是什么?
- php - 如何通过id从数据库中获取值
- javascript - Jquery 是 JavaScript 的替代品吗?
- angular - 将模型设置为未定义时的角度清除输入
- c++ - 将 exp 用于指数形式 (C++, 1.#INF)
- spring - API 未使用@PostMapping 接收信息,但使用@RequestMapping 是。Spring Boot、Kotlin、REST API