How to Dynamically change content of TabLayout having dynamic tabs

26 Views Asked by At

So I am trying to retrieve table names from the SQLite database, create fragments dynamically for each table, and populate the RecyclerView within each fragment with the respective table data. I am using Tablayout along with viewpager.

MainActivity.java

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        DataBaseHelper dataBaseHelper = new DataBaseHelper(MainActivity.this);
        dataBaseHelper.createTable("Fav Task");
        dataBaseHelper.createTable("My Task");
        tabs = (ArrayList<String>) dataBaseHelper.getAllTables();

        binding.tabLayout.removeAllTabs();

        for (int k = 0; k <tabs.size(); k++) {
            binding.tabLayout.addTab(binding.tabLayout.newTab().setText(""+tabs.get(k)));
            
        if(tabs.size()==0){
            Toast.makeText(this, "empty", Toast.LENGTH_SHORT).show();
        }else{
            VPAdapter adapter = new VPAdapter(getSupportFragmentManager(), binding.tabLayout.getTabCount(), tabs);
            binding.viewPager.setAdapter(adapter);
            binding.viewPager.setOffscreenPageLimit(1);
            binding.viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(binding.tabLayout));

            if (binding.tabLayout.getTabCount() == 2) {
                binding.tabLayout.setTabMode(TabLayout.MODE_FIXED);
            } else {
                binding.tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
            }
        }
}

Adapter code for recycler view

public class RecyclerTaskAdapter extends RecyclerView.Adapter<TaskViewHolder> {

    private List<ToDoModel> mList;
    ArrayList<String> taskTitle;
    private DataBaseHelper myDB;

    public RecyclerTaskAdapter(ArrayList<String> taskTitle){
        this.taskTitle = taskTitle;
    }

    @NonNull
    @Override
    public TaskViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.recycler_task_view, parent, false);

        return new TaskViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull TaskViewHolder holder, int position) {
            
            holder.taskRadio.setText(String.valueOf(taskTitle.get(position)));
    }

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

Fragment code

public class Fragment_task extends Fragment {

    RecyclerView recyclerView;
    ArrayList<String> taskTitle;
    View view;

    public static Fragment_task newInstance(int tabPosition, String tabName){
        Fragment_task fragment = new Fragment_task();
        Bundle args = new Bundle();
        args.putInt("tabPosition", tabPosition);
        args.putString("tabName", tabName);
        fragment.setArguments(args);
        return fragment;
    }

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

        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_task, container, false);
        recyclerView = view.findViewById(R.id.recycleView);

        String tabName = getArguments().getString("tabName");
        DataBaseHelper dataBaseHelper = new DataBaseHelper(getContext());

        taskTitle = dataBaseHelper.getTasksTitle(tabName);

        if(taskTitle!=null && !taskTitle.isEmpty()){
            Log.e("ARRAYLIST", taskTitle.get(0));
        }else{
            Log.e("EMPTYYYY", "NOO TASSKK");
        }

        RecyclerTaskAdapter adapter = new RecyclerTaskAdapter(taskTitle);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(adapter);

        return view;
    }

}

frgament_task.xml

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout 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:id="@+id/swipeRefreshLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Fragment.Fragment_task">

    <androidx.constraintlayout.widget.ConstraintLayout

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recycleView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

recycler_task_view.xml

<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:cardCornerRadius="8dp"
    app:cardElevation="5dp"
    app:cardBackgroundColor="?attr/colorSurfaceDim"
    android:layout_marginHorizontal="16dp"
    android:layout_marginVertical="8dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="8dp">


        <RadioButton
            android:id="@+id/taskRadio"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RadioButton"
            android:padding="10dp"/>
    </RelativeLayout>


</androidx.cardview.widget.CardView>

if anyone wants to look complete project code please follow this link: https://github.com/S-aurav/ToDo_App_SQLite

0

There are 0 best solutions below