繁体   English   中英

如何在Android的特定标签上加载数据?

[英]How can i load data on specific tab in 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);
    }   
}

这应该工作。 在您的片段中:

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

希望能帮助到你

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

    @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上附加侦听器。

暂无
暂无

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

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