Soru & Cevap

Java Android ile Recyclerview geç açılma problemi.

19.11.2018 - 03:37

Recyclerview'daki uygulamanın simgelerini ve adlarını gösteriyorum.İlk açılışta biraz geç açılıyor.Nasıl hızlandırabilirim? Kodlarım aşşağıdaki gibidir.

 

AppInfo.class


public class AppInfo {
    private String appName;
    private Drawable appIcon;

    private Context mContext;
    private ArrayList<AppInfo> myApps;

    public Drawable getAppIcon() {
        return appIcon;
    }

    public void setAppIcon(Drawable appIcon) {
        this.appIcon = appIcon;
    }

    public String getAppName() {
        return appName;
    }

    public void setAppName(String appName) {
        this.appName = appName;
    }

    public AppInfo(Context c) {
        mContext = c;
        myApps = new ArrayList<AppInfo>();
    }

    public ArrayList<AppInfo> getApps() {
        loadApps();
        return myApps;
    }

    private void loadApps() {
        List<ApplicationInfo> packages = mContext.getPackageManager().getInstalledApplications(0);
        for (ApplicationInfo packageInfo : packages) {
            AppInfo newApp = new AppInfo(mContext);
            newApp.setAppName(getApplicationName(packageInfo.packageName));
            newApp.setAppIcon(getApplicationIcon(packageInfo.packageName));
            myApps.add(newApp);
            Log.i("AppInfo", "loadApps");
        }

        Collections.sort(myApps, new Comparator<AppInfo>() {
            @Override
            public int compare(AppInfo s1, AppInfo s2) {
                return s1.getAppName().compareToIgnoreCase(s2.getAppName());

            }
        });
    }

    private Drawable getApplicationIcon(String packageName) {
        Drawable icon;
        try {
            icon = mContext.getPackageManager().getApplicationIcon(packageName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            icon = ContextCompat.getDrawable(mContext, R.drawable.ic_launcher_background);
        }
        Log.i("AppInfo", "getAppIconByPackageName");
        return icon;
    }

    private String getApplicationName(String packageName) {
        PackageManager packageManager = mContext.getPackageManager();
        ApplicationInfo applicationInfo;
        String label = "Unknown";
        try {
            applicationInfo = packageManager.getApplicationInfo(packageName, 0);
            if (applicationInfo != null) {
                label = (String) packageManager.getApplicationLabel(applicationInfo);
            }

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        Log.i("AppInfo", "getApplicationLabelByPackageName");
        return label;
    }

}


InstalledAppsAdapter

public class InstalledAppsAdapter extends RecyclerView.Adapter<InstalledAppsAdapter.ViewHolder> {

    private Context mContext;
    private ArrayList<AppInfo> mDataSet;


    public InstalledAppsAdapter(Context context, ArrayList<AppInfo> list) {
        mContext = context;
        mDataSet = list;
        Log.i("InstalledAppsAdapter","InstalledAppsAdapter");
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView mTextViewLabel;
        public ImageView mImageViewIcon;

        public ViewHolder(View v) {
            super(v);
            mTextViewLabel = v.findViewById(R.id.txtAppName);
            mImageViewIcon = v.findViewById(R.id.imgIcon);
            Log.i("InstalledAppsAdapter","ViewHolder");
        }

    }

    @Override
    public InstalledAppsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);
        ViewHolder vh = new ViewHolder(v);
        Log.i("InstalledAppsAdapter","InstalledAppsAdapter.ViewHolder");
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.mTextViewLabel.setText(mDataSet.get(position).getAppName());
        holder.mImageViewIcon.setImageDrawable(mDataSet.get(position).getAppIcon());
        Log.i("InstalledAppsAdapter","onBindViewHolder");
    }

    @Override
    public int getItemCount() {
        return mDataSet.size();
    }
}


MainActivity

public class MainActivity extends AppCompatActivity {

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

        RecyclerView mRecyclerView = findViewById(R.id.rcylView);

        mRecyclerView.setHasFixedSize(true);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager);
        AppInfo appInfo = new AppInfo(this);
        InstalledAppsAdapter mAdapter = new InstalledAppsAdapter(getApplicationContext(),appInfo.getApps());
        mRecyclerView.setAdapter(mAdapter);

    }
}


 

6 Görüntülenme

1 Cevap

Sitedeki sorulara cevap verebilmek için giriş yapın ya da üye olun.

Profile picture for user furkankaplan
furkankaplan
29.09.2019 - 06:20
Çözünürlüğü yüksek resimlerden kaynaklanan bir sorundur büyük ihtimalle. Resimleri düzenleyip tekrar deneyin. Kolay gelsin