我怎样才能在mainActivity中找到最后一个片段

时间:2015-12-14 14:42:39

标签: java android android-fragments

我想让viewPager支持Rtl,并在最后一个片段上设置当前项目,但是当我在MainActivity.java中编写此代码时,我不知道如何能够访问最后一个片段。

根据this post,我希望将viewPager设为Rtl:

iViewPager.setCurrentItem();

MainActivity.java ,我在其中定义viewPager

package com.example.imajid.grid2.activities;

import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.example.imajid.grid2.R;
import com.example.imajid.grid2.adapters.PagerAdapter;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private DrawerLayout mDrawerLayout;

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

        //----------------------- set toolbar
        Toolbar iToolbar = (Toolbar) findViewById(R.id.xmlToolbar);
        setSupportActionBar(iToolbar);

        //------------------------------ initialize toolbar
        ActionBar actionBar = getSupportActionBar();
        actionBar.setHomeAsUpIndicator(R.drawable.menu);
        actionBar.setDisplayHomeAsUpEnabled(true);

        //****************************  find tablayout *****************************//
        TabLayout iTabLayout = (TabLayout) findViewById(R.id.xmlTabLayout);
        iTabLayout.addTab(iTabLayout.newTab());
        iTabLayout.addTab(iTabLayout.newTab());
        iTabLayout.addTab(iTabLayout.newTab());

        iTabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

        //*******************************  Array list for sending titles *********************************//

        ArrayList<String> viewPagersTitle = new ArrayList<String>();

        viewPagersTitle.add("صفحه اصلی");
        viewPagersTitle.add("فیلم");
        viewPagersTitle.add("آهنگ");

        //******************************* find view pager in xml *****************************//

        ViewPager iViewPager = (ViewPager) findViewById(R.id.xmlViewPager);

        //************************************** Rtlizer view pager ************************************

        ====>iViewPager.setCurrentItem();

        //**************************************  find adapter for view pager *******************************//

        PagerAdapter viewPagerAdapter = new PagerAdapter(getSupportFragmentManager(), iTabLayout.getTabCount(), viewPagersTitle);

        //************************************  set adapter for view pager *******************************//
        iViewPager.setAdapter(viewPagerAdapter);

        //***********************************  setup tablayout to view pager********************************//
        iTabLayout.setupWithViewPager(iViewPager);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.xmlDrawerLayoutRoot);

        //-------------------------------------- find navigation view
        NavigationView navigationView = (NavigationView)findViewById(R.id.xmlNavigation);
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                menuItem.setChecked(true);
                mDrawerLayout.closeDrawers();
                Toast.makeText(MainActivity.this, menuItem.getTitle(), Toast.LENGTH_SHORT).show();
                return true;
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }

    //onoptionItemSelected
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        String btnName = null;

        switch (itemId){
            case android.R.id.home:
                mDrawerLayout.openDrawer(GravityCompat.START);
                return true;

            case R.id.menu_setting:
                btnName = "Setting";
                break;
            case R.id.menu_compass:
                btnName = "Compass";
                break;
            case R.id.menu_help:
                btnName = "Help";
                break;
        }

        Toast.makeText(MainActivity.this, "You clicked at " + btnName, Toast.LENGTH_SHORT).show();
        return true;
    }
}

最后,这是我的查看寻呼机适配器:

package com.example.imajid.grid2.adapters;

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

import com.example.imajid.grid2.fragments.TabFragment1;
import com.example.imajid.grid2.fragments.TabFragment2;
import com.example.imajid.grid2.fragments.TabFragment3;

import java.util.ArrayList;

/**
 * Created by imajid on 12/8/2015.
 */
public class PagerAdapter extends FragmentStatePagerAdapter {

    private ArrayList<String> titles;
    int numOfTabs;

    public PagerAdapter(FragmentManager fm, int numOfTabs, ArrayList<String> titles) {
        super(fm);
        this.numOfTabs = numOfTabs;
        this.titles = titles;
    }

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

            case 0:
                TabFragment1 tab1 = new TabFragment1();
                return tab1;
            case 1:
                TabFragment2 tab2 = new TabFragment2();
                return tab2;
            case 2:
                TabFragment3 tab3 = new TabFragment3();
                return tab3;
            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        return  this.numOfTabs;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return titles.get(position);
    }
}

1 个答案:

答案 0 :(得分:1)

请问:

setCurrentItem(viewPagerAdapter.getCount());

在设置适配器之前它不起作用,我应该在设置adpter之后编写此代码,感谢Kevin Chris和K Neeraj Lal。