2017-07-18 8 views
0

私の仕事は、1つのタブアクティビティで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); 
    } 
} 

答えて

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

私は、スワイプ方向について

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

アダプタで使用すると、1つのタブは、特定のデータを持っているしたい場合は、そのケースを処理する必要があり、それは

+0

私は日付を解析する際に何らかの間違いがあると思います。それは私にいくつかのエラーを示しています。 – MilkaMozhi

+0

私の悪いことは、自分でコードをテストしませんでした。これは今すぐ動作するはずです。 – Greaper

0

を役に立てば幸いリスナーをViewPagerにアタッチする必要があると思います。

+0

私はすでにあなたが言ったことを違う方法で行ったと思います。 – MilkaMozhi

+0

newInstanceを使って?私はそこにifステートメントを入れません –

関連する問題