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

问题描述 投票:1回答:2

我有一个与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;
    }

}    

请帮帮我

android tabs android-viewpager
2个回答
1
投票

就像@Mrugesh Thaker所说的那样,每当FragmentonCreateView()膨胀时,所有View生命周期函数 - Fragment等都会被执行。

在这里,每当View成为当前Fragment页面的相邻Fragment时,每个FragmentFragment都会膨胀。

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

在你的ViewPager,当你设置OnPageChangeListener,添加一个ViewPager到yourprivate 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

qazxswpoi


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.