如何在android中的特定选项卡上加载数据?

时间:2017-07-18 13:21:39

标签: java android

我的任务是在一个标签活动中从100个屏幕中的第50个屏幕加载数据,并在向后滑动时在向前滑动和递减日期上增加日期。

我编写代码来增加日期并将第50个屏幕作为当前屏幕,但我不知道如何在第50个屏幕中加载当前日期并在向后滑动时减少日期。

这是我的主要活动:

public class MainActivity extends AppCompatActivity {
    public static Tab2 self;
    private SectionsPagerAdapter mSectionsPagerAdapter;
    private ViewPager mViewPager;

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

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);
        mViewPager.setCurrentItem(50);
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        public PlaceholderFragment() {
        }

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            TextView textView = (TextView) rootView.findViewById(R.id.section_label);
            textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
            return rootView;
        }
    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

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


        @Override
        public Fragment getItem(int position) {
            //TabHost tabHost = Tab2.self.getTabHost();
            //tabHost.setCurrentTab(0);

            return Tab2.newInstance(position);
        }

        @Override
        public CharSequence getPageTitle(int position) {
             return "SECTION 3";
        }
    }
}

我的标签活动是

public class Tab2 extends Fragment {
    private int position;

    public Tab2() {

    }

    public static Tab2 newInstance(int position) {
        Tab2 fragment = new Tab2();
        Bundle args = new Bundle();
        args.putInt("position", position);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            position = getArguments().getInt("position");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.tab2, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, position);
        String dte = sdf.format(c.getTime()).toString();

        TextView textView2 =  getView().findViewById(R.id.textView2);
        textView2.setText(dte);
    }   
}

2 个答案:

答案 0 :(得分:0)

这应该有效。 在你的片段中:

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    Calendar c = Calendar.getInstance();
    c.setTime(new Date());
    c.add(Calendar.DATE, position);
    String dte = sdf.format(c.getTime()).toString();

    TextView textView2 =  getView().findViewById(R.id.textView2);
    textView2.setText(dte);
}

希望有所帮助

答案 1 :(得分:0)

如果您希望一个选项卡具有特定数据,则需要在适配器中处理该情况

    @Override
    public Fragment getItem(int position) {
        Fragment f =  Tab2.newInstance(position);
        if (position == specificPosition) {
            Bundle args = new Bundle(); 
            // put into args... 
            f.setArguments(args);
        }
        return f;
    }

关于滑动方向,我认为您需要在ViewPager上附加侦听器。