如何在MainActivity中找到最后一个片段

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

标签: java android android-fragments

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

iViewPager.setCurrentItem();

这是我定义viewPager

MainActivity.java
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);
    }
}

3 个答案:

答案 0 :(得分:1)

你可以改变你的部分代码::

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

        case 2://change it from 0 -> max
            TabFragment1 tab1 = new TabFragment1();
            return tab1;
        case 1:
            TabFragment2 tab2 = new TabFragment2();
            return tab2;
        case 0:
            TabFragment3 tab3 = new TabFragment3();
            return tab3;
        default:
            return null;

    }
}

或者您甚至可以更改片段名称::

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

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

    }
}

尝试一下..

答案 1 :(得分:0)

你可以使用

HomeListFragment fragment = 
      (HomeListFragment) getSupportFragmentManager().findFragmentByTag("android:switcher:"+R.id.viewpager+":0");
         //"android:switcher:"+R.id.viewpager+":0" returns current fragment

  if(fragment != null)  // could be null if not instantiated yet
  {
     if(fragment.getView() != null) 
     {

        ViewPager.LayoutParams params = (ViewPager.LayoutParams) group.getLayoutParams();
        params.setLayoutDirection(0x00000001);

     }
  }

请注意,0x00000001是rtl的const值,请参考here

答案 2 :(得分:0)

这个解决方案有点棘手但是,这是这个问题的最佳答案

首先,我们需要访问getCount()的{​​{1}}

在定义PagerAdapter.java的实例并在ViewPager上设置PagerAdapter之后mainActivity.javaonCreate()中的

(在设置适配器之后这很重要)我们写:

PagerAdapter

所以在我的代码中,MainActivity是这样的:

iViewPager.setCurrentItem(viewPagerAdapter.getCount());