首页 > 解决方案 > 从 RecyclerView 中“隐藏”项目

问题描述

从 RecyclerView 中“隐藏”一个项目。移除一个项目,但允许用户将其取回。用户必须能够将所有已移除的项目取回。对此采取不同的方法。

在此处输入图像描述

上面的链接是列表的屏幕截图。我希望能够从列表中隐藏一个项目(如上图所示),但仍然允许用户将所有隐藏的项目带回列表中。

用于填充列表的代码。

    //Method used to retrieve all installed applications
    private ArrayList<AppData> getInstalledPackages() {
        List<String> appsList = new ArrayList<>();
        ArrayList<AppData> appDataArrayList = new ArrayList<AppData>();
        AppInfoExtractor apkInfoExtractor = new AppInfoExtractor(this);
        appsList = apkInfoExtractor.GetAllInstalledApkInfo();
        if (appsList != null && appsList.size() > 0) {
            for (int i = 0; i < appsList.size(); i++) {

                AppData appData = new AppData();
                appData.setAppName(apkInfoExtractor.GetAppName(appsList.get(i)));
                appData.setPackageName(appsList.get(i));
                Version version = new Version();
                version.setVersionName(apkInfoExtractor.getVersionName(appsList.get(i)));
                version.setVersionCode(apkInfoExtractor.getAppVersionCode(appsList.get(i)));
                List<String> grantedPermissions = getPermissionsForPackage(appsList.get(i));
                List<String> grantedPermissionsShort = getShortPermissionStrings(grantedPermissions);
                version.setPermissions(grantedPermissionsShort);
                ArrayList<Version> versions = new ArrayList<Version>();
                versions.add(version);
                appData.setVersions(versions);
                appDataArrayList.add(appData);
            }
        }
        return appDataArrayList;
    }

列表适配器的代码。

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {

        AppInfoExtractor apkInfoExtractor = new AppInfoExtractor(context1);

        final String ApplicationPackageName = (String) stringList.get(position);
        final String ApplicationLabelName = apkInfoExtractor.GetAppName(ApplicationPackageName);
        Drawable drawable = apkInfoExtractor.getAppIconByPackageName(ApplicationPackageName);

        viewHolder.textView_App_Name.setText(ApplicationLabelName);

        viewHolder.textView_App_Package_Name.setText(ApplicationPackageName);
        viewHolder.tv_version_name.setText("Version Name: " + apkInfoExtractor.getVersionName(ApplicationPackageName));
        viewHolder.tv_version_code.setText("Version Code: " + apkInfoExtractor.getAppVersionCode(ApplicationPackageName));

        viewHolder.imageView.setImageDrawable(drawable);

        //Adding click listener on CardView to open clicked application directly from here .
        viewHolder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent perm = new Intent(context1, AppDetailActivity.class);
                perm.putExtra("PACKAGE_NAME", ApplicationPackageName);
                perm.putExtra("APP_NAME", ApplicationLabelName);
                context1.startActivity(perm);
            }
        });
    }

标签: androidandroid-recyclerview

解决方案


请参阅Recyclerview Swipe to remove以从中删除项目recyclerView

要取消隐藏所有项目,请添加一个带有“取消隐藏所有”选项的菜单选项。在列表中滑动时存储所有已删除的数据,并在选择“取消隐藏全部”菜单选项时
将其附加到数据中。recyclerView

希望能帮助到你。


推荐阅读