I have a fragment with a tabLayout and a recyclerView. When the user selects a tab I want the recyclerView to change its content with the method refreshList(). On default the first tab is selected and when I select the second one the content changes. But when I select the first one again it doesn't change the content anymore.
Here is my class:
public class SearchFragment extends Fragment {
private RecyclerView recView;
private View view;
private FloatingActionButton fab;
private TabLayout tabLayout;
private OwnAdapter ownAdapter;
final int MY_PERMISSIONS_REQUEST_EXTERNAL_STORAGE = 1;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.search_fragment, container, false);
recView = view.findViewById(R.id.file_list);
LinearLayoutManager manager = new LinearLayoutManager(getContext());
recView.setLayoutManager(manager);
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recView.getContext(),
manager.getOrientation());
recView.addItemDecoration(dividerItemDecoration);
if (ContextCompat.checkSelfPermission(getActivity(),
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
Manifest.permission.READ_EXTERNAL_STORAGE)) {
} else {
ActivityCompat.requestPermissions(getActivity(),
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_EXTERNAL_STORAGE);
}
}
refreshList("/sdcard/");
fab = view.findViewById(R.id.fab_search);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
tabLayout = view.findViewById(R.id.tabLayout);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
System.out.println(tab.getPosition());
switch(tab.getPosition()){
case 0:
refreshList("/sdcard/");
case 1:
refreshList("/storage/0000-0000/");
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {}
@Override
public void onTabReselected(TabLayout.Tab tab) {}
});
return view;
}
private void refreshList(String s){
File file = new File(s);
File[] files = file.listFiles();
ArrayList<File> itemArrayList = new ArrayList<>();
try {
for (int i = 0; i < files.length; i++) {
if(files[i].isFile()){
itemArrayList.add(files[i]);
}else{
itemArrayList.add(files[i].getAbsoluteFile());
}
}
}catch(RuntimeException e){
}
String[] itemNames = new String[itemArrayList.size()];
for(int j = 0; j < itemNames.length; j++){
itemNames[j] = itemArrayList.get(j).getName();
}
ownAdapter = new OwnAdapter(itemNames);
recView.setAdapter(ownAdapter);
}
}
The adapter class:
public class OwnAdapter extends RecyclerView.Adapter {
private String[] dataset;
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class MyViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView textView;
public MyViewHolder(TextView v) {
super(v);
textView = v;
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public OwnAdapter(String[] myDataset) {
dataset = myDataset;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
TextView v = (TextView) LayoutInflater.from(parent.getContext())
.inflate(R.layout.textview_for_files, parent, false);
MyViewHolder vh = new MyViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
MyViewHolder holder1 = (MyViewHolder) holder;
holder1.textView.setText(dataset[position]);
}
@Override
public int getItemCount() {
return dataset.length;
}
}
And the layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingRight="6dp"
android:paddingLeft="6dp">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="6dp">
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/internal_storage" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sd_card" />
</com.google.android.material.tabs.TabLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/file_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:paddingLeft="6dp"
android:paddingRight="6dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tabLayout"
tools:layout_editor_absoluteX="6dp"></androidx.recyclerview.widget.RecyclerView>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/fab_margin"
android:layout_marginBottom="@dimen/fab_margin"
app:fabSize="normal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="@drawable/add" />
</androidx.constraintlayout.widget.ConstraintLayout>