我如何在选项卡导航中解决以下代码中的错误?

时间:2016-11-15 07:19:34

标签: java android

我无法在标签导航中找到以下代码中的错误。

  

我如何在下面的代码中解决这个问题?

我发现找出错误非常困难。请帮助我解决下面提到的代码中的以下错误..

错误发生在,

    PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(),fragments);
            final ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
            pager.setAdapter(adapter);

以及以下行

            ActionBar.TabListener tabListener = new ActionBar.TabListener() {
                @Override
                public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {

                    pager.getCurrentItem(tab.getPosition());

                }

完整的代码是

        import android.app.Fragment;
        import android.support.v4.app.FragmentTransaction;
        import android.support.v4.view.*;
        import android.support.v7.app.ActionBar;
        import android.os.Bundle;
        import android.support.v7.app.AppCompatActivity;
        import android.support.v7.widget.Toolbar;
        import android.widget.TableLayout;
        //import android.support.design.widget.TabLayout;

        import java.util.List;
        import java.util.Vector;

        public class MainActivity extends AppCompatActivity {



            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                List<Fragment> fragments = new Vector<>();
                fragments.add(Fragment.instantiate(this, FragmentOne.class.getName()));
                fragments.add(Fragment.instantiate(this, FragmentTwo.class.getName()));

             PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(),fragments);
                final ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
                pager.setAdapter(adapter);

                final ActionBar actionBar = getSupportActionBar();
                actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

                ActionBar.TabListener tabListener = new ActionBar.TabListener() {
                    @Override
                    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {

                        pager.getCurrentItem(tab.getPosition());

                    }

                    @Override
                    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {

                    }

                    @Override
                    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {

                    }
                };

                actionBar.addTab(actionBar.newTab().setText("page 1").setTabListener(tabListener));
                actionBar.addTab(actionBar.newTab().setText("page 2").setTabListener(tabListener));

                pager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()
                {
                    @Override
                    public void onPageSelected(int position) {
                        actionBar.setSelectedNavigationItem(position);
                    }
                });
            }
        }


import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;

import java.util.List;

/**
 * Created by seyali-01 on 14/11/16.
 */

public class PagerAdapter extends FragmentPagerAdapter {

    private List<Fragment> fragments;

    public PagerAdapter(FragmentManager fm, List<Fragment> fragments) {
        super(fm);
        this.fragments = fragments;
    }



    @Override
    public Fragment getItem(int i) {

        return fragments.get(i);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }
}

1 个答案:

答案 0 :(得分:1)

我的解决方案代码:

fragment_bid.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.BoardiesITSolution.CritiMonApp.AppsActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    <include layout="@layout/toolbar" />
    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill" />
 </android.support.design.widget.AppBarLayout>

 <android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
    <!--app:layout_behaviour="@string/appbar_scrolling_view_behaviour" />-->
 </android.support.design.widget.CoordinatorLayout>

BidFragment.class:

public class BidFragment extends Fragment {
  @InjectView(R.id.tabs)
  TabLayout tabs;
  @InjectView(R.id.viewpager)
  ViewPager viewpager;
  SupportMapFragment fragment;
  @Override
  public void onAttach(Context context) {
    super.onAttach(context);

}


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

    return inflater.inflate(R.layout.fragment_bid_booking, container, false);
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    FragmentManager fm = getFragmentManager();
    fragment = (SupportMapFragment) fm.findFragmentById(R.id.map_container);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    ButterKnife.inject(this, view);
    tabs.addTab(tabLayout.newTab().setText("BID"));
    tabs.addTab(tabLayout.newTab().setText("BOOK"));
    tabs.setTabGravity(TabLayout.GRAVITY_FILL);
    final PagerAdapter adapter = new PagerAdapter
            (getActivity().getSupportFragmentManager(), tabs.getTabCount());
    viewpager.setAdapter(adapter);
    viewpager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewpager.setCurrentItem(tab.getPosition());
            onFragmentInteractionListener.OnConfirmrRidesBookingCallBack();
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });
}


}

PagerAdapter.class:

public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;

public PagerAdapter(FragmentManager fm, int NumOfTabs) {
    super(fm);
    this.mNumOfTabs = NumOfTabs;
}

@Override
public Fragment getItem(int position) {

    switch (position) {
        case 0:
            BidMainFragment bidMainFragment = new BidMainFragment();
            return bidMainFragment;
        case 1:
            BookMainFragment bookMainFragment = new BookMainFragment();
            return bookMainFragment;
        default:
            return null;
    }
}

@Override
public int getCount() {
    return mNumOfTabs;
}
}