Skip to content

Instantly share code, notes, and snippets.

@ogaclejapan
Created April 5, 2015 03:38
Show Gist options
  • Save ogaclejapan/ea54259895761e3f4bbe to your computer and use it in GitHub Desktop.
Save ogaclejapan/ea54259895761e3f4bbe to your computer and use it in GitHub Desktop.
Sample to set the argument to the Fragment of SmartTabLayout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
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_underlineThickness="0dp"
app:stl_dividerThickness="0dp"
app:stl_defaultTabTextColor="#FC000000"
app:stl_defaultTabTextSize="12sp"
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.v4.FragmentPagerItem;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItemAdapter;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItems;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// One fragment will change by the parameter
FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(
getSupportFragmentManager(), FragmentPagerItems.with(this)
.add(FragmentPagerItem.of("title1", PageFragment.class, PageFragment.arguments("param:page1")))
.add(FragmentPagerItem.of("title2", PageFragment.class, PageFragment.arguments("param:page2")))
.add(FragmentPagerItem.of("title3", PageFragment.class, PageFragment.arguments("param:page3")))
.create());
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(adapter);
SmartTabLayout viewPagerTab = (SmartTabLayout) findViewById(R.id.viewpagertab);
viewPagerTab.setViewPager(viewPager);
}
}
<?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">
<TextView
android:id="@+id/page_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="16sp"
/>
</FrameLayout>
package com.ogaclejapan.myapplication;
import com.ogaclejapan.smarttablayout.utils.v4.Bundler;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class PageFragment extends Fragment {
private static final String KEY_PARAM = "key_param";
public static PageFragment newInstance(String param) {
PageFragment f = new PageFragment();
f.setArguments(arguments(param));
return f;
}
public static Bundle arguments(String param) {
return new Bundler()
.putString(KEY_PARAM, param)
.get();
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.page, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
String param = getArguments().getString(KEY_PARAM);
TextView pageText = (TextView) view.findViewById(R.id.page_text);
pageText.setText(param);
}
}
@regitroy
Copy link

error in viewPager.setAdapter(adapter);
adapter cannot be applied

@Bhushan46
Copy link

thank you so much brother. I kept thinking about instances and you gave me this . saved my day brother.

@bagherhasani
Copy link

i really really appreciate it
i was looking for exactly example like this
i wish it worked thank you very much

@yasdeveloper
Copy link

best

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment