Android:使用Tab检测单个片段viewpager

时间:2016-05-16 14:22:22

标签: android tabs android-viewpager

我在设置ViewPagerFragmentPagerAdapter

的情况下设置了TabLayout安卓问题

TabLayout运行正常。但是,所有选项卡中的代码同时运行。我希望当前所选标签中的代码能够运行

这是我的活动

package francesco.prisco.siamostudenti.ui;

import android.os.Bundle;
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.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
import francesco.prisco.siamostudenti.R;
import francesco.prisco.siamostudenti.ui.fragments.CalendarFragment;
import francesco.prisco.siamostudenti.ui.fragments.ChatFragment;
import francesco.prisco.siamostudenti.ui.fragments.EmailFragment;
import francesco.prisco.siamostudenti.ui.fragments.HomeFragment;
import francesco.prisco.siamostudenti.ui.fragments.ProfiloFragment;


public class HomeTabActivity extends AppCompatActivity {

//  private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_tab);

       //     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);
        setupTabIcons();
    }
    private void setupTabIcons() {
        int[] tabIcons = {
            R.drawable.ic_home,
            R.drawable.ic_calendar,
            R.drawable.ic_chat,
            R.drawable.ic_email,
            R.drawable.ic_profile
        };

        tabLayout.getTabAt(0).setIcon(tabIcons[0]);
        tabLayout.getTabAt(1).setIcon(tabIcons[1]);
        tabLayout.getTabAt(2).setIcon(tabIcons[2]);
        tabLayout.getTabAt(3).setIcon(tabIcons[3]);
        tabLayout.getTabAt(4).setIcon(tabIcons[4]);
    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFrag(new HomeFragment(), "HOME");
        adapter.addFrag(new CalendarFragment(), "CALENDAR");
        adapter.addFrag(new ChatFragment(), "CHAT");
        adapter.addFrag(new EmailFragment(), "EMAIL");
        adapter.addFrag(new ProfiloFragment(), "PROFILO");
        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 addFrag(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {

            // return null to display only the icon
            return null;
         }
     }
 }

这是片段的代码

package francesco.prisco.siamostudenti.ui.fragments;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import francesco.prisco.siamostudenti.R;
import francesco.prisco.siamostudenti.ui.HomeTabActivity;

public class ProfiloFragment extends Fragment {
    public ProfiloFragment() {}
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_profilo, container, false);
        return rootView;
    }

}    

请帮帮我

2 个答案:

答案 0 :(得分:1)

像@Mrugesh Thaker所说,每当Fragment的{​​{1}}膨胀时,所有onCreateView()生命周期函数 - View等都会被执行。

此处,每当Fragment View成为当前Fragment页面的相邻Fragment时,Fragment就会被充气。

因此,当它成为当前选定的Fragment页面时,您应该将'每个片段需要执行的代码'移动到OnPageChangeListener

HomeTabActivity中,当您设置ViewPager时,请添加 OnPageChangeListener

ViewPager
private void setupViewPager(ViewPager viewPager) {    
    .
    .
    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener(){
        public void onPageScrollStateChanged (int state){
        }

        public void onPageSelected (int position){
            /*
             * Define the action to be performed for each page.
             * Variable 'position' is the index of the page in the ViewPager
             */  
        }

        public void onPageScrolled (int position, float positionOffset, int positionOffsetPixels){
        }
    });
}

Learning Resource

答案 1 :(得分:0)

实际上,查看寻呼机就是这样的。查看寻呼机的主要目的是使用户能够顺利滑动,所以它的作用是,它会创建您一次给出的所有片段,以获得平滑的滑动流程。这实际上是它的局限性,因为片段生命周期的所有方法都是一次调用的。