0

I actually hope this question makes sense as I've only started coding Java since the beginning of the year. My terminology isn't particularly great and while I understand the majority of core coding concepts, my Java is shaky at best. soooo... I've built a basic android app, based on this tutorial -

https://www.youtube.com/watch?v=K9rj_AFyLKk which is part 3 of 3.

the app doesn't really have anything of substance, it just allows the user to swap out fragments with a swipe, of which there 3. I've managed to get to the end of the tutorial and have replicated the app pretty much identically.

I want to add an extra feature; is there anyway to add a hit counter to each of the fragments to tally how many times each of the fragments has been visited?

I.e have an extra String on each fragment that reads "I've been visited " + someVariable + " this many times".

I find it easy enough to tally an event such as a button click, but cannot see where I could implement the same for something like this?

below is MainActivity.java which I believe deals with the swapping of the fragments

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends FragmentActivity {

    ViewPager viewPager = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = (ViewPager)findViewById(R.id.pager);
        FragmentManager fragmentManager = getSupportFragmentManager();
        viewPager.setAdapter(new MyAdapter(fragmentManager));
    }
}
class MyAdapter extends FragmentStatePagerAdapter{


    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

Below is a continuation, with the addition of variables used for storing the tally, not even sure if this is correct, but have tried it as this is what I would do for a button event - storing the tally as the event is fired.

    //@Override
    int numSwipes = 0;
    int numSwipes2 = 0;
    int numSwipes3 = 0;
    public Fragment getItem(int i) {
        Fragment fragment = null;
        if(i==0)
        {
            fragment = new FragmentA();
            numSwipes++;
        }
        if(i==1)
        {
            fragment = new FragmentB();
            numSwipes2++;
        }
        if(i==2)
        {
            fragment = new FragmentC();
            numSwipes3++;
        }
        return fragment;
    }

    @Override
    public int getCount() {
        return 3;
    }
    public CharSequence getPageTitle(int position){
        if(position==0)
        {
            return "tab 1";
        }
        if(position==1)
        {
            return "tab 2";
        }
        if(position==2)
        {
            return "tab 3";
        }
        return null;
    }
}

there are seperate classes for each of the fragments; FragmentA.java,FragmentB.java and FragmentC.java whichy look identical to eachother except R.layout corresponds to its relevant fragment -

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;


public class FragmentA extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,    Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_a,container,false);
    }
}

if using the tally variables is the correct way to go, how could I access them in the fragment classes?

Treeno1
  • 139
  • 1
  • 2
  • 18
  • Yes there is a way, but you must add the relevant code of displaying the fragments. No one is going to visit that link, view the code and do it for you. – TDG May 27 '16 at 14:42
  • apologies, I've amended it above. – Treeno1 May 27 '16 at 15:08
  • this question seems to deal with what you're asking: http://stackoverflow.com/questions/10024739/how-to-determine-when-fragment-becomes-visible-in-viewpager FYI - You're dealing with some pretty advanced stuff... hang in there. – Nerdy Bunz Jun 03 '16 at 07:32

0 Answers0