简体   繁体   English

如何获得片段在活动中的位置?

[英]How to get the position of the fragment in an Activity?

I am new to android. 我是Android新手。 I have a sliding tab layout and a viewpager with a fragment. 我有一个滑动选项卡布局和一个带有片段的viewpager。 I have setup a basic link between these to switch tabs for a count of 5(ie 5 tabs) The problem is that, in each of these tabs, I want to populate a listview using simplecursoradapter, for which I need the position of each fragment so that I could populate it accordingly. 我已经在这些标签之间设置了一个基本链接,以切换5个标签(即5个标签)。问题是,在每个标签中,我想使用simplecursoradapter填充列表视图,为此我需要每个片段的位置这样我就可以相应地进行填充了 I have got the position in the fragment adapter class, but I am unable to pass it to the main activity. 我已经在片段适配器类中找到了位置,但是无法将其传递给主要活动。 Also, How can I access DBadapter inside my fragment class to populate listview from the database? 另外,如何访问片段类内部的DBadapter以从数据库填充listview? or is there some other way to do this? 还是有其他方法可以做到这一点? Below is my code. 下面是我的代码。 Any help is greatly appreciated. 任何帮助是极大的赞赏。 Thanks in advance. 提前致谢。

public class myFragment extends Fragment{
    private ListView mainList;
    public static myFragment getInstance(int position){
        myFragment MyFragment = new myFragment();
        Bundle args = new Bundle();
        args.putInt("position", position);
        MyFragment.setArguments(args);

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

            View layout = inflater.inflate(R.layout.fragment_main_list,container, false);
            mainList = (ListView)layout.findViewById(R.id.mainListView);
            Bundle bundle = getArguments();

            if(bundle!=null){
                if(bundle.getInt("position")==0){
                mainList.setBackgroundColor(getResources().getColor(R.color.primary500));
            }
            else if(bundle.getInt("position")==1){
                mainList.setBackgroundColor(getResources().getColor(R.color.primary100));
            }
            else if(bundle.getInt("position")==2){
                mainList.setBackgroundColor(getResources().getColor(R.color.primary700));
            }
            else if(bundle.getInt("position")==3){
                mainList.setBackgroundColor(getResources().getColor(R.color.accent));
            }
            else if(bundle.getInt("position")==4){
                mainList.setBackgroundColor(getResources().getColor(R.color.black));
            }

            }
            return layout;
    }   

}


The Main Activity is as follows: 主要活动如下:

public class MainActivity extends ActionBarActivity {
private Toolbar toolbar;
private ViewPager mPage;
private SlidingTabLayout mTabs;
DBadapter myDb;

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

    toolbar = (Toolbar)findViewById(R.id.app_bar);
    mPage = (ViewPager)findViewById(R.id.pager);
    mTabs = (SlidingTabLayout)findViewById(R.id.tabs);
    mFab = (FloatingActionButton)findViewById(R.id.fab);

    setSupportActionBar(toolbar);
    mPage.setAdapter(new myPagerAdapter(getSupportFragmentManager()));
    mTabs.setViewPager(mPage);
}
public class myPagerAdapter extends FragmentPagerAdapter{
        String[] tabs;
        public myPagerAdapter(FragmentManager fm) {
            super(fm);
            tabs = getResources().getStringArray(R.array.tabs);
        }

        @Override
        public Fragment getItem(int position) {
            myFragment MyFragment = myFragment.getInstance(position);
            return MyFragment;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tabs[position];
        }

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

    } 

Use this method : 使用此方法:

mPage.setOnPageChangeListener(new SimpleOnPageChangeListener() {

            @Override
            public void onPageSelected(int pos) {
              // pos is the position of fragment that app showing
        });

UPDATE UPDATE
in your view pager adapter : 在您的视图寻呼机适配器中:

public Fragment getItem(int position) {
    return new myFragment(position);
 } 

And in your fragment class : 在您的片段类中:

public class myFragment extends Fragment{

private int pos
public void myFragment(int position){
   this.pos = position;
}
// other code of your class 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM