创建一个片段命中计数器

时间:2016-05-31 10:03:43

标签: android android-fragments

我有一个非常基本的应用程序,它有三个片段 - 片段a,b和c,每个片段都包含一段文字,上面写着“我已被访问过”+ tallyVariable +“这多次”。

我不完全确定这是正确的做法,因为我是应用程序开发的新手,而且是java语言的新手,但是试图试一试。

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 {
    //tally variables
    public int numSwipes = 0;
    public int numSwipes2 = 0;
    public int numSwipes3 = 0;


    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{

    // reference to MainActivity and variables

    MainActivity mainActivity = new MainActivity();
    int fragNumSwipes = mainActivity.numSwipes;
    int fragNumSwipes2 = mainActivity.numSwipes2;
    int fragNumSwipes3 = mainActivity.numSwipes3;

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

    //@Override

    public Fragment getItem(int i) {
        Fragment fragment = null;
        if(i==0)
        {
            fragment = new FragmentA();
            fragNumSwipes++;
        }
        if(i==1)
        {
            fragment = new FragmentB();
            fragNumSwipes2++;
        }
        if(i==2)
        {
            fragment = new FragmentC();
            fragNumSwipes3++;
        }
        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;
    }
}
下面的

是其中一个片段的代码(其余片段的代码是相同的)。

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


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);
    }

    // MyAdapter adapterVariables = new MyAdapter();

}

我已经注释掉了上面的变量声明“adapterVariables”,但是当它处于活动状态时,它不会让我创建对现在存在计数变量的MyAdapter类的引用?我错过了什么,我认为它需要括号之间的东西?

1 个答案:

答案 0 :(得分:0)

MainActivity mainActivity = new MainActivity();这不会创建对MainActivity的引用!您的片段代码也是如此。您正在创建类的新实例。您可以在构造函数中传递引用。但是对于你的想法,你不需要这个。下面,您可以找到更正的版本,如果您对此有疑问,请随时询问!

public class MainActivity extends FragmentActivity {

    //tally variables
    private int numSwipes = 0;
    private int numSwipes2 = 0;
    private int numSwipes3 = 0;

    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));

         //instead of increasing the number in the adapter, add a listener, since this is a better place to do it.
         viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch(position){
                    case 0:
                        numSwipes++;
                        break;
                    case 1:
                        numSwipes2++;
                        break;
                    case 2:
                        numSwipes3++;
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}

class MyAdapter extends FragmentStatePagerAdapter {    
    public MyAdapter(FragmentManager fm) {
        super(fm);
    } 

    @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;
    }
}