2017-07-18 49 views
0

我的任務是在一個選項卡活動50屏幕上加載的數據進行100個的屏幕,也遞增上向後輕掃向前滑動並遞減日期日期。我怎樣才能加載Android中的特定選項卡上的數據?

我寫的代碼將增加日期和取得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); 
} 

希望它可以幫助

+0

我認爲這是在解析date.It一些錯誤顯示我的一些錯誤。 – MilkaMozhi

+0

我的壞事沒有自己測試代碼。這應該現在工作。 – Greaper

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上附加監聽器。

+0

我覺得我已經做了什麼,你在不同的方式 – MilkaMozhi

+0

紛紛表示通過使用的newInstance?我不會在那裏放一個if語句 –

相關問題