Content of tabs not displaying - android

After failing to stop reloading the fragments on tab change by using fragment manager, I implemented it using viewpager.
But the content doesn't display in the tabs. Everything seems to be correct. I have been trying for hours, and unable to figure out what the problem is.
This is the code for my activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.audit_form_activity);
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
button = (Button) findViewById(R.id.button);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setOffscreenPageLimit(no_of_categories);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener()
{
#Override
public void onTabSelected (TabLayout.Tab tab){
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected (TabLayout.Tab tab){
}
#Override
public void onTabReselected (TabLayout.Tab tab){
}
});
}
private void setupViewPager(ViewPager viewPager) {
// Outside Loop
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
// Get the JSON Data from DataSource
try {
JSONObject pageObj = new JSONObject(page);
JSONArray auditQuestionsArray = pageObj.getJSONArray("AuditQuestions");
int auditQuestionsArrayLength = auditQuestionsArray.length();
no_of_categories = auditQuestionsArrayLength;
// Outer-Loop Through the CategoryNames to Create Fragments for each CategoryName.
for (int i = 0; i < no_of_categories; i++) {
JSONObject auditQuestionsArrayObject = auditQuestionsArray.getJSONObject(i);
category = auditQuestionsArrayObject.getString("categoryName");
//JSONArray questionListArray = auditQuestionsArrayObject.getJSONArray("QuestionList");
Tab1 myFrag = Tab1.newInstance(category);
adapter.addFrag(myFrag, category);
}
} catch (JSONException e) {
e.printStackTrace();
}
viewPager.setAdapter(adapter);
}
public void bringBackBoolean(Boolean booleanReceived) {
//bList.add(booleanReceived);
}
This is the code for my pager adapter which is the inner class in the earlier activity:
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
Please help me figure out the problem.

Related

how make Sliding tabs make clickable in Android Development?

I have made a sliding tabs feature which is currently working. I also want to make it clickable
public class MainActivity extends AppCompatActivity {
public List<String> datafragmnt = new ArrayList<String>();
public ViewPager viewPager;
public pagerfragmetAdapter myAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
datafragmnt.add("Frgmnt 1 ");
datafragmnt.add("Frgmnt 2 ");
datafragmnt.add("Frgmnt 3 ");
datafragmnt.add("Frgmnt 4 ");
myAdapter = new pagerfragmetAdapter(getSupportFragmentManager());
myAdapter.setMydata(datafragmnt);
myAdapter.setFragmntList(new fragment1());
myAdapter.setFragmntList(new fragment2());
myAdapter.setFragmntList(new fragment3());
myAdapter.setFragmntList(new fragment4());
viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPager.setAdapter(myAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
}
This is what I have tried so far. Any help is welcome!
Please help me!
You Need to create an Adapter for that view page,something like this:
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
And in the SetUpViewPager you add your fragments like this:
private void setupViewPager(ViewPager viewPager) {
Main.ViewPagerAdapter adapter = new Main.ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new fragment1(), "FRAGN+MENT UNO");

Contents of all tabs disappear after navigating to a page other than page with tabs and coming back

I have code that creates tabs dynamically.
This is my MyActivity.java:
public class MyActivity extends AppCompatActivity {
.
.
.
.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.audit_form_activity);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
try {
JSONObject jsonObj = new JSONObject(page);
JSONArray jssArray = jsonObj.getJSONArray("AuditQuestions");
int jsonArrLength = jssArray.length();
no_of_categories = jsonArrLength;
for (int i = 0; i < jsonArrLength; i++) {
JSONObject jsonChildObj = jssArray.getJSONObject(i);
categoryName.add(jsonChildObj.getString("categoryName"));
}
} catch (JSONException e) {
e.printStackTrace();
}
viewPager = (ViewPager) findViewById(R.id.pager);
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setOffscreenPageLimit(no_of_categories);
setupViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(onTabSelectedListener(viewPager));
}
private void setupViewPager(ViewPager viewPager) {
Pager adapter = new Pager(getSupportFragmentManager());
for (int i = 0; i < no_of_categories; i++) {
Tab1 tab1 = new Tab1();
adapter.addFragment(tab1, categoryName.get(i));
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
onTabSelectedListener(viewPager);
}
}
private TabLayout.OnTabSelectedListener onTabSelectedListener(final ViewPager pager) {
return new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
pager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
};
}
}
This is my adapter Pager.java:
public class Pager extends FragmentStatePagerAdapter {
int mNumOfTabs;
Fragment fragment = null;
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public Pager(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
On removing this line from MyActivity.java , the content is there in tabs even after navigating away from tabs page and coming back:
viewPager.setOffscreenPageLimit(no_of_categories);
But it doesn't solve the issue because the contents displayed in the tabs are inconsistent, i.e, contents that are supposed to appear in one tab appears on other tab.
I tried to solve it but with no success. May be it has to do with the life cycle of fragments?
How do I solve this?
Not able to find any solution for this. Please help.
Any help will be appreciated.
Thanks in advance!

Dynamic Tab containing List View Showing Adjacent Tab List

I'm having one parent Fragment whose class is Scrollbaltab which contains many dynamic child Fragment whose class is Fragment1 but when I'm scrolling tab "two" it shows tab "one" data and when scroll to tab "three" it shows tab "two" data please help me
//Dynamic Fragment
public class Fragment1 extends Fragment {
RecyclerViewAdapter recyclerViewAdapter;
HashMap<String, ArrayList<String>> item = new HashMap<String,ArrayList<String>>();
ArrayList onlineData1 = new ArrayList<>();
ArrayList onlineData2 = new ArrayList<>();
ArrayList onlineData3 = new ArrayList<>();
String titleName="";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inflateView = inflater.inflate(R.layout.fragment_fragment1, null, false);
RecyclerView recyclerView = (RecyclerView) inflateView.findViewById(R.id.recyclerView);
recyclerView.removeAllViews();
recyclerViewAdapter = new RecyclerViewAdapter(this.getActivity() ,item.get(titleName));
recyclerView.setAdapter(recyclerViewAdapter);
recyclerViewAdapter.notifyDataSetChanged();
recyclerView.setHasFixedSize(true);
//Layout manager for Recycler view
recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity() ));
recyclerViewAdapter.notifyDataSetChanged();
return inflateView;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int tab_position = Scrollbaltab.pos;
if(onlineData1.isEmpty())
{
onlineData1.clear();
}
if(onlineData2.isEmpty())
{
onlineData2.clear();
}
if(onlineData3.isEmpty())
{
onlineData3.clear();
}
item.clear();
onlineData1.add("one");
onlineData1.add("two");
onlineData1.add("three");
onlineData2.add("a");
onlineData2.add("b");
onlineData2.add("c");
onlineData3.add("x");
onlineData3.add("y");
onlineData3.add("z");
if(item.get("ONE")==null)
item.put("ONE",onlineData1);
if(item.get("TWO")==null)
item.put("TWO",onlineData2);
if(item.get("THREE")==null)
item.put("THREE",onlineData3);
String [] arr= new String[]{"ONE","TWO","THREE"};
if(tab_position>=0) {
titleName = arr[tab_position];
}
// set adpter
Log.d("values"+tab_position,item.get(titleName).toString());
}
}
//parent Fragment
public class Scrollbaltab extends AppCompatActivity {
Toolbar toolbar;
ViewPager viewPager;
TabLayout tabLayout;
ViewPagerAdapter adapter;
public static int pos=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrollbaltab);
inlizeview();
}
private void inlizeview() {
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("scrolling tabbar");
setSupportActionBar(toolbar);
//both are use for set arrow back button in toolbar
// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
// when click on arrow button of tootbar it finish the current activity
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
pos=position;
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
}
private void setupViewPager(ViewPager viewPager) {
adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new Fragment1(), "ONE");
adapter.addFragment(new Fragment1(), "TWO");
adapter.addFragment(new Fragment1(), "THREE");
viewPager.setAdapter(adapter);
}
}
//view pager adapter
public final List<Fragment> mFragmentList = new ArrayList<>();
public final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
Log.e("DD","mFragmentList--"+mFragmentList.get(position));
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
#Override
public int getItemPosition(Object object){
return ViewPagerAdapter.POSITION_NONE;
}
}

Android How to add Tabs dynamically from json using viewpager

How to add Tabs dynamically from json using viewpager at load page on Mainactivity and show corresponding data on gridview.
I already try this ,just static method.
can i add dynamically tabs from json? and it's data corresponding.
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
if (viewPager != null) {
setupViewPager(viewPager);
}
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
ViewPager viewPager1 = (ViewPager) findViewById(R.id.viewpager1);
if (viewPager1 != null) {
setupViewPager1(viewPager1);
}
TabLayout tabLayout1 = (TabLayout) findViewById(R.id.tabs1);
tabLayout1.setupWithViewPager(viewPager1);
}
public void setTabFragmentTwo(String t){
TabFragmentTwo = t;
}
public String getTabFragmentTwo(){
return TabFragmentTwo;
}
private void setupViewPager(ViewPager viewPager) {
Adapter adapter = new Adapter(getSupportFragmentManager());
adapter.addFragment(new OneFragment(), "Category 1");
adapter.addFragment(new TwoFragment(), "Category 2");
adapter.addFragment(new ThreeFragment(), "Category 3");
adapter.addFragment(new ThreeFragment(), "Category 3");
adapter.addFragment(new ThreeFragment(), "Category 3");
adapter.addFragment(new ThreeFragment(), "Category 3");
viewPager.setAdapter(adapter);
}
static class Adapter extends FragmentPagerAdapter {
private final List<Fragment> mFragments = new ArrayList<>();
private final List<String> mFragmentTitles = new ArrayList<>();
public Adapter(FragmentManager fm) {
super(fm);
}
public void addFragment(Fragment fragment, String title) {
mFragments.add(fragment);
mFragmentTitles.add(title);
}
#Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
#Override
public int getCount() {
return mFragments.size();
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitles.get(position);
}
}
private void setupViewPager1(ViewPager viewPager) {
Adapter1 adapter1 = new Adapter1(getSupportFragmentManager());
adapter1.addFragment(new Fragment_quick_bill(), "Quick Bill");
adapter1.addFragment(new Fragment_addvnced_bill(), "Addvanc-Bil");
viewPager.setAdapter(adapter1);
}
static class Adapter1 extends FragmentPagerAdapter {
private final List<Fragment> mFragments = new ArrayList<>();
private final List<String> mFragmentTitles = new ArrayList<>();
public Adapter1(FragmentManager fm) {
super(fm);
}
public void addFragment(Fragment fragment, String title) {
mFragments.add(fragment);
mFragmentTitles.add(title);
}
#Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
#Override
public int getCount() {
return mFragments.size();
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitles.get(position);
}
}
You can easily add tabs dynamically:
private void addTab(String title) {
mTabLayout.addTab(mTabLayout.newTab().setText(title));
mPagerAdapter.addTabPage(title);
}
and addTabPage method to your PagerAdapter(tabItems are item list).
public void addTabPage(String title) {
tabItems.add(title);
notifyDataSetChanged();
}
// complete class(check how using for loop I am able to add 3 tabs dynamically now you can call above mentioned method wherever you need like getting category list from api or using button click etc.)
public class MainActivity extends AppCompatActivity {
TabLayout mTabLayout;
ViewPager mViewPager;
Adapter mPagerAdapter;
List<String> tabItems = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = (ViewPager) findViewById(R.id.viewpager);
mTabLayout = (TabLayout) findViewById(R.id.tabs);
mPagerAdapter = new Adapter(getSupportFragmentManager());
mViewPager.setAdapter(mPagerAdapter);
for(int i=1; i<=3; i++){
addTab("Tab"+ i);
}
mTabLayout.setupWithViewPager(mViewPager);
}
private void addTab(String title) {
mTabLayout.addTab(mTabLayout.newTab().setText(title));
addTabPage(title);
}
public void addTabPage(String title) {
tabItems.add(title);
mPagerAdapter.notifyDataSetChanged();
}
class Adapter extends FragmentPagerAdapter {
public Adapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return OneFragment.newInstance("param1","param2");
}
#Override
public int getCount() {
return tabItems.size();
}
#Override
public CharSequence getPageTitle(int position) {
return tabItems.get(position);
}
}
}

Update fragments - by swipe from MainActivity

I have main_activity with fragments. Main Activity contains swiperefreshlayout overlaying viewerpager. After swiping new data is downloaded, now I want to refresh it on every fragment. How can I achieve that?
My code:
public class MainActivity extends AppCompatActivity {
object simple_object;
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
SwipeRefreshLayout mSwipeRefreshLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout1);
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
// Refresh items
refreshItems(); //refreshing simple object
}
});
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new main_tab1(simple_object), "Tab1");
adapter.addFragment(new main_tab2(simple_object), "Tab2");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}

Categories

Resources