Beranda Pemrograman CodePaste Android create tab fragment / viewpager PemrogramanCodePasteJava android studioUncategorized Android create tab fragment / viewpager Penulis ariefendi Java fragment1.java import android.os.Bundle; import android.os.Handler; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.ListView; import android.widget.ProgressBar; import androidx.fragment.app.Fragment; import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import java.util.ArrayList; import java.util.HashMap; public class fragment1 extends Fragment { public void fragment1(){ } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View dp = inflater.inflate(R.layout.layout, container, false); return dp; } } Java fragment2.java import android.os.Bundle; import android.os.Handler; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.ListView; import android.widget.ProgressBar; import androidx.fragment.app.Fragment; import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import java.util.ArrayList; import java.util.HashMap; public class fragment2 extends Fragment { public void fragment2(){ } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View dp = inflater.inflate(R.layout.layout, container, false); return dp; } } Java Hs_pager.java import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentStatePagerAdapter; public class Hs_pager extends FragmentStatePagerAdapter { private int number_tabs; public Hs_pager(FragmentManager fm, int number_tabs) { super(fm); this.number_tabs = number_tabs; } @Override public Fragment getItem(int position) { switch (position){ case 0: return new fragment1(); case 1: return new fragment2(); default: return null; } } @Override public int getCount() { return number_tabs; } } XML layout.xml <com.google.android.material.tabs.TabLayout android:layout_below="@id/headbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tab_layout_hs" app:tabTextColor="@color/text_color" app:tabMode="fixed"> <com.google.android.material.tabs.TabItem android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/his_depo" android:text="Deposit"/> <com.google.android.material.tabs.TabItem android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/his_transfer" android:text="Transfer"/> </com.google.android.material.tabs.TabLayout> <androidx.viewpager.widget.ViewPager android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/pager_ct" android:layout_below="@id/tab_layout_hs"/> Java MainActivity.java TabLayout tab = findViewById(R.id.tab_layout_hs); final ViewPager pg = findViewById(R.id.pager_ct); Hs_pager pagerAdapter = new Hs_pager(getSupportFragmentManager(), tab.getTabCount()); pg.setAdapter(pagerAdapter); pg.setOffscreenPageLimit(0); pg.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tab)); tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { pg.setCurrentItem(tab.getPosition()); } @Override public void onTabUnselected(TabLayout.Tab tab) { /* */ } @Override public void onTabReselected(TabLayout.Tab tab) { /* */ } }); BERITA TERKAITDARI PENULIS Android Example retrofit upload file CodePaste Php auto create mail on cpanel whm, Php membuat email otomatis di cpanel whm CodePaste Auto create subdomain php cpanel, Membuat subdomain otomatis di cpanel whm Android Java android studio generate qr CodePaste PHP write text to image, Add text to image