I have an app with 3 tabs. A tab has a fragment with a button to go another view within the same tab. My implementation is simple. I have main_activity with view pager. Here is snippet of main_activity.xml.
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
Below the MainActivity.java
public class MainActivity extends AppCompatActivity {
public SectionsPageAdapter mSectionPageAdapter;
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionPageAdapter = new SectionsPageAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
setupViewPager(mViewPager);
//Assigns the ViePager to TabLayout
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
//Defines the number of tabs by setting appropriate fragment and tab name.
private void setupViewPager(ViewPager viewPager){
SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
adapter.addFragment(new FirsTab(),"TAB1");
adapter.addFragment(new SecondTab(),"TAB2");
adapter.addFragment(new ThirdTab(),"TAB3");
viewPager.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
When I replace the FirstTab with FirsTabSubFragment with getChildFragmentManager() I get a "no view found" error.
java.lang.IllegalArgumentException: No view found for id 0x7f0d0073 (com.none.app:id/pager) for fragment FirsTabSubFragment{607b4a6 #0 id=0x7f0d0073}
Below the code for FirtTab.java
public class FirstTab extends Fragment{
private Button btnScan;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.scan_layout, container, false);
btnScan = (Button) view.findViewById(R.id.btnScan);
btnScan.setOnClickListener(new View.OnClickListener(){
public void onClick(View view) {
getChildFragmentManager()
.beginTransaction()
.replace(R.id.pager, new FirsTabSubFragment())
.addToBackStack(null)
.commit();
}
});
return view;
}
A replacement with a getFragmentmanager() results in a empty view. Can anyone advice me on this topic?
Related
There's any tutorial/code for refreshing tab? When you change a tab activity and when you go back after you add a new data. It automatically refresh and added.
Below is my MainActivity Code,
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private SectionsPageAdapter mSectionsPageAdapter;
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionsPageAdapter = new SectionsPageAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
setupViewPager(mViewPager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
tabLayout.getTabAt(0).setIcon(R.drawable.ic_news_feed);
tabLayout.getTabAt(1).setIcon(R.drawable.ic_grade);
tabLayout.getTabAt(2).setIcon(R.drawable.ic_profile);
}
private void setupViewPager(ViewPager viewPager) {
SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
adapter.addFragment(new NewsFeedFragment());
adapter.addFragment(new LatestGradeFragment());
adapter.addFragment(new ProfileFragment());
//adapter.addFragment(new Tab3Fragment());
viewPager.setAdapter(adapter);
}
}
Thanks.
Hi I have a TabLayout and ViewPager inside a fragment
public class Explore : Android.Support.V4.App.Fragment, AppCompatActivity
{
private TabLayout tablayout;
private ViewPager viewPager;
private TimeBuget timeBuget;
private SpecialActivity specialActivity;
public override void OnCreate (Bundle savedInstanceState)
{
base.OnCreate (savedInstanceState);
// Create your fragment here
}
public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Use this to return your custom view for this Fragment
// return inflater.Inflate(Resource.Layout.YourFragment, container, false);
//return base.OnCreateView (inflater, container, savedInstanceState);
return inflater.Inflate(Resource.Layout.ExploreLayout,container,false);
viewPager = View.FindViewById<ViewPager> (Resource.Id.viewpagerExplore);
setupViewPager (viewPager);
tablayout = View.FindViewById<TabLayout> (Resource.Id.sliding_tabsExplore);
tablayout.SetupWithViewPager (viewPager);
}
private void InditialFragment()
{
timeBuget = new TimeBuget ();
specialActivity = new SpecialActivity ();
}
public void setupViewPager(ViewPager viewPager)
{
InditialFragment ();
ViewPagerAdapter adapter = new ViewPagerAdapter (SupportFragmentManager);
adapter.addFragment (timeBuget, "Explore");
adapter.addFragment (specialActivity, "Featured");
viewPager.Adapter=adapter;
}
}
Classes cannot have multiple base classes.
If I don't use AppCompatActivity so I can't use SupportFragmentManager
How to solve it?
You have to host the fragment inside the activity, not use both at the same time. After you do that you could access the SupportFragmentManager like this:
ViewPagerAdapter adapter = new ViewPagerAdapter ((SpecialActivity as AppCompatActivity).SupportFragmentManager);
I have a recyclerview inside a fragment which is inside viewpager. I have button outside viewpager. How to update the recyclerview upon clicking the button.
public class Level2MainActivity_grid1 extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.level2mainlayout_place1);
// I am putting a viewpager with fragments into them and put tabs
ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager(), Titles, Numboftabs);
viewPager.setAdapter(pagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
Button buttonId = (Button) findViewById(R.id.planmytraveludupitown);
buttonId.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//I want to save some things and do the changes in the recyclerview of tab 0;
}
});
}
}
The framgment code in tab 0 of the viewpager, in which i am using recyclerview is as below:
public class Udupiwhattosee extends Fragment {
private RecyclerView rv;
private List<Udupiwhattoseearray> udupiwhattoseearray;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
RecyclerView v =(RecyclerView) inflater.inflate(R.layout.tab_1,container,false);
v.setLayoutManager(new LinearLayoutManager(getActivity()));
v.setHasFixedSize(true);
initializeData();
WhattoseeRVAdapter adapter = new WhattoseeRVAdapter(udupiwhattoseearray,container.getContext());
v.setAdapter(adapter);
return v;
}
private void initializeData(){
udupiwhattoseearray = new ArrayList<>();
udupiwhattoseearray.add(new Udupiwhattoseearray("Udupi Sri Krishna Temple", "Udupi, Karanata,INDIA", R.drawable.udupi));
udupiwhattoseearray.add(new Udupiwhattoseearray("Udupi place 2", "Udupi, Karanata,INDIA", R.drawable.udup_krishna_math_gopura_udupi));
udupiwhattoseearray.add(new Udupiwhattoseearray("Udupi Place 3", "Udupi, Karanata,INDIA", R.drawable.udupi_rath));
udupiwhattoseearray.add(new Udupiwhattoseearray("Udupi place 4", "Udupi, Karanata,INDIA", R.drawable.udupi_window));
}
recycler_view.post(new Runnable() {
public void run() {
adapter.notifyDataSetChanged();
}
});
HERE you will see the working of interface ,try this hope its gonna work fine
I'm looking at a guide on how to use TabLayout. This is how they show to attach the PagerAdapter class.
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the ViewPager and set it's PagerAdapter so that it can display items
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new SampleFragmentPagerAdapter(getSupportFragmentManager(),
MainActivity.this));
// Give the TabLayout the ViewPager
TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
}
However, their TabLayout is in an Activity, and mine is in a Fragment. I'm having trouble adapting their code to my use case, because I don't fully understand the practical differences between activities and fragments. Right now, when I try and swipe between tabs, nothing happens. Here is my code:
public class CandidateListFragment extends Fragment {
public static final String TAG = "candidates";
#InjectView(R.id.tabLayout) TabLayout tabLayout;
public CandidateListFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_candidatelist, container, false);
initInstances(v);
return v;
}
private void initInstances(View v){
//TODO: Change to support below API level 21
getActivity().findViewById(R.id.toolbar_actionbar).setElevation(R.dimen.toolbar_candidatelist_elevation);
ViewPager viewPager = (ViewPager) v.findViewById(R.id.viewpager);
viewPager.setAdapter(new TabsFragmentPagerAdapter(getActivity().getSupportFragmentManager(),
getActivity()));
ButterKnife.inject(this, v);
tabLayout.setupWithViewPager(viewPager);
}
#Override public void onDestroyView() {
super.onDestroyView();
ButterKnife.reset(this);
}
I figured it out!!!
I had my xml layout file for the fragment set to RelativeLayout instead of LinearLayout, so my ViewPager wasn't on the screen, which meant I couldn't swipe. It's the simple mistakes that get me
i wanted to know if someone can help me solve this issue. I have a button setup and when its clicked, its supposed to navigate to another viewpager page. But everytime its clicked, the app crashes returning the error at onClick at fragmenttab2 class. Here is my Main Activity.
public class MainActivity extends SherlockFragmentActivity {
ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
viewPager.setCurrentItem(1);
}
}
Here is the fragment containing the onClick:
public class FragmentTab1 extends SherlockFragment {
ViewPager viewPager;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Activity activity = getActivity();
if (activity != null) {
addListenerOnButton();
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragmenttab1, container, false);
return view;
}
public void addListenerOnButton() {
Button nextfrag = (Button) getView().findViewById(R.id.btn_nextfrag);
nextfrag.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
((ViewPager) viewPager.findViewById(R.id.pager)).setCurrentItem(1);
}
});
}
}
The log:
E/AndroidRuntime java.lang.NullPointerException
E/AndroidRuntime com.test.seriessample.FragmentTab1$1.onClick(FragmentTab1.java:58)
your viewPager is probably null. I also see that viewPager itself is a ViewPager and in the onClick you search for another ViewPager within that ViewPager which doesn't seem right.
EDIT:
you should add something like this in your activity
public ViewPager getPager() {
return viewPager;
}
And in the onClick of your Fragment
((MainActivity) getActivity()).getPager().setCurrentItem(1);