在Fratment刷新片段内的SectionedRecyclerViewAdapter对数据进行了更改

时间:2018-04-07 14:05:53

标签: android sectionedrecyclerviewadapter

我使用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()基本上保存了switchcompat的最后状态。

1 个答案:

答案 0 :(得分:0)

经过一些研究,我找到了解决问题的方法。解决方案不是与应用程序列表一起使用,而是刷新片段。基本上我已经创建了一个接口,用于我使用方法 <table> <thead> <tr> <th ><%= check_box_tag "select_all",id: 'select_all_messages' %></th> <th>Content</th> <th colspan="3"></th> </tr> </thead> <tbody> <%= form_tag messages_path, method: :delete do %> <% @messages.each do |message| %> <tr> <td><%#= check_box_tag "message_ids[]", message.id, class: 'check_uncheck_message'%></td> <td><%= check_box_tag "message_ids[]",message.id,false, class: 'check_uncheck_message'%></td> <td><%= message.content %></td> <td><%= link_to 'Show', message %></td> <td class="edititem"><%= link_to 'Edit', edit_message_path(message)%></td> </tr> <% end %> </tbody> </table> <script type="text/javascript"> $("#select_all_messages").on('click', function () { $('.check_uncheck_message').prop('checked', this.checked); //$('.check_uncheck_message').prop("checked", true); }); </script> 执行场景的两个部分,并在我的片段中创建了一个实现

代码如下:

接口

refresh();

fargment中的修改

public interface IFragmentOperation {
    public void refresh();
}

}

正如您所看到的,我已经使用方法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(); } 实现了一个界面,并在我的部分cunstructor中添加了一个额外的参数 this 。这样我就可以将接口实现发送到我的节类中了。

然后在我的部分中,我只需创建refresh()并在构造函数中指定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();

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