如何在Activity中初始化自定义适配器

时间:2017-08-17 11:49:57

标签: java android xml android-layout viewgroup

我有一个自定义寻呼机适配器(PagerIot),我已成功使用片段实现,如下所示

public class SensorIconFragment extends Fragment {



public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

      View view = inflater.inflate(R.layout.layout_listview, container, false);
      ButterKnife.bind(this, view);
      pagerIot = (PagerIot)container;

我想在活动中使用寻呼机适配器,但问题是我需要知道如何在活动中初始化寻呼机。在一个片段中它非常简单,因为片段有一个onCreateViewMethod,其中一个viewGroup作为参数传入,所以我可以简单地说p PagerIot = (PagerIot)container;

在Activity中,我不能简单地初始化PagerIOT因为onCreate没有传入ViewGroup所以有人知道如何在Activity中初始化适配器吗?

这是我的PagerIOT课程

public class PagerIot extends ViewPager
{
   public PagerIot(Context context, AttributeSet attrs)
   {
      super(context, attrs);
   }

   public void allowScrollingToTab(int tab)
   {
      // Set the limit of the number of pages in the page adapter
      ManagementTabFragment.SectionsPagerAdapter adapter = (ManagementTabFragment.SectionsPagerAdapter)getAdapter();
      adapter.allowScrollingToTab(tab);
   }
}

我还将包括上面类中引用的管理选项卡

package com.dtect.devicesensorinsight.home.tabs;

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

import com.dtect.common.ITabView;
import com.dtect.common.MyFragmentPagerAdapter;
import com.dtect.devicesensorinsight.R;
import com.dtect.devicesensorinsight.events.JumpToManagementTabEvent;
import com.dtect.devicesensorinsight.home.PagerIot;
import com.dtect.devicesensorinsight.home.tabs.management.CostCentreListFragment;
import com.dtect.devicesensorinsight.home.tabs.management.IotTabFragment;
import com.dtect.devicesensorinsight.home.tabs.management.SensorIconFragment;
//import com.dtect.devicesensorinsight.home.tabs.notifications.TenantDetailsFragment;
import com.dtect.devicesensorinsight.home.tabs.management.iot.SensorInformationChildFragment;
import com.dtect.devicesensorinsight.sensors.ItemClickedEvent;

import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;

public class ManagementTabFragment extends Fragment implements ITabView
{
   public static final int TAB_USER_LANDINGPAGE = 0;
   public static final int TAB_TENANT_SCANQRCODE= 1;
   public static final int TAB_TENANT_DETAILS = 2;
   public static final int TAB_UOM_SYMBOLS = 3;
   public static final int TAB_LOCATIONS = 4;
   public static final int TAB_IOT = 5;

   @BindView(R.id.managementpager)
   PagerIot viewPager;
   private SectionsPagerAdapter sectionsPagerAdapter;

   @Nullable
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
   {
      View view = inflater.inflate(R.layout.fragment_management_pages, container, false);
      ButterKnife.bind(this, view);
      sectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());
      viewPager.setAdapter(sectionsPagerAdapter);
      viewPager.setOffscreenPageLimit(5);

      return view;
   }

   @Override
   public void onStart() {
      super.onStart();
      EventBus.getDefault().register(this);
   }

   @Override
   public void onStop() {
      super.onStop();
      EventBus.getDefault().unregister(this);
   }

   public void onEvent(JumpToManagementTabEvent tab) {
      setSelectedPage(tab.tabIndex);
   }

   private void setSelectedPage(int index)
   {
      viewPager.setCurrentItem(index);
   }

   @Override
   public MyFragmentPagerAdapter getAdapter() {
      return sectionsPagerAdapter;
   }

   @Override
   public ViewPager getViewPager() {
      return viewPager;
   }

   /**
    * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
    * one of the sections/tabs/pages.
    */
   public class SectionsPagerAdapter extends MyFragmentPagerAdapter {

      private int allowScrollingToTabOffset = 0;

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

      public void allowScrollingToTab(int tab)
      {
         this.allowScrollingToTabOffset = tab;
         notifyDataSetChanged();
      }

      @Override
      public Fragment getItem(int position) {
         switch (position)
         {

            case TAB_USER_LANDINGPAGE:
               return new UserLandingPageFragment();
            case TAB_UOM_SYMBOLS:
               return new SensorIconFragment();
            case TAB_LOCATIONS:
               return new CostCentreListFragment();
            case TAB_IOT:
               return new IotTabFragment();
            default:
               return null;
         }
      }

      @Override
      public int getCount() {
         return allowScrollingToTabOffset + 1;
      }

      @Override
      public CharSequence getPageTitle(int position) {
         Locale l = Locale.getDefault();
         return "" + position;
      }
   }
}

0 个答案:

没有答案