Yasiru Nayanajith
Yasiru Nayanajith

Reputation: 1737

How to reload the recycleview which loads data from a thread

In my android app, I have a recyclerView in a fragment. So the recyclerView carriers the details about the installed apps. I am using a customAdapter and loads the data to the recyclerView with a thread because if not the UI hangs. I am using a swipetoReload control. I can reload the recylerView by restarting the thread I used. But then the recyclerView dissappears and re-appears again instantly.

Am I doing something wrong here? Is there a way to avoid that. Or is there a better way to do this without a thread or something like that. Here are my codes.

Fragment :

Thread loadApps = new Thread() {
            @Override
            public void run() {

                swipeapplist.post(new Runnable() {
                    @Override
                    public void run() {
                        if (!swipeapplist.isRefreshing()) {
                            swipeapplist.setRefreshing(true);
                        }
                    }
                });

                appAdapter = new AppAdapter(context, getInstalledApps());
                final LinearLayoutManager layoutManager = new LinearLayoutManager(context);
                recyclerInstalledApps.post(new Runnable() {
                    @Override
                    public void run() {
                        recyclerInstalledApps.setLayoutManager(layoutManager);
                        LayoutAnimationController controller = AnimationUtils.loadLayoutAnimation(context, R.anim.recycler_layout_animation);

                        recyclerInstalledApps.setLayoutAnimation(controller);
                        recyclerInstalledApps.scheduleLayoutAnimation();
                        recyclerInstalledApps.setAdapter(appAdapter);
                    }
                });

                swipeapplist.post(new Runnable() {
                    @Override
                    public void run() {
                        if (swipeapplist.isRefreshing()) {
                            swipeapplist.setRefreshing(false);
                        }
                    }
                });
            }
        };
        loadApps.start();

Method to get app info

private ArrayList<AppInfo> getInstalledApps() {

    ArrayList<AppInfo> res = new ArrayList<>();
    try {
        if (context != null) {
            pm = context.getPackageManager();
            List<PackageInfo> packs = pm.getInstalledPackages(PackageManager.GET_PERMISSIONS);
            Collections.sort(packs, new Comparator<PackageInfo>() {
                @Override
                public int compare(PackageInfo arg0, PackageInfo arg1) {
                    CharSequence name0 = arg0.applicationInfo.loadLabel(pm);
                    CharSequence name1 = arg1.applicationInfo.loadLabel(pm);
                    return name0.toString().compareTo(name1.toString());
                }
            });
            for (int i = 0; i < packs.size(); i++) {
                PackageInfo p = packs.get(i);
                if ((!isSystemPackage(p))) {
                    String appName = p.applicationInfo.loadLabel(context.getPackageManager()).toString();
                    String packageName = p.applicationInfo.packageName;
                    String appVersion = "Version : " + p.versionName;
                    Drawable icon = p.applicationInfo.loadIcon(context.getPackageManager());
                    res.add(new AppInfo(appName, packageName, appVersion, icon));
                }
            }
        }
    } catch (NullPointerException ex) {
        ex.printStackTrace();
    }
    return res;
}

Upvotes: 0

Views: 114

Answers (1)

Marcelo Maia
Marcelo Maia

Reputation: 36

Try use notifyDataSetChanged

     appAdapter = new AppAdapter(context, getInstalledApps());
            final LinearLayoutManager layoutManager = new LinearLayoutManager(context);
            recyclerInstalledApps.post(new Runnable() {
                @Override
                public void run() {
                    recyclerInstalledApps.setLayoutManager(layoutManager);
                    LayoutAnimationController controller = AnimationUtils.loadLayoutAnimation(context, R.anim.recycler_layout_animation);

                    recyclerInstalledApps.setLayoutAnimation(controller);
                    recyclerInstalledApps.scheduleLayoutAnimation();
                    recyclerInstalledApps.setAdapter(appAdapter);
                }
            });
   appAdapter.notifyDataSetChanged();

Upvotes: 1

Related Questions