1

我正在使用 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()));
        }

    }

如您所见,如果我设置为isShimmertrue,则微光会无限运行。因此,我无法获得所有数据都已加载以更改值的时间段isShimmer

4

2 回答 2

3

我使用这个库在获取数据时显示微光效果。首先在 xml 文件中为您的微光布局设置可见性,然后在活动开始时启动微光shimmerViewContainer.startShimmer()

shimmerViewContainer.stopShimmer()数据设置到活动文件中的适配器后,在您的 volley 方法中设置。

以及停止闪烁onPause()onDestroy()

于 2020-04-30T05:06:11.997 回答
0

如果您将微光放在父布局中而不是适配器布局中,您可能更容易保持微光状态。我更喜欢以下方法:


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>

重要属性:

应用程序:shimmer_auto_start="true"

所以你不需要以编程方式启动微光。


2.活动/片段

void initList(List<Data> items) {
  adapter.setItems(items);
  shimmer_container.setVisibility(View.GONE);
}

于 2020-04-30T05:02:59.493 回答