【问题标题】:How to get the position of the fragment in an Activity?如何获取片段在 Activity 中的位置?
【发布时间】:2015-02-01 06:27:53
【问题描述】:

我是安卓新手。我有一个滑动选项卡布局和一个带有片段的查看器。我已经在这些选项卡之间设置了一个基本链接,以切换 5 个选项卡(即 5 个选项卡) 问题是,在每个选项卡中,我想使用 simplecursoradapter 填充一个列表视图,为此我需要每个片段的位置这样我就可以相应地填充它。我在片段适配器类中获得了位置,但我无法将其传递给主要活动。另外,如何访问片段类中的 DBadapter 以从数据库中填充列表视图?还是有其他方法可以做到这一点?下面是我的代码。任何帮助是极大的赞赏。提前致谢。

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;
    }   

}


主要活动如下:

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;
        }

    } 

【问题讨论】:

    标签: java android android-fragments android-listview android-tabs


    【解决方案1】:

    使用这个方法:

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

    更新
    在您的视图寻呼机适配器中:

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

    在你的片段类中:

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

    【讨论】:

    • 我用过。但我无法在选项卡之间切换。卡在第一个位置。我没有使用任何条件来检查位置。只是一条祝酒消息来检查位置。但是卡住了。我无法在选项卡之间导航。但是滑动时位置会改变。
    • 向左滑动或书写时发生了什么?什么都没有改变,或者它改变了但显示你的第一个片段?
    • 我通过 toast 显示的位置正在改变。但它只显示第一个片段。
    • 为你的片段创建一个 cunsrtuct 方法并为位置设置一个变量,然后在你的 getItem 方法中为它创建一个实例,然后在你的片段中检查这个变量
    • myFragment MyFragment = myFragment.getInstance(position);填充列表视图(位置);你的意思是这样吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-13
    • 2012-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多