【问题标题】:How to use recycleview inside an alertdialog in fragment如何在片段的警报对话框中使用recycleview
【发布时间】:2021-10-11 19:03:11
【问题描述】:

Fragment 中的alertdialog 中显示recycleView 中的项目时遇到问题

我显示的项目是文件名。

这是我正在做的设计结构 >Activity > Fragment > AlertDialog

在我的 AlertDialog 中,我正在扩充一个新布局以获得我的自定义设计,我可以在其中放置事件以打开存储并获取多个或单个文件。

下面的代码只打开文件

getActivity().startActivityForResult(intent, 1001); 这意味着我的活动覆盖了OnActivityResult

private void browseDocuments(){

    String[] mimeTypes =
            {"application/msword","application/vnd.openxmlformats-officedocument.wordprocessingml.document", // .doc & .docx
                    "application/vnd.ms-powerpoint","application/vnd.openxmlformats-officedocument.presentationml.presentation", // .ppt & .pptx
                    "application/vnd.ms-excel","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", // .xls & .xlsx
                    "text/plain",
                    "application/pdf",
                    "application/zip"};

    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        intent.setType(mimeTypes.length == 1 ? mimeTypes[0] : "*/*");
        if (mimeTypes.length > 0) {
            intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
        }
    } else {
        String mimeTypesStr = "";
        for (String mimeType : mimeTypes) {
            mimeTypesStr += mimeType + "|";
        }
        intent.setType(mimeTypesStr.substring(0,mimeTypesStr.length() - 1));
    }

    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE,true);
    getActivity().startActivityForResult(intent, 1001);

}

下面的代码在我的 Activity 的 OnActivityResult 中将 filesNames 添加到 arraysList,它正在完美运行并获取文件

 if (requestCode == 1001){

        FileAdapter = new fileAdapter(announcement_fragment.FileListName);
        LayoutInflater inflater = this.getLayoutInflater(); //Is this correct?
        View dialogView = inflater.inflate(R.layout.add_announcement, null);
        RecyclerView listFiles = dialogView.findViewById(R.id.containerForFiles);
        listFiles.setLayoutManager(new LinearLayoutManager(dialogView.getContext()));
        listFiles.setHasFixedSize(true);
        listFiles.setAdapter(FileAdapter);

       if(resultCode == RESULT_OK){
            //Upload Mutliple
            if(data.getClipData() !=null){
                announcement_fragment.FileListName.clear();

               // Toast.makeText(this,"Mutliple",Toast.LENGTH_LONG).show();
                int count = data.getClipData().getItemCount();
                int i=0;
                while (i<count){
                    announcement_fragment.FileListName.add(getFileName(File));
                    FileAdapter.notifyDataSetChanged();
                    i++;
                }
            }else if(data.getData() != null){
                announcement_fragment.FileListName.add(getFileName(File));
                FileAdapter.notifyDataSetChanged();
            }else{
                //Do nothing
            }
      }

 }

我在OnActivityResult 内的活动中调用并设置适配器(参见上面的代码)以通知适配器但它不起作用,接下来我不知道可以做些什么来实现它,我应该把适配器放在哪里来触发回收查看项目

【问题讨论】:

    标签: java android-studio android-fragments android-recyclerview android-alertdialog


    【解决方案1】:

    您可以先尝试使用 ListView,然后看看它是否有效。
    问题可能出在您的代码上。

    尝试使用ListView,如果可行,请尝试使用RecycleView

    【讨论】:

      猜你喜欢
      • 2014-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-10
      • 1970-01-01
      • 1970-01-01
      • 2018-08-27
      相关资源
      最近更新 更多