Wednesday, December 13, 2023

Android Studio Hedgehog | 2023.1.1: Java RecyclerView & Fragment

Android Studio Hedgehog | 2023.1.1

Note: For personal reference using RecyclerView and Fragment

Create new Project (No Activity)

Name Recycler View Tutorial
Package Name com.dedetok.recyclerviewtutorial

Create Activity -> Empty View Activity

Activity Name: Main Activity
Layout Name activity_main
Laucher Activity checked

build.gradle.kts(Module :app)

plugins {
    id("com.android.application")
}

android {
    namespace = "com.dedetok.recyclerviewtutorial"
    compileSdk = 34

    defaultConfig {
        applicationId = "com.dedetok.recyclerviewtutorial"
        minSdk = 24
        targetSdk = 34
        versionCode = 1
        versionName = "1.0"
,,,

Layout

activity_main.xml

<?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"
    tools:context=".MainActivity">

    <androidx.fragment.app.FragmentContainerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id = "@+id/frag_container"/>
</androidx.constraintlayout.widget.ConstraintLayout>

my_single_column.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="72dp"
    android:orientation="vertical"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/first_column" />

</androidx.appcompat.widget.LinearLayoutCompat>

 my_recycler_view_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

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

</androidx.appcompat.widget.LinearLayoutCompat>

Java

MyAdapter.java

package com.dedetok.recyclerviewtutorial;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class MyAdapter extends RecyclerView.Adapter {

    private String[] mDataSet;

    // constructor
    public MyAdapter(String[] mDataSet) {
        this.mDataSet = mDataSet;
    }

    @NonNull
    @Override
    //public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { // Original
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // Create a new view.
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.my_single_column, parent, false);
        return new MyViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ((MyViewHolder) holder).getTextView().setText(mDataSet[position]);
    }


    @Override
    public int getItemCount() {
        return mDataSet.length;
    }

    ///////////////////
    /*
     * class MyViewHolder extends from  RecyclerView.ViewHolder {
     */
    private class MyViewHolder extends RecyclerView.ViewHolder {
        private final TextView textView;

        public MyViewHolder(View v) {
            super(v);
            v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    System.out.println("Element " + getAdapterPosition() + " clicked.");
                }
            });
            textView = (TextView) v.findViewById(R.id.first_column);
        }

        public TextView getTextView() {
            return textView;
        }
    }

    //////////////////


}

RecyclerViewFragment.java

package com.dedetok.recyclerviewtutorial;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class RecyclerViewFragment extends Fragment {

    // initial data
    String[] mDataSet  = {"ace", "boom", "crew", "dog", "eon"};
    protected RecyclerView mRecyclerView;
    protected MyAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.my_recycler_view_layout, container, false); // conteain androidx.recyclerview.widget.RecyclerView

        // get androidx.recyclerview.widget.RecyclerView
        mRecyclerView = rootView.findViewById(R.id.r_view_main_layout);

        // Define Layout for androidx.recyclerview.widget.RecyclerView
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(mLayoutManager);

        mAdapter = new MyAdapter(mDataSet);
        // Set CustomAdapter as the adapter for RecyclerView.
        mRecyclerView.setAdapter(mAdapter);
        // END_INCLUDE(initializeRecyclerView)


        return rootView;
    }

}

MainActivity.java

package com.dedetok.recyclerviewtutorial;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            RecyclerViewFragment fragment = new RecyclerViewFragment();
            transaction.replace(R.id.frag_container, fragment);
            transaction.commit();
        }
    }
}


No comments:

Post a Comment