数据更改时 Fratment 刷新片段中的 SectionedRecyclerViewAdapter

SectionedRecyclerViewAdapter inside Fratment refresh fragment on data changed

我正在使用 SectionedRecyclerViewAdapter 在 recyclerview 中创建多个部分。问题是当我触发 switchcompat 时,我希望两个部分都得到更新。这意味着每当我将 switchcompat 状态更改为选中时,我希望该项目从所有应用程序列表中删除并添加到锁定的应用程序列表中,反之亦然。 换句话说,我希望我的片段在我触发 switchcompat 时得到更新。

实现它的最佳方法是什么?

片段

public class AppListFragment extends Fragment {

    Context mContext;
    RecyclerView recyclerView;
    RecyclerView.Adapter adapter;
    RecyclerView.LayoutManager recyclerViewLayoutManager;

    private SectionedRecyclerViewAdapter sectionAdapter;

    public AppListFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View appListView = inflater.inflate(R.layout.fragment_app_list,container,false);

        sectionAdapter = new SectionedRecyclerViewAdapter();

        List<AppItem> lockedAppItemList = getLockedAppList(inflater.getContext());
        List<AppItem> allAppItemList = getAllAppList(inflater.getContext());

        sectionAdapter.addSection(new SectionLockedApps("Locked Apps", inflater.getContext(),new AppManager(inflater.getContext()).getAllInstalledApkInfo(), lockedAppItemList,sectionAdapter));
        sectionAdapter.addSection(new SectionAllApps("All Apps", inflater.getContext(),new AppManager(inflater.getContext()).getAllInstalledApkInfo(), allAppItemList,sectionAdapter));

        try {
            recyclerView = (RecyclerView) appListView.findViewById(R.id.rv_applist);
            recyclerViewLayoutManager = new LinearLayoutManager(this.getContext());
            recyclerView.setLayoutManager(recyclerViewLayoutManager);
            //adapter = new AppsAdapter(inflater.getContext(), new AppManager(inflater.getContext()).getAllInstalledApkInfo());
            recyclerView.setAdapter(sectionAdapter);

        }catch (Exception ex){
            Log.i("exception",ex.getMessage());
        }

        return appListView;
    }

    @Override
    public void onResume() {
        super.onResume();

        if (getActivity() instanceof AppCompatActivity) {
            AppCompatActivity activity = ((AppCompatActivity) getActivity());
            if (activity.getSupportActionBar() != null) {
                activity.getSupportActionBar().setTitle(R.string.app_name);
            }
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        mContext = context;
    }

    private List<AppItem> getLockedAppList(Context ctx) {
        AppManager appManager = new AppManager(ctx);
        List<AppItem> appItemList = new ArrayList<>();
        List<String> apkPackInfoList = appManager.getAllInstalledApkInfo();

        for(String apkPackInfo:apkPackInfoList){
            String appName = appManager.getAppName(apkPackInfo);
            Drawable imageDrawable = appManager.getAppIconByPackageName(apkPackInfo);
            Boolean isLocked = appManager.isGuardEnabled(apkPackInfo);
            if(isLocked){
                appItemList.add(new AppItem(imageDrawable,appName,apkPackInfo,isLocked));
            }
        }

        return appItemList;
    }

    private List<AppItem> getAllAppList(Context ctx) {
        AppManager appManager = new AppManager(ctx);
        List<AppItem> appItemList = new ArrayList<>();
        List<String> apkPackInfoList = appManager.getAllInstalledApkInfo();

        for(String apkPackInfo:apkPackInfoList){
            String appName = appManager.getAppName(apkPackInfo);
            Drawable imageDrawable = appManager.getAppIconByPackageName(apkPackInfo);
            Boolean isLocked = appManager.isGuardEnabled(apkPackInfo);
            if(!isLocked){
                appItemList.add(new AppItem(imageDrawable,appName,apkPackInfo,isLocked));
            }
        }

        return appItemList;
    }
}

锁定的应用部分

public class SectionLockedApps extends StatelessSection {

    private SectionedRecyclerViewAdapter sectionAdapter;

    String title;
    boolean isGuardEnabled;
    Context ctxAppList;
    List<String> stringList;
    List<AppItem> appItemList;

    public SectionLockedApps(String title, Context ctx, List<String> stringList, List<AppItem> appItemList, SectionedRecyclerViewAdapter sectionAdapter) {
        super(SectionParameters.builder()
                .itemResourceId(R.layout.cardview_layout_applist)
                .headerResourceId(R.layout.section_header)
                .build());

        this.title = title;
        this.ctxAppList = ctx;
        this.stringList = stringList;
        this.appItemList = appItemList;
        this.sectionAdapter = sectionAdapter;
    }

    @Override
    public int getContentItemsTotal() {
        return appItemList.size();
    }

    @Override
    public RecyclerView.ViewHolder getItemViewHolder(View view) {
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindItemViewHolder(RecyclerView.ViewHolder holder, final int position) {

        final AppManager appManager = new AppManager(ctxAppList);
        final String applicationPackageName = appItemList.get(position).getPackageName();
        isGuardEnabled = appManager.isGuardEnabled(applicationPackageName);

        if (appItemList.size() > 0) {
            String ApplicationLabelName = appItemList.get(position).getAppName();
            Drawable drawable = appItemList.get(position).getImageDrawable();



            final ItemViewHolder itemHolder = (ItemViewHolder) holder;

            itemHolder.textView_App_Name.setText(ApplicationLabelName);

            //viewHolder.textView_App_Package_Name.setText(applicationPackageName);

            itemHolder.imageView.setImageDrawable(drawable);

            itemHolder.switchCompat.setChecked(isGuardEnabled);

            //Adding click listener on CardView to open clicked application directly from here .
            itemHolder.rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    Intent intent = ctxAppList.getPackageManager().getLaunchIntentForPackage(applicationPackageName);
                    if (intent != null) {

                        ctxAppList.startActivity(intent);

                    } else {

                        Toast.makeText(ctxAppList, applicationPackageName + " Error, Please Try Again.", Toast.LENGTH_LONG).show();
                    }
                }
            });

            itemHolder.switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    appManager.setGuard(applicationPackageName, isChecked);
                    //appItemList.get(position).setLocked(isChecked);

                }
            });
        }

    }

    @Override
    public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
        return  new HeaderViewHolder(view);
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
        HeaderViewHolder headerHolder = (HeaderViewHolder) holder;

        if(appItemList.size()>0){
            headerHolder.tvTitle.setText(title);
        }else {
            headerHolder.tvTitle.setVisibility(View.GONE);
        }



    }

    private class HeaderViewHolder extends RecyclerView.ViewHolder {

        private final TextView tvTitle;

        HeaderViewHolder(View view) {
            super(view);
            tvTitle = (TextView) view.findViewById(R.id.tvTitle);
        }
    }

    private class ItemViewHolder extends RecyclerView.ViewHolder {

        private final View rootView;

        public TextView textView_App_Name;
        public ImageView imageView;
        public SwitchCompat switchCompat;

        ItemViewHolder(View view) {
            super(view);

            rootView = view;

            textView_App_Name = (TextView) view.findViewById(R.id.Apk_Name);
            imageView = (ImageView) view.findViewById(R.id.imgApplist);
            switchCompat = (SwitchCompat) view.findViewById(R.id.compatSwitch);

        }
    }

}

所有应用部分

public class SectionAllApps extends StatelessSection {

    String title;
    boolean isGuardEnabled;
    Context ctxAppList;
    List<String> stringList;
    List<AppItem> appItemList;
    String applicationPackageName;
    AppManager appManager;

    private SectionedRecyclerViewAdapter sectionAdapter;

    public SectionAllApps(String title, Context ctx, List<String> stringList, List<AppItem> appItemList, SectionedRecyclerViewAdapter sectionAdapter) {
        super(SectionParameters.builder()
                .itemResourceId(R.layout.cardview_layout_applist)
                .headerResourceId(R.layout.section_header)
                .build());

        this.title = title;
        this.ctxAppList = ctx;
        this.stringList = stringList;
        this.appItemList = appItemList;
        this.sectionAdapter = sectionAdapter;
    }

    @Override
    public int getContentItemsTotal() {
        return appItemList.size();
    }

    @Override
    public RecyclerView.ViewHolder getItemViewHolder(View view) {
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindItemViewHolder(RecyclerView.ViewHolder holder, final int position) {

        final AppManager appManager = new AppManager(ctxAppList);
        final String applicationPackageName = appItemList.get(position).getPackageName();
        isGuardEnabled = appManager.isGuardEnabled(applicationPackageName);

        if (appItemList.size() > 0) {
            String ApplicationLabelName = appItemList.get(position).getAppName();
            Drawable drawable = appItemList.get(position).getImageDrawable();



            final SectionAllApps.ItemViewHolder itemHolder = (SectionAllApps.ItemViewHolder) holder;

            itemHolder.textView_App_Name.setText(ApplicationLabelName);

            //viewHolder.textView_App_Package_Name.setText(applicationPackageName);

            itemHolder.imageView.setImageDrawable(drawable);

            itemHolder.switchCompat.setChecked(isGuardEnabled);

            //Adding click listener on CardView to open clicked application directly from here .
            itemHolder.rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    Intent intent = ctxAppList.getPackageManager().getLaunchIntentForPackage(applicationPackageName);
                    if (intent != null) {

                        ctxAppList.startActivity(intent);

                    } else {

                        Toast.makeText(ctxAppList, applicationPackageName + " Error, Please Try Again.", Toast.LENGTH_LONG).show();
                    }
                }
            });

            itemHolder.switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    appManager.setGuard(applicationPackageName, isChecked);

                    //appItemList.get(position).setLocked(isChecked);

                }
            });
        }
    }

    @Override
    public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
        return new HeaderViewHolder(view);
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
        HeaderViewHolder headerHolder = (HeaderViewHolder) holder;

        headerHolder.tvTitle.setText(title);
    }

    private class HeaderViewHolder extends RecyclerView.ViewHolder {

        private final TextView tvTitle;

        HeaderViewHolder(View view) {
            super(view);
            tvTitle = (TextView) view.findViewById(R.id.tvTitle);
        }
    }

    private class ItemViewHolder extends RecyclerView.ViewHolder {

        private final View rootView;
        public ImageView imageView;
        public TextView textView_App_Name;
        public SwitchCompat switchCompat;


        ItemViewHolder(View view) {
            super(view);

            rootView = view;

            imageView = (ImageView) view.findViewById(R.id.imgApplist);
            textView_App_Name = (TextView) view.findViewById(R.id.Apk_Name);
            switchCompat = (SwitchCompat) view.findViewById(R.id.compatSwitch);
        }
    }
}

appManager.SetGuard() basically saves switchcompat's last state.

经过一些研究,我找到了解决问题的方法。解决方案不是使用应用程序列表,而是刷新片段。基本上我已经创建了一个接口,用于我的场景执行方法 refresh(); 的两个部分,并在我的片段

中实现了一个实现

代码如下:

界面

public interface IFragmentOperation {
    public void refresh();
}

fargment 中的修改

public class AppListFragment extends Fragment implements IFragmentOperation {
....

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    ...

    sectionAdapter.addSection(new SectionLockedApps("Locked Apps", inflater.getContext(),lockedAppItemList,this));
    sectionAdapter.addSection(new SectionAllApps("All Apps", inflater.getContext(),allAppItemList,this));

    ...
}

//Refresh fragment on switchcompat touched
@Override
public void refresh() {
    FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
    fragmentTransaction.detach(this).attach(this).commit();
}

}

如您所见,我已经使用方法 refresh() 实现了一个接口,并在我的 sections cunstructor 中添加了一个额外的参数 this。通过这种方式,我将接口实现发送到我的部分 类.

然后在我的部分中,我简单地创建 IFragmentOperation fragmentOperation; 并在构造函数

中分配 fragmentOperation 的值
IFragmentOperation fragmentOperation;

public SectionLockedApps(String title, Context ctx,List<AppItem> appItemList,IFragmentOperation fragmentOperation) {
    super(SectionParameters.builder()
            .itemResourceId(R.layout.cardview_layout_applist)
            .headerResourceId(R.layout.section_header)
            .build());

    this.title = title;
    this.ctxAppList = ctx;
    this.appItemList = appItemList;
    this.fragmentOperation = fragmentOperation;
}

然后像这样在需要的地方使用它fragmentOperation.refresh();

itemHolder.switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        appManager.setGuard(applicationPackageName, isChecked);
        //appItemList.get(position).setLocked(isChecked);
        fragmentOperation.refresh();

    }
});

就是这样。希望它能帮助有相关问题的人。