0

我总共有 1000 多个标签/页面可供查看。我希望能够通过在 EditText 中输入一个数字来查看任何选项卡/页面,并且在单击 Button 时它应该跳转到指定的选项卡。例如,如果我在第 1 页,如果我输入“600”并点击搜索(按钮),我应该能够查看该特定页面并确保 600 旁边的页面应该是 601、601 ......以及之前600 应该是 599, 598....

public class MainActivity extends FragmentActivity {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

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

public class SectionsPagerAdapter extends FragmentPagerAdapter {
    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    public Fragment getItem(int position) {
        switch(position){
        case 0:

        case 1: 
            Ang100s.Ang1 ang1 = new Ang100s.Ang1();
            return ang1;

        case 2: 
            Ang100s.Ang2 ang2 = new Ang100s.Ang2();
            return ang2;
        case 3: 
            Ang100s.Ang3 ang4 = new Ang100s.Ang3();
            return ang4;
        }
        Help help = new Help();
        return help;
    }
    @Override
    public int getCount() {
        return 1430;
    }
    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        for(int i = 1;i < 1430; i++){
            if(position == i){
                return "Ang " + position;
            }
        }
        return null;
    }
}

public static class Help extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main_dummy,
                container, false);
        final Button search = (Button) rootView.findViewById(R.id.clicktosearch);
        final EditText number = (EditText)rootView.findViewById(R.id.number);
        search.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(number.getText().toString().equals("1")){

                }
            }
        });
        return rootView;
    }
}
}
4

1 回答 1

0

使用 ViewPager,您可以使用setCurrentItem(int)并传递您想要显示的项目的索引。此外,您的 FragmentPagerAdapter 需要能够为从零到的任何索引生成一个项目getCount()- 目前它没有。

于 2013-03-31T21:49:13.810 回答