2016-07-14 73 views
1

我在android開發新手,只是想學習一路!如何顯示其他佈局

我正在創建一個Android應用程序,我想展示不同的視圖,一個用於重量,天氣和水合作用(我現在稱之爲)。雖然當我運行我的應用程序時,我只顯示我的一個視圖。

我必須添加/修改哪些代碼才能在標籤視圖中顯示其他兩種佈局?

這是我在MainActivity.java

package com.oxinc.android.drate; 

import android.support.design.widget.TabLayout; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 

import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 

import android.widget.EditText; 
import android.widget.TextView; 

public class MainActivity extends AppCompatActivity { 

    int quantity = 0; 
    String outcome = ""; 
    int required = 0; 
    int height = 0; 

    /** 
    * The {@link android.support.v4.view.PagerAdapter} that will provide 
    * fragments for each of the sections. We use a 
    * {@link FragmentPagerAdapter} derivative, which will keep every 
    * loaded fragment in memory. If this becomes too memory intensive, it 
    * may be best to switch to a 
    * {@link android.support.v4.app.FragmentStatePagerAdapter}. 
    */ 
    private SectionsPagerAdapter mSectionsPagerAdapter; 

    /** 
    * The {@link ViewPager} that will host the section contents. 
    */ 
    private ViewPager mViewPager; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     // Create the adapter that will return a fragment for each of the three 
     // primary sections of the activity. 
     mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 

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

     //Tabbed Layout 
     TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); 
     tabLayout.setupWithViewPager(mViewPager); 

     //Floating Action Button 
     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Are you hydrated?", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     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 static PlaceholderFragment fragment_main() { 
      PlaceholderFragment fragment = new PlaceholderFragment(); 
      return fragment; 
     } 

     /** 
     * Returns a new instance of this fragment for the given section 
     * number. 
     */ 
     public static PlaceholderFragment weight(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 hydration = inflater.inflate(R.layout.hydration, container, false); 
      return hydration; 
     } 
    } 

    /** 
    * 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 Fragment getItem(int position) { 
      // getItem is called to instantiate the fragment for the given page. 
      // Return a PlaceholderFragment (defined as a static inner class below). 
      return PlaceholderFragment.weight(position + 1); 
     } 

     @Override 
     public int getCount() { 
      // Show 3 total pages. 
      return 3; 
     } 

     @Override 
     public CharSequence getPageTitle(int position) { 
      switch (position) { 
       case 0: 
        return "Hydration"; 
       case 1: 
        return "Weigh In"; 
       case 2: 
        return "Weather"; 
      } 
      return null; 
     } 
    } 

     public void eight(View view) { 
      quantity = quantity + 8; 
      displayQuantity(quantity); 
     } 

     public void twelve(View view) { 
      quantity = quantity + 12; 
      displayQuantity(quantity); 
     } 

     public void sixteen(View view) { 
      quantity = quantity + 16; 
      displayQuantity(quantity); 
     } 

     public void thirty_two(View view) { 
      quantity = quantity + 32; 
      displayQuantity(quantity); 
     } 

     public void sixty_four(View view) { 
      quantity = quantity + 64; 
      displayQuantity(quantity); 
     } 

     //Reset Button 
     public void reset(View view) { 
      quantity = 0; 
      displayQuantity(quantity); 
      outcome = ""; 
      displayOutcome(outcome); 
     } 

     //Starts the basic formula for oz. per day 
     public int height(int value) { 
      EditText height = (EditText) findViewById(R.id.height); 
      String height_value = height.getText().toString(); 
      int int_height = Integer.parseInt(height_value); 

      if (int_height >= 60) { 
       required = 64; 
      } 
      return required; 
     } 
     /** 
     * This method is called when the check button is clicked. 
     */ 
     public String check(View view) { 

      height((int) height); 
      if (quantity >= required) { 
       outcome = "You have met your goal!!"; 
      } else { 
       outcome = "Keep trying you are almost there"; 
      } 
      displayOutcome(outcome); 
      return outcome; 
     } 

     //Displays the Outcome to the outcome text view 
     public void displayOutcome(String n) { 
      TextView outcomeTextView = (TextView) findViewById(
        R.id.outcome_text_view); 
      outcomeTextView.setText(outcome); 
     } 

     /** 
     * This method displays the given quantity value on the screen. 
     */ 
     public void displayQuantity(int quantity) { 
      TextView quantityTextView = (TextView) findViewById(
        R.id.quantity_text_view); 
      quantityTextView.setText(quantity + ""); 
     } 

} 

的代碼,一旦我運行它,我只是得到看起來像這樣的三個屏幕...

the display

+0

你的意思是這三個選項卡顯示相同的內容? – statosdotcom

+0

@statosdotcom是所有三個選項卡顯示完全相同的視圖,我將如何製作三個單獨的視圖? –

回答

0

裏面你
public Fragment getItem(int position) {
哪裏有你的
public class SectionsPagerAdapter extends FragmentPagerAdapter {
實現此:

@Override 
    public Fragment getItem(int position) { 
     switch (position) { 
      case 0: 
       return new fragment1(); 
      case 1: 
       return new fragment2(); 
      case 2: 
       return new fragment3(); 
      default: 
       // this should never happen 
       return null; 
       //return new Fragment(); 
     } 
    } 

去哪兒「fragment1-2和3」是你定義在那裏運行Java類的名稱。

我正是用這種方式使用我的「getItem」,其中沒有更多。所以我不知道你是否必須在那裏維護你的return PlaceholderFragment.weight(position + 1);。嘗試評論一下,看看會發生什麼。我也不使用createview。最好。

+0

謝謝你這個工作,我刪除了返回PlaceholderFragment.weight(position + 1);那是由Android Studio生成的啓動器代碼 –

+0

謝謝Fidel_Willis。如Sampaio所述,也請查看createview。這確實會導致不可預知的行爲。保持它的男人。 – statosdotcom

+0

我希望我能爲您的答案投票,但我沒有足夠的代表。 –

1

的問題是在這裏

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

你總是誇大水化佈局,在每一個片段

+0

好點。恭喜。 – statosdotcom

+0

應用程序仍然可以不使用這整個班級工作?我可以刪除整件事嗎? –

+0

我的意見:是的。 – statosdotcom