Skip to content

Instantly share code, notes, and snippets.

@ogaclejapan
Last active September 27, 2016 07:43
Show Gist options
  • Save ogaclejapan/57173ab483e176da078b to your computer and use it in GitHub Desktop.
Save ogaclejapan/57173ab483e176da078b to your computer and use it in GitHub Desktop.
Sample of SmartTabLayout Issue #2
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.ogaclejapan.smarttablayout.SmartTabLayout
android:id="@+id/viewpagertab"
android:layout_width="match_parent"
android:layout_height="48dp"
app:stl_indicatorColor="#40C4FF"
app:stl_indicatorThickness="4dp"
app:stl_indicatorCornerRadius="2dp"
app:stl_underlineColor="#4D000000"
app:stl_underlineThickness="1dp"
app:stl_dividerColor="#4D000000"
app:stl_dividerThickness="1dp"
app:stl_defaultTabTextAllCaps="true"
app:stl_defaultTabTextColor="#FC000000"
app:stl_defaultTabTextSize="12sp"
app:stl_defaultTabTextHorizontalPadding="16dp"
app:stl_defaultTabTextMinWidth="0dp"
app:stl_distributeEvenly="true"
/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
package com.ogaclejapan.myapplication;
import com.ogaclejapan.smarttablayout.SmartTabLayout;
import com.ogaclejapan.smarttablayout.utils.ViewPagerItem;
import com.ogaclejapan.smarttablayout.utils.ViewPagerItemAdapter;
import com.ogaclejapan.smarttablayout.utils.ViewPagerItems;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
public class MainActivity extends Activity {
private final ViewPager.OnPageChangeListener mPageChangeListener =
new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
View page = mAdapter.getPage(position);
if (page == null) {
showToast("page is null");
return;
}
RecyclerView cardList = (RecyclerView) page.findViewById(R.id.cardList);
if (cardList == null) {
showToast("cardList is null");
}
showToast("cardList is not null");
}
};
private ViewPagerItemAdapter mAdapter;
private Toast mToast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
SmartTabLayout viewPagerTab = (SmartTabLayout) findViewById(R.id.viewpagertab);
mAdapter = new ViewPagerItemAdapter(ViewPagerItems.with(this)
.add(new ViewPagerSampleItem("A", R.layout.page))
.add(new ViewPagerSampleItem("B", R.layout.page))
.add(new ViewPagerSampleItem("C", R.layout.page))
.add(new ViewPagerSampleItem("D", R.layout.page))
.create());
viewPager.setAdapter(mAdapter);
viewPagerTab.setViewPager(viewPager);
viewPagerTab.setOnPageChangeListener(mPageChangeListener);
}
private void showToast(String text) {
if (mToast != null) {
mToast.cancel();
}
mToast = Toast.makeText(this, text, Toast.LENGTH_LONG);
mToast.show();
}
private static class ViewPagerSampleItem extends ViewPagerItem {
private ViewPagerSampleItem(CharSequence title, int resource) {
super(title, DEFAULT_WIDTH, resource);
}
@Override
public View initiate(LayoutInflater inflater, ViewGroup container) {
View page = super.initiate(inflater, container);
RecyclerView cardList = (RecyclerView) page.findViewById(R.id.cardList);
cardList.setHasFixedSize(true);
GridLayoutManager llm = new GridLayoutManager(page.getContext(), 3);
cardList.setLayoutManager(llm);
return page;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/cardList"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</FrameLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment