如何获得加载数据以停止微光的时间?
How to get period when data is loaded to stop shimmer?
我正在使用 Volley 处理请求,并希望在加载数据时添加微光。我可以用 Handler 执行它,但我不知道什么时候会有数据,无法设置准确的时间。
这是我片段中的代码。我想将值 false
设置为变量 isShimmer
它变得如此之快所以我的微光不是 运行:
String url = "my_url";
JsonArrayRequest getArticleOfTheDayRequest = new JsonArrayRequest(Request.Method.GET, url, null,
response -> {
try {
for(int i = 0; i < response.length(); i++){
JSONObject articleObject = response.getJSONObject(i);
int article_id = articleObject.getInt("article_id");
String title = articleObject.getString("title");
String text = articleObject.getString("text");
ArticleOfTheWeek articleOfTheWeek = new ArticleOfTheWeek(article_id, title, text);
articleOfTheWeekList.add(articleOfTheWeek);
}
articleOfTheWeekAdapter = new ArticleOfTheWeekAdapter(getContext(), articleOfTheWeekList);
recyclerView.setAdapter(articleOfTheWeekAdapter);
articleOfTheWeekAdapter.isShimmer = false;
articleOfTheWeekAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
},
error -> Log.d("Error.Response", error.getMessage() + " ")
);
这是我的适配器中的代码:
public boolean isShimmer = true;
int shimmerNumber = 2;
@Override
public void onBindViewHolder(@NonNull final ArticleOfTheWeekViewHolder holder, int position) {
final ArticleOfTheWeek articleOfTheWeek = articleOfTheWeekList.get(position);
if(isShimmer)
{
holder.shimmerFrameLayout.startShimmer();
}else
{
holder.shimmerFrameLayout.stopShimmer();
holder.shimmerFrameLayout.setShimmer(null);
holder.textViewTitle.setBackground(null);
holder.textViewTitle.setText(articleOfTheWeek.getTitle());
holder.textViewDesc.setBackground(null);
holder.textViewDesc.setText(Html.fromHtml(articleOfTheWeek.getText()));
}
}
如您所见,如果我将 isShimmer
设置为 true,微光将无限运行。所以我无法获得加载所有数据以更改 isShimmer
值的时间段
如果将 shimmer 放在父布局而不是适配器布局中,可能会更容易保持 shimmer 状态。我更喜欢以下方法:
1. Activity/Fragment
的布局里面
<RelativeLayout>
<RecyclerView/>
<FrameLayout
android:id="@+id/shimmer_container">
<!-- show hide this FrameLayout container depend on state of data loading/loaded -->
<include layout="@layout/list_shimmer"/>
</FrameLayout>
</RelativeLayout>
list_shimmer 可能看起来像:
<com.facebook.shimmer.ShimmerFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:shimmer_auto_start="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/just_like_adapter_item_layout" />
<include layout="@layout/just_like_adapter_item_layout" />
<!-- repeat until it's enough to fill the screen -->
</LinearLayout>
</com.facebook.shimmer.ShimmerFrameLayout>
重要的属性:
app:shimmer_auto_start="true"
因此您不需要以编程方式启动 shimmer。
2。 Activity/Fragment
void initList(List<Data> items) {
adapter.setItems(items);
shimmer_container.setVisibility(View.GONE);
}
我使用 this 库在获取数据时显示微光效果。首先在 xml 文件中将 visibility 设置为 true,然后在 activity shimmerViewContainer.startShimmer()
.
文件的开头启动 shimmer
在 activity 文件中为适配器设置数据后,在 volley 方法中设置 shimmerViewContainer.stopShimmer()
。
以及停止在 onPause()
和 onDestroy()
上闪烁
我正在使用 Volley 处理请求,并希望在加载数据时添加微光。我可以用 Handler 执行它,但我不知道什么时候会有数据,无法设置准确的时间。
这是我片段中的代码。我想将值 false
设置为变量 isShimmer
它变得如此之快所以我的微光不是 运行:
String url = "my_url";
JsonArrayRequest getArticleOfTheDayRequest = new JsonArrayRequest(Request.Method.GET, url, null,
response -> {
try {
for(int i = 0; i < response.length(); i++){
JSONObject articleObject = response.getJSONObject(i);
int article_id = articleObject.getInt("article_id");
String title = articleObject.getString("title");
String text = articleObject.getString("text");
ArticleOfTheWeek articleOfTheWeek = new ArticleOfTheWeek(article_id, title, text);
articleOfTheWeekList.add(articleOfTheWeek);
}
articleOfTheWeekAdapter = new ArticleOfTheWeekAdapter(getContext(), articleOfTheWeekList);
recyclerView.setAdapter(articleOfTheWeekAdapter);
articleOfTheWeekAdapter.isShimmer = false;
articleOfTheWeekAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
},
error -> Log.d("Error.Response", error.getMessage() + " ")
);
这是我的适配器中的代码:
public boolean isShimmer = true;
int shimmerNumber = 2;
@Override
public void onBindViewHolder(@NonNull final ArticleOfTheWeekViewHolder holder, int position) {
final ArticleOfTheWeek articleOfTheWeek = articleOfTheWeekList.get(position);
if(isShimmer)
{
holder.shimmerFrameLayout.startShimmer();
}else
{
holder.shimmerFrameLayout.stopShimmer();
holder.shimmerFrameLayout.setShimmer(null);
holder.textViewTitle.setBackground(null);
holder.textViewTitle.setText(articleOfTheWeek.getTitle());
holder.textViewDesc.setBackground(null);
holder.textViewDesc.setText(Html.fromHtml(articleOfTheWeek.getText()));
}
}
如您所见,如果我将 isShimmer
设置为 true,微光将无限运行。所以我无法获得加载所有数据以更改 isShimmer
如果将 shimmer 放在父布局而不是适配器布局中,可能会更容易保持 shimmer 状态。我更喜欢以下方法:
1. Activity/Fragment
的布局里面<RelativeLayout>
<RecyclerView/>
<FrameLayout
android:id="@+id/shimmer_container">
<!-- show hide this FrameLayout container depend on state of data loading/loaded -->
<include layout="@layout/list_shimmer"/>
</FrameLayout>
</RelativeLayout>
list_shimmer 可能看起来像:
<com.facebook.shimmer.ShimmerFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:shimmer_auto_start="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/just_like_adapter_item_layout" />
<include layout="@layout/just_like_adapter_item_layout" />
<!-- repeat until it's enough to fill the screen -->
</LinearLayout>
</com.facebook.shimmer.ShimmerFrameLayout>
重要的属性:
app:shimmer_auto_start="true"
因此您不需要以编程方式启动 shimmer。
2。 Activity/Fragment
void initList(List<Data> items) {
adapter.setItems(items);
shimmer_container.setVisibility(View.GONE);
}
我使用 this 库在获取数据时显示微光效果。首先在 xml 文件中将 visibility 设置为 true,然后在 activity shimmerViewContainer.startShimmer()
.
在 activity 文件中为适配器设置数据后,在 volley 方法中设置 shimmerViewContainer.stopShimmer()
。
以及停止在 onPause()
和 onDestroy()