Created
April 2, 2019 17:59
-
-
Save senamit2708/00a3a70e21b1e7cba764aa36381cf58a to your computer and use it in GitHub Desktop.
page tab in android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://android.jlelse.eu/tablayout-and-viewpager-in-your-android-app-738b8840c38a | |
https://developer.android.com/reference/com/google/android/material/tabs/TabLayout | |
https://material.io/develop/android/components/tab-layout/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"> | |
<com.google.android.material.tabs.TabLayout | |
android:id="@+id/tabs" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent"> | |
<com.google.android.material.tabs.TabItem | |
android:icon="@drawable/ic_shopping_cart_one" | |
android:text="@string/tab_a_label" | |
android:layout_height="match_parent" | |
android:layout_width="wrap_content" /> | |
<com.google.android.material.tabs.TabItem | |
android:icon="@drawable/ic_shopping_cart_two" | |
android:text="@string/tab_b_label" | |
android:layout_height="match_parent" | |
android:layout_width="wrap_content" /> | |
<com.google.android.material.tabs.TabItem | |
android:icon="@drawable/ic_delete_one" | |
android:text="@string/tab_c_label" | |
android:layout_height="match_parent" | |
android:layout_width="wrap_content" /> | |
</com.google.android.material.tabs.TabLayout> | |
<androidx.viewpager.widget.ViewPager | |
android:id="@+id/viewPager" | |
android:layout_width="match_parent" | |
android:layout_height="0dp" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toBottomOf="@+id/tabs" | |
app:layout_constraintVertical_bias="0.0"></androidx.viewpager.widget.ViewPager> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package smit.aen.tuktukone.adapter; | |
import android.content.Context; | |
import androidx.annotation.Nullable; | |
import androidx.fragment.app.Fragment; | |
import androidx.fragment.app.FragmentManager; | |
import androidx.fragment.app.FragmentPagerAdapter; | |
import smit.aen.tuktukone.R; | |
import smit.aen.tuktukone.fragments.TokenListCloseFrag; | |
import smit.aen.tuktukone.fragments.TokenListCompFrag; | |
import smit.aen.tuktukone.fragments.TokenListFrag; | |
public class SimpleFragPagerAdap extends FragmentPagerAdapter { | |
private static final String TAG = SimpleFragPagerAdap.class.getSimpleName(); | |
private Context mContext; | |
private int tabCount = 3; | |
public SimpleFragPagerAdap(Context context, FragmentManager fm) { | |
super(fm); | |
mContext = context; | |
} | |
@Override | |
public Fragment getItem(int position) { | |
if (position==0){ | |
return new TokenListFrag(); | |
}else if (position==1){ | |
return new TokenListCompFrag(); | |
}else if (position==2){ | |
return new TokenListCloseFrag(); | |
}else { | |
return null; | |
} | |
} | |
@Override | |
public int getCount() { | |
return tabCount; | |
} | |
@Nullable | |
@Override | |
public CharSequence getPageTitle(int position) { | |
switch (position){ | |
case 0: | |
return mContext.getString(R.string.tab_a_label); | |
case 1: | |
return mContext.getString(R.string.tab_b_label); | |
case 2: | |
return mContext.getString(R.string.tab_c_label); | |
default: | |
return null; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package smit.aen.tuktukone.fragments; | |
import android.content.Context; | |
import android.os.Bundle; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import com.google.android.material.tabs.TabLayout; | |
import java.util.Objects; | |
import androidx.annotation.NonNull; | |
import androidx.annotation.Nullable; | |
import androidx.fragment.app.Fragment; | |
import androidx.viewpager.widget.ViewPager; | |
import smit.aen.tuktukone.NavMainActivity; | |
import smit.aen.tuktukone.R; | |
import smit.aen.tuktukone.adapter.SimpleFragPagerAdap; | |
public class TokenTabTestFrag extends Fragment { | |
private static final String TAG = TokenTabTestFrag.class.getSimpleName(); | |
private Context context; | |
private ViewPager mViewPager; | |
private SimpleFragPagerAdap mAdapter; | |
private TabLayout tabLayout; | |
@Override | |
public void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
} | |
@Nullable | |
@Override | |
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { | |
View view = inflater.inflate(R.layout.af_token_tab_test, container, false); | |
context = container.getContext(); | |
return view; | |
} | |
@Override | |
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { | |
super.onViewCreated(view, savedInstanceState); | |
bindView(view); | |
tabLayout.setupWithViewPager(mViewPager); | |
} | |
private void bindView(View view) { | |
mViewPager = view.findViewById(R.id.viewPager); | |
mAdapter = new SimpleFragPagerAdap(context, getActivity().getSupportFragmentManager()); | |
mViewPager.setAdapter(mAdapter); | |
tabLayout = view.findViewById(R.id.tabs); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment