Home » Android » Why can't I implement a file browser in a fragment?

Why can't I implement a file browser in a fragment?

Posted by: admin November 1, 2017 Leave a comment

Questions:

I am building a music app which has a tablayout and the tabs are implemented as fragments. I created a file browser in one of the fragments(MyFiles.java) using RecyclerView. It shows the directories and files but the problem is when I click any of the directories it does not go into the directory

Screenshot below:

enter image description here

MyFiles.java

public class My_Files extends Fragment {


private static final String TAG = "My_Files";

RecyclerView recyclerView_files;
private ArrayList<MyFilesModel> item = new ArrayList<>();
private ArrayList<String> path = new ArrayList<>();
private String root;
private TextView myFilesPath;
MyFilesAdapter myFilesAdapter;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.my_files_activity, container, false);

    recyclerView_files = view.findViewById(R.id.recyclerView_files);
    myFilesPath = view.findViewById(R.id.myFilesPath);
    root = Environment.getExternalStorageDirectory().getPath();

    LinearLayoutManager mlinearLayoutManager = new LinearLayoutManager(getContext());
    recyclerView_files.setLayoutManager(mlinearLayoutManager);




    MyFilesAdapter myFilesAdapter = new MyFilesAdapter(getContext(), item, new MyFilesAdapter.FilePickerListener() {
        @Override
        public void onClickListener(MyFilesModel myFilesModel, int positionFiles) {

            File file = new File(path.get(positionFiles));

            if (file.isDirectory())
            {
                if(file.canRead()){

                    getDir(path.get(positionFiles));

                }else{

                    Toast.makeText(getContext(), "Can't open", Toast.LENGTH_SHORT).show();
                }
            }else {

                //play music


            }


        }
    });

    getDir(root);

    recyclerView_files.setAdapter(myFilesAdapter);

    return view;
}


private void getDir(String dirPath)

{

    myFilesPath.setText("Location: " + dirPath);


    MyFilesModel myFilesModel;




    File f = new File(dirPath);

    File[] files = f.listFiles();



    if(!dirPath.equals(root))

    {

        myFilesModel = new MyFilesModel(root);


        item.add(myFilesModel);


        path.add(root);

        myFilesModel = new MyFilesModel("../");

        item.add(myFilesModel);

        path.add(f.getParent());



    }



    for(int i=0; i < files.length; i++)

    {

        File file = files[i];

        String filename = file.getName();
        String ext = filename.substring(filename.lastIndexOf('.') + 1, filename.length());

        if (!file.isHidden() && file.canRead()) {






                path.add(file.getPath());


                if (file.isDirectory()) {



                    myFilesModel = new MyFilesModel(file.getName() + "/");


                    item.add(myFilesModel);


                } else {

                    if (ext.equals("mp3") || ext.equals("MP3")) {

                        myFilesModel = new MyFilesModel(file.getName());


                        item.add(myFilesModel);

                    }
                }

              myFilesAdapter.notifyDataSetChanged();

        }



    }



}

MyFilesAdapter.java:

public class MyFilesAdapter extends RecyclerView.Adapter<MyFilesAdapter.MyFilesHolder> {

Context fContext;
ArrayList<MyFilesModel> FileList = new ArrayList<>();
private FilePickerListener filePickerListener;

public MyFilesAdapter(Context fContext, ArrayList<MyFilesModel> fileList, MyFilesAdapter.FilePickerListener filePickerListener) {
    this.fContext = fContext;
    FileList = fileList;
    this.filePickerListener = filePickerListener;
}

@Override
public MyFilesAdapter.MyFilesHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View fView = LayoutInflater.from(fContext).inflate(R.layout.row_myfiles, parent, false);

    return new MyFilesHolder(fView);
}

@Override
public void onBindViewHolder(MyFilesAdapter.MyFilesHolder holder, int position) {

    final MyFilesModel myFilesModel = FileList.get(position);

    holder.myFilesText.setText(myFilesModel.getFileFolderName());

    holder.bindFile(myFilesModel, filePickerListener);

}

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

public class MyFilesHolder extends RecyclerView.ViewHolder {

    ImageView myFilesImage;
    TextView myFilesText;

    public MyFilesHolder(View itemView) {
        super(itemView);

        myFilesImage = itemView.findViewById(R.id.myFilesImage);

        myFilesText = itemView.findViewById(R.id.myFilesText);



    }

    public void bindFile(final MyFilesModel myFilesModel, final FilePickerListener filePickerListener) {

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                filePickerListener.onClickListener(myFilesModel, getLayoutPosition());
            }
        });

    }

}

public interface FilePickerListener{

    void onClickListener( MyFilesModel myFilesModel, int positionFiles);

}
Answers:

You need to call adapter.notifyDataSetChanged() after you’ve added the files to your list i.e. in the getDir(..) method.

public class My_Files extends Fragment {


private static final String TAG = "My_Files";

RecyclerView recyclerView_files;
private ArrayList<MyFilesModel> item = new ArrayList<>();
private ArrayList<String> path = new ArrayList<>();
private String root;
private TextView myFilesPath;
MyFilesAdapter myFilesAdapter;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.my_files_activity, container, false);

    recyclerView_files = view.findViewById(R.id.recyclerView_files);
    myFilesPath = view.findViewById(R.id.myFilesPath);
    root = Environment.getExternalStorageDirectory().getPath();

    LinearLayoutManager mlinearLayoutManager = new LinearLayoutManager(getContext());
    recyclerView_files.setLayoutManager(mlinearLayoutManager);




    myFilesAdapter = new MyFilesAdapter(getContext(), item, new MyFilesAdapter.FilePickerListener() {
        @Override
        public void onClickListener(MyFilesModel myFilesModel, int positionFiles) {

            File file = new File(path.get(positionFiles));

            if (file.isDirectory())
            {
                if(file.canRead()){

                    getDir(path.get(positionFiles));

                }else{

                    Toast.makeText(getContext(), "Can't open", Toast.LENGTH_SHORT).show();
                }
            }else {

                //play music


            }


        }
    });

    getDir(root);

    recyclerView_files.setAdapter(myFilesAdapter);

    return view;
}


private void getDir(String dirPath)

{

    myFilesPath.setText("Location: " + dirPath);


    MyFilesModel myFilesModel;




    File f = new File(dirPath);

    File[] files = f.listFiles();



    if(!dirPath.equals(root))

    {

        myFilesModel = new MyFilesModel(root);


        item.add(myFilesModel);


        path.add(root);

        myFilesModel = new MyFilesModel("../");

        item.add(myFilesModel);

        path.add(f.getParent());



    }



    for(int i=0; i < files.length; i++)

    {

        File file = files[i];

        String filename = file.getName();
        String ext = filename.substring(filename.lastIndexOf('.') + 1, filename.length());

        if (!file.isHidden() && file.canRead()) {






            path.add(file.getPath());


            if (file.isDirectory()) {



                myFilesModel = new MyFilesModel(file.getName() + "/");


                item.add(myFilesModel);


            } else {

                if (ext.equals("mp3") || ext.equals("MP3")) {

                    myFilesModel = new MyFilesModel(file.getName());


                    item.add(myFilesModel);

                }
            }

            myFilesAdapter.notifyDataSetChanged();

        }



    }



}