How to set Tabview inside the fragment in android - android

I am new to android.I want to design a page in android which contains a Tab view inside the fragment .
Fragment :
For that I have a Fragment named fragment_home.xml .
Layout Resource files for tabs:
And three layout resource file named
today.xml
this-week.xml
this_month.xml
and java classes for the resource file is
Today.java
ThisWeek.java
Thismonth.java
What is my doubt is how can I implement the tab view inside the fragment .I have referred some SO files but nothing is helped for me .
Home.java (Fragment):
public class Home extends Fragment{
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
private TabLayout tabLayout;
private ViewPager viewPager;
private View rootView;
private OnFragmentInteractionListener mListener;
public Home() {
}
public static Home newInstance(String param1, String param2) {
Home fragment = new Home();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_home, container, false);
tabLayout = (TabLayout) rootView.findViewById(R.id.tabLayout);
viewPager = (ViewPager) rootView.findViewById(R.id.pager);
setupViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
return rootView;
}
public void setupViewPager(ViewPager viewPager) {
Pager adapter = new Pager(getChildFragmentManager());
adapter.addFragment(new Today(), "Today");
adapter.addFragment(new Reminders(), "Reminders");
adapter.addFragment(new Settings(), "Settings");
viewPager.setAdapter(adapter);
}
public static 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);
}
}
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
void onFragmentInteraction(Uri uri);
}
}
Here I want to implement something but I don't know ,How to ?
Please help me to get this.
fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/home_Fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragments.Home">
<android.support.design.widget.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"/>
</FrameLayout>
PagerAdapter
public class Pager extends FragmentStatePagerAdapter {
int tabCount;
public Pager(FragmentManager fragmentManager, int tabCount) {
super(fragmentManager);
this.tabCount= tabCount;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Today todayObj = new Today();
return todayObj;
case 1:
ThisWeek thisWeekObj = new ThisWeek();
return thisWeekObj;
case 2:
ThisMonth thisMonthObj = new ThisMonth();
return thisMonthObj;
default:
return null;
}
}
#Override
public int getCount() {
return tabCount;
}
public void addFragment(Today today, String today1) {
}
public void addFragment(Reminders reminders, String reminders1) {
}
public void addFragment(Settings settings, String settings1) {
}
}

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
public class Home extends Fragment{
private TabLayout tabLayout;
private ViewPager viewPager;
private View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_home, container, false);
tabLayout = (TabLayout) rootView.findViewById(R.id.tabLayout);
viewPager = (ViewPager) rootView.findViewById(R.id.pager);
setupViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
return rootView;
}
public void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
adapter.addFragment(new FragmentOne(), "Fragment name one"); //Need to call first tab fragment
adapter.addFragment(new FragmentTwo(), "Fragment name two"); // Need second tab fragment and so on...
viewPager.setAdapter(adapter);
}
public static 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);
}
}
}
FragmentOne.class
public class FragmentOne extends Fragment {
private View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_one, container, false);
return rootView;
}
}
fragment_one.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"
android:background="#color/plainWhite"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Fragment One Showing"
android:gravity="center"
android:textColor="#ffffff"
android:padding="4dp"
android:textSize="18sp"/>
</LinearLayout>
</FrameLayout>
FragmentTwo.class
public class FragmentTwo extends Fragment {
private View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_two, container, false);
return rootView;
}
}
fragment_two.xml
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"
android:background="#color/plainWhite"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Fragment Two Showing"
android:gravity="center"
android:textColor="#ffffff"
android:padding="4dp"
android:textSize="18sp"/>
</LinearLayout>
</FrameLayout>
use this code , this is the complete example of tablayout using view pager

Related

Why fragment is not added when implemented Interface for communication in Tablayout

I used tabLayout having fragments in it. When I call the Fragment using interface from activity.
I Can't access Listview instance and getActivity() too both producing null reference and I tried
testing using the isAdded method , the fragment itself not added when called from interface.
PrescriptionHistoryFragment called From PrescriptionActivity
ListView listView;
CustomAdapter adapter;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View retView = inflater.inflate(R.layout.fragment_history, container, false);
if (retView != null) {
listView = retView.findViewById(R.id.listView);
adapter = new CustomAdapter;
}
return retView;
}
InterFace implemented in Fragment. Cant access here the listview and also not
added to the activity
#Override
public void onInputPatient(int patientID) {
Log.d("Patient", "onInputPatient: Recieved" + patientID );
if(isAdded()){
Log.d("Tag", "onInputPatient:Attached ");
}else {
Log.d("Tag", "onInputPatient: NotAdded"); // called
}
//accessing listview here also produces null reference error
listview.setAdapter(adapter);
}
Xml file used in fragment
<?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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".Activities.Activities.PrescriptionActivity">
<LinearLayout
android:id="#+id/history_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".Activities.Activities.PrescriptionActivity"
>
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:visibility="visible"
android:nestedScrollingEnabled="true"
/>
</LinearLayout>
</LinearLayout>
Activity
private void setupViewPager(ViewPager viewPager) {
adapter = new ViewPagerAdapter(getSupportFragmentManager());
PrescriptionFragment prescriptionFragment = new PrescriptionFragment();
prescription_historyFragment prescriptionFragment = new prescription_historyFragment();
adapter.addFragment(prescriptionFragment, "PRESCRIPTION");
adapter.addFragment(prescription_historyFragment, "HISTORY");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private 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);
}
public void removeFragment(Fragment fragment, int position) {
mFragmentList.remove(position);
mFragmentTitleList.remove(position);
}
}
#Override
public void onInputPatient(int patientID) {
prescription_historyFragment.onInputPatient(patientID);
}

Issue while changing fragment in ViewPager

I am trying to change/slide fragment on click of a button in ViewPager. My code works in one project but, not in current.
xml of activity to load fragment:
<?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:id="#+id/content_swipe"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="firebasetest.imranrana.android.firebasetest.SwipeActivity"
tools:showIn="#layout/activity_swipe"
android:orientation="vertical"
>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/viewPagerId"
>
</android.support.v4.view.ViewPager>
</LinearLayout>
activity:
public class SwipeActivity extends AppCompatActivity{
ViewPager viewPager;
Toolbar toolbar;
TabLayout tabLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_swipe);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().hide();
viewPager=(ViewPager)findViewById(R.id.viewPagerId);
setupViewPager(viewPager);
// tabLayout=(TabLayout)findViewById(R.id.tabLayoutId);
// tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new WebViewFragment()/*, "Main"*/);
adapter.addFragment(new MenuFragment()/*, "TWO"*/);
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentStatePagerAdapter {
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);
}*/
}
}
fragment of viewpager contains changing button:
public class MenuFragment extends Fragment {
Button funPageButton;
Button profilePageButton;
Fragment fragment;
FragmentTransaction fragmentTransaction;
public MenuFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view=inflater.inflate(R.layout.fragment_menu, container, false);
funPageButton=(Button)view.findViewById(R.id.funPageButtonId);
profilePageButton=(Button)view.findViewById(R.id.profileButtonId);
funPageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getActivity(),"test",Toast.LENGTH_LONG).show();
fragment=new TestFragment();
fragmentTransaction=getActivity().getSupportFragmentManager().beginTransaction(); ;
fragmentTransaction.replace(R.id.content_swipe,fragment).commit();
}
});
profilePageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getActivity(),"test 1",Toast.LENGTH_LONG).show();
fragment=new TestFragment1();
fragmentTransaction=getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.content_swipe,fragment).commit();
}
});
return view;
}
}
What am i doing wrong?

Tabs with in the Tabs in android studio

I just want to implements Tabs with in the Tabs. If it is possible than please share the code
See image below for reference
This is the answer of my question. I set the tabs with in the Tabs. Basically set the tab in fragment:
My java code:
public class SD extends Fragment {
View view;
PagerAdapter adapter;
public SD() {
// Required empty public constructor
}
private void setupViewPager(ViewPager viewPager) {
///Here we have to pass ChildFragmentManager instead of FragmentManager.
adapter = new PagerAdapter(getChildFragmentManager());
adapter.addFragment(new Sim1Detail(), "Sim 1");
adapter.addFragment(new Sim2Detail(), "Sim 2");
viewPager.setAdapter(adapter);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.sd, container, false);
super.onCreate(savedInstanceState);
// ButterKnife.bind(getActivity(), view);
final ViewPager viewPager = ButterKnife.findById(view, R.id.viewpager);
setupViewPager(viewPager);
TabLayout tabLayout = ButterKnife.findById(view, R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
return view;
}
#Override
public void onDestroyView() {
super.onDestroyView();
// ButterKnife.unbind(getActivity);
}
static class PagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragments = new ArrayList<>();
private final List<String> mFragmentTitles = new ArrayList<>();
public PagerAdapter(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);
}
}
}
<?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"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
here it is ..

Fragment null when call function viewpager

My Fragment class:
onCreatedView:
parentActivity = (MainActivity) getActivity();
parentView = inflater.inflate(R.layout.fragmentla, container, false);
mFragmentAdapter = new myFragmentAdapter(parentActivity.getSupportFragmentManager(), parentView.getContext());
// Get the ViewPager and set it's PagerAdapter so that it can display items
ViewPager viewPager = (ViewPager) parentView.findViewById(R.id.viewpager);
viewPager.setAdapter(mFragmentAdapter);
// Give the TabLayout the ViewPager
TabLayout tabLayout = (TabLayout) parentView.findViewById(R.id.tablayout);
tabLayout.setupWithViewPager(viewPager);
Thats my fragment (Blue border). My fragment contains toolbar, tablayout, and viewpager.
My FragmentAdapter class:
public class myFragmentAdapter extends FragmentStatePagerAdapter {
final int PAGE_COUNT = 2;
private String tabTitles[] = new String[] { "ITEM1", "ITEM2" };
private Context context;
public myFragmentAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
#Override
public int getCount() {
return 2;
}
#Override
public Fragment getItem(int position) {
if(position == 0) {
return FirstFragment.newInstance(position + 1);
} else {
return SecondFragment.newInstance(position + 1);
}
}
#Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
#Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
}
And this is my FirstFragment class:
public static FirstFragment newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
FirstFragment fragment = new FirstFragment();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARG_PAGE);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
parentView = inflater.inflate(R.layout.fragment_first_newest, container, false);
sometext = (TextView) parentView.findViewById(R.id.wi_my_text);
return parentView;
}
So, my question is: Everytime that I press my menu button in toolbar the textview should change, but this return null cause "onCreatedView" is not called anymore. I tried with this in Fragment class:
public boolean onOptionsItemSelected(MenuItem item){
...
Fragment mFragment = mFragmentAdapter.getItem(0);
if(mFragment instanceof FirstFragment){
((FirstFragment) mFragment).OnButtonPressed();
}
}
Then on my FirstFragment but still getting null exception:
public void OnButtonPressed() {
someText.setText("its working");
createLocationRequest();
}
UPDATED: MY XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:id="#+id/wi_my_text"
android:layout_height="24dp"
android:layout_gravity="center"
android:gravity="center"
android:textStyle="bold"
android:paddingEnd="5dp"
android:text="texttest"/>
</LinearLayout>

Android view pager not displaying Fragment when it is called again

I have a slider menu in my application, when I click on an item in the menu it displays the corresponding fragment. That fragmet has a tablayout and a viewpager. The Fragment xml is given below
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:background="#color/colorPrimary"
app:tabSelectedTextColor="#color/textColorPrimary"
app:tabTextColor="#color/colorAccent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"/>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_below="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
Below given is the code snippet of the fragment clicked on the slider menu
public class MyCoursesFragment extends Fragment {
private TabLayout tabLayout;
private ViewPager viewPager;
public MyCoursesFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());
adapter.addFragment(new ScheduledFragment(), "SCHEDULED");
adapter.addFragment(new LiveStreamingFragment(), "LIVE STREAMING");
adapter.addFragment(new UpcomingFragment(), "UPCOMING");
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);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("mlearning", "inside my course");
View rootView = inflater.inflate(R.layout.fragement_courses, container, false);
viewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) rootView.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
return rootView;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
#Override
public void onDetach() {
super.onDetach();
}
}
There are three tabs in the above fragment. The xml for each fragment contains a list view, the xml is given below.
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/lvList"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:backgroundTint="#888686" />
The code for the tablayout fragment is given below:
public class ScheduledFragment extends Fragment {
public ScheduledFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("mlearning", "inside scheduled");
View rootView = inflater.inflate(R.layout.fragment_scheduled, container, false);
ArrayList<CoursePojo> searchResults = GetSearchResults();
final ListView lv1 = (ListView) rootView.findViewById(R.id.lvList2);
lv1.setAdapter(new MyCustomCourseList(getActivity(), searchResults));
lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Object o = lv1.getItemAtPosition(position);
CoursePojo fullObject = (CoursePojo) o;
//Toast.makeText(getBaseContext(), "You have chosen: " + " " + fullObject.getId(), Toast.LENGTH_LONG).show();
}
});
return rootView;
}
private ArrayList<CoursePojo> GetSearchResults(){
ArrayList<CoursePojo> results = new ArrayList<CoursePojo>();CoursePojo coursePojo3=new CoursePojo();
coursePojo3.setName("Android");
coursePojo3.setDesc("First os was released without a phone ");
coursePojo3.setStartdate("20 Mar");
coursePojo3.setEndDate("8 Apr");
coursePojo3.setProgress("40");
results.add(coursePojo3);
// Inflate the layout for this fragment
return results;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
#Override
public void onDetach() {
super.onDetach();
}}
Initially when I click on the menu fragment, the values in the tab fragment comes as it is mentioned in the code. But if I again tap on the menu slider the items in the tab layout are not displaying.And also some tabs empty or some tabs are having some value. Where am I going wrong?
Use FragmentStatePagerAdapter instant of FragmentPagerAdapter.
Try populating data in List inside onResume()

Categories

Resources