Fragment di Android Studio
hello temen2,
yuk kita belajar fragment di android studio...
pertama kita buat konfigurasikan mainactivity sebagai berikut...
package com.example.cobafragment;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getSupportFragmentManager();
HomeFragment homeFragment = new HomeFragment();
Fragment fragment = fragmentManager.findFragmentByTag(HomeFragment.class.getSimpleName());
if (!(fragment instanceof HomeFragment)){
Log.d("Ini fragment", "Fragment :" + HomeFragment.class.getSimpleName());
fragmentManager
.beginTransaction()
.add(R.id.frame_container, homeFragment, HomeFragment.class.getSimpleName())
.commit();
}
}
}
dan buat layout nya seperti berikut..
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
kedua, kita buat fragment homenya...
package com.example.cobafragment;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
* A simple {@link Fragment} subclass.
*/
public class HomeFragment extends Fragment implements View.OnClickListener{
public HomeFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button btnFragment = view.findViewById(R.id.btn_fragment);
btnFragment.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn_fragment){
NewFragment newFragment = new NewFragment();
FragmentManager fragmentManager = getFragmentManager();
if (fragmentManager != null) {
fragmentManager
.beginTransaction()
.replace(R.id.frame_container, newFragment, NewFragment.class.getSimpleName())
.addToBackStack(null)
.commit();
}
}
}
}
lalu buat layout untuk fragment homenya..
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".HomeFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:textSize="20sp"
android:text="Ini Fragment Home" />
<Button
android:id="@+id/btn_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:text="Ini Fragment Baru"/>
</LinearLayout>
ketiga, buat fragment new,
package com.example.cobafragment;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class NewFragment extends Fragment {
public NewFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_new, container, false);
}
}
dan buat layoutnya seperti berikut...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".NewFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Ini Fragment Baru" />
</LinearLayout>
dan selesai, sekarang projekmu bisa dijalankan...
dan lihat hasilnya akan seperti ini..
jika di klik, maka akan ganti fragment berikut...
Komentar
Posting Komentar