How to Create the dynamic Fragment in Android Studio.
JSON Data Example:-
{
"menu": [
{
"cat_id": "246",
"cat_name": "A",
"description": "" },
{
"cat_id": "174",
"cat_name": "B ",
"description": "" },
{
"cat_id": "35",
"cat_name": "C ",
"description": "" },
{
"cat_id": "37",
"cat_name": "D ",
"description": "" }
]
}
// Make the Xml file here.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<android.support.design.widget.TabLayout
android:id="@+id/htab_tabs"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="bottom"
android:background="#fff"
app:tabGravity="fill"
app:tabMode="scrollable"
app:tabIndicatorColor="@android:color/black" />
<android.support.v4.view.ViewPager
android:id="@+id/htab_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
//Make the dynamic fragment.
public class MainActivity extends AppCompatActivity {
private TabLayout tabLayout;
private ViewPager viewPager;
// Fragment List
private final List<Fragment> mFragmentList = new ArrayList<>();
// Title List private final List<String> mFragmentTitleList = new ArrayList<>();
private final ArrayList<PageItem> pageItems = new ArrayList<>();
private ViewPagerAdapter viewPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.htab_viewpager);
initTitle();
// edited , you can use multiple titles and one Fragment
for (int i = 0; i < mFragmentTitleList.size(); i++) {
mFragmentList.add(new FragmentCard());
}
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.htab_tabs);
tabLayout.setupWithViewPager(viewPager);
// Tab ViewPager setting
viewPager.setOffscreenPageLimit(mFragmentList.size());
tabLayout.setupWithViewPager(viewPager);
tabLayout.setTabsFromPagerAdapter(viewPagerAdapter);
//using the click listener
listener();
//End }
private void listener () {
//Here we can get the value on tab click and swipe.
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override public void onTabSelected (TabLayout.Tab tab){
}
@Override public void onTabUnselected (TabLayout.Tab tab){
}
@Override public void onTabReselected (TabLayout.Tab tab){
}
});
}
/**
* load title data ,it muest below ViewPagerAdapter initialize
*/
private void initTitle() {
//Your Json Data
try {
JSONObject objectJson = new JSONObject(readJSONFromAsset());
JSONArray menu = objectJson.getJSONArray("menu");
for (int i = 0; i < menu.length(); i++) {
String cat_name = menu.getJSONObject(i).getString("cat_name");
String cat_id = menu.getJSONObject(i).getString("cat_id");
mFragmentTitleList.add(cat_name);
PageItem pageItem = new PageItem();
pageItem.setPageId(cat_id);
pageItems.add(pageItem);
}
} catch(
JSONException e)
{
e.printStackTrace();
}
}
private void setupViewPager(ViewPager viewPager) {
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), mFragmentList, mFragmentTitleList);
viewPager.setAdapter(viewPagerAdapter);
}
/**
* ViewPagerAdapter setting
*/
class ViewPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> mFragmentList = new ArrayList<>();
private List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager fm, List<Fragment> fragments, List<String> titleLists) {
super(fm);
this.mFragmentList = fragments;
this.mFragmentTitleList = titleLists;
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList == null ? 0 : mFragmentList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
// Here we are creating the static json response. You will get the data through json response
public String readJSONFromAsset() {
String json = null;
try {
InputStream is = getAssets().open("data.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
}
JSON Data Example:-
{
"menu": [
{
"cat_id": "246",
"cat_name": "A",
"description": "" },
{
"cat_id": "174",
"cat_name": "B ",
"description": "" },
{
"cat_id": "35",
"cat_name": "C ",
"description": "" },
{
"cat_id": "37",
"cat_name": "D ",
"description": "" }
]
}
// Make the Xml file here.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<android.support.design.widget.TabLayout
android:id="@+id/htab_tabs"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="bottom"
android:background="#fff"
app:tabGravity="fill"
app:tabMode="scrollable"
app:tabIndicatorColor="@android:color/black" />
<android.support.v4.view.ViewPager
android:id="@+id/htab_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
//Make the dynamic fragment.
public class MainActivity extends AppCompatActivity {
private TabLayout tabLayout;
private ViewPager viewPager;
// Fragment List
private final List<Fragment> mFragmentList = new ArrayList<>();
// Title List private final List<String> mFragmentTitleList = new ArrayList<>();
private final ArrayList<PageItem> pageItems = new ArrayList<>();
private ViewPagerAdapter viewPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.htab_viewpager);
initTitle();
// edited , you can use multiple titles and one Fragment
for (int i = 0; i < mFragmentTitleList.size(); i++) {
mFragmentList.add(new FragmentCard());
}
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.htab_tabs);
tabLayout.setupWithViewPager(viewPager);
// Tab ViewPager setting
viewPager.setOffscreenPageLimit(mFragmentList.size());
tabLayout.setupWithViewPager(viewPager);
tabLayout.setTabsFromPagerAdapter(viewPagerAdapter);
//using the click listener
listener();
//End }
private void listener () {
//Here we can get the value on tab click and swipe.
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override public void onTabSelected (TabLayout.Tab tab){
}
@Override public void onTabUnselected (TabLayout.Tab tab){
}
@Override public void onTabReselected (TabLayout.Tab tab){
}
});
}
/**
* load title data ,it muest below ViewPagerAdapter initialize
*/
private void initTitle() {
//Your Json Data
try {
JSONObject objectJson = new JSONObject(readJSONFromAsset());
JSONArray menu = objectJson.getJSONArray("menu");
for (int i = 0; i < menu.length(); i++) {
String cat_name = menu.getJSONObject(i).getString("cat_name");
String cat_id = menu.getJSONObject(i).getString("cat_id");
mFragmentTitleList.add(cat_name);
PageItem pageItem = new PageItem();
pageItem.setPageId(cat_id);
pageItems.add(pageItem);
}
} catch(
JSONException e)
{
e.printStackTrace();
}
}
private void setupViewPager(ViewPager viewPager) {
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), mFragmentList, mFragmentTitleList);
viewPager.setAdapter(viewPagerAdapter);
}
/**
* ViewPagerAdapter setting
*/
class ViewPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> mFragmentList = new ArrayList<>();
private List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager fm, List<Fragment> fragments, List<String> titleLists) {
super(fm);
this.mFragmentList = fragments;
this.mFragmentTitleList = titleLists;
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList == null ? 0 : mFragmentList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
// Here we are creating the static json response. You will get the data through json response
public String readJSONFromAsset() {
String json = null;
try {
InputStream is = getAssets().open("data.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
}
No comments:
Post a Comment