java - Recyclerview 不会使用大型数据集制作动画

原文 标签 java android animation android-recyclerview android-animation

我正在制作一个包含几个电影屏幕的应用程序,其中一个是从 imdbs 有史以来排名前 250 的电影列表中加载的。我试图在卡片加载时实现动画但无济于事。最终我将问题缩小到数据集太大的事实。当我将变量 loadHowMany 更改为 25 或更少时,动画加载正常。我是否必须实现某种无限滚动功能,或者有没有办法为这个大型数据集设置动画?

Top250Activity.java

public class Top250Activity extends BaseActivity {

    private RecyclerView mListView;
    JSONArray array;
    Movie[] searchResults;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_top250);

        mListView = findViewById(R.id.top250list);
        mListView.setNestedScrollingEnabled(false);


        try {
            getResults(new Runnable() {
                @Override
                public void run() {

                    final MovieCardAdapter adapter = new MovieCardAdapter(getApplicationContext(), searchResults);

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            LinearLayoutManager lLayout;

                            mListView.setNestedScrollingEnabled(false);
                            Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.item_animation_fall_down);
                            mListView.setAnimation(animation);
                            lLayout = new GridLayoutManager(Top250Activity.this, 2);
                            mListView.setHasFixedSize(true);
                            mListView.setLayoutManager(lLayout);
                            mListView.setAdapter(adapter);

                        }
                    });

                }
            });

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void getResults(final Runnable runnable) throws JSONException {
        final String apiKey = #######;
        final int loadHowMany = 100;
        String theURL = "https://api.morph.io/btrav528/imdb-top-250/data.json?key=#############&query=select%20*%20from%20%22data%22%20limit%20" + loadHowMany;
        final OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(theURL).build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                final String jsonData = response.body().string();

                JSONObject results;

                try {
                    array = new JSONArray(jsonData);
                    searchResults = new Movie[array.length()];
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject jsonResult = array.getJSONObject(i);
                        final Movie movie = new Movie();

                        String html = jsonResult.getString("name");
                        String title = Html.fromHtml(html).toString();
                        movie.setTitle(title);
                        movie.setYear(jsonResult.getString("year"));
                        Uri uri = Uri.parse(jsonResult.getString("link"));
                        String path = uri.getPath();
                        Log.d("taggie", path);
                        String[] segments = uri.getPath().split("/");
                        String idStr = segments[segments.length-1];
                        Log.d("taggieidstr", idStr);

                        String posterUrl = "http://img.omdbapi.com/?i=" + idStr + "&h=600&apikey=" + apiKey;

                        movie.setPosterURL(posterUrl);
                        movie.setImdbID(idStr);

                        searchResults[i] = movie;
                    }
                    runnable.run();

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

}

item_animation_fall_down.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:duration="@integer/config_shortAnimTime">

    <translate
        android:fromYDelta="-20%"
        android:toYDelta="0"
        android:interpolator="@android:anim/decelerate_interpolator"
        />

    <alpha
        android:fromAlpha="0"
        android:toAlpha="1"
        android:interpolator="@android:anim/decelerate_interpolator"
        />

    <scale
        android:fromXScale="105%"
        android:fromYScale="105%"
        android:toXScale="100%"
        android:toYScale="100%"
        android:pivotX="50%"
        android:pivotY="50%"
        android:interpolator="@android:anim/decelerate_interpolator"
        />

</set>

最佳答案

我想最好通过无限滚动来实现它。
一个有用的链接:
https://camposha.info/source/android-sqlite-recyclerview-infiniteendless-paginationload-serverside

关于java - Recyclerview 不会使用大型数据集制作动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50421108/

相关文章:

java - 对于这种特殊情况,我应该使用什么数据结构?

java - 静态资源 : where define context aliases with tomcat

android - 无法单击 ListView 项目,没有任何其他监听器被调用

android - WebViewClient 无法解析为类型

android - 使用 Canvas 时如何制作动画

javascript - 遵循 TweenJs 中的自定义路径

java - 通用辅助方法与直接类转换?

java switch 跳转到多于1个案例

android - 通过热点获取连接设备的名称

css - CSS悬停颜色从右到div的25%的变化