-1

我正在应用网上看到的例子到我的代码,实现介绍页面(ViewPager与4页)。 为此,我使用Android Studio在创建新活动时提供的本机代码。我的代码似乎在我的开关情况下去两次

我适于下面的代码根据我们的片段texte即写:视

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

为了下面的代码,用一个开关,以显示不同的东西(按钮颜色,texte,图标)在片段我们的:

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     Context context = getContext(); 

     View rootView = inflater.inflate(R.layout.fragment_welcome, container, false); 
     TextView textViewTitle = (TextView) rootView.findViewById(R.id.txtViewWelcomeTitle); 
     TextView textViewDesc = (TextView) rootView.findViewById(R.id.txtViewWelcomeDesc); 
     ImageView imageView = (ImageView) rootView.findViewById(R.id.imageViewWelcome); 

     switch (getArguments().getInt(ARG_SECTION_NUMBER)) 
     { 
      case 1: 

       imageView.setImageResource(R.drawable.ic_menu_camera); 
       textViewTitle.setText(R.string.fragment_1_title); 
       textViewDesc.setText(R.string.fragment_1_desc); 

       rootView.setBackgroundColor(ContextCompat.getColor(context, R.color.bg_screen1)); 
       btnNext.setText(R.string.next); 
       btnSkip.setVisibility(View.VISIBLE); 

       /*btnNext.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         // launch next page of the fragment. 

        } 
       });*/ 

       break; 

      case 2: 

       imageView.setImageResource(R.drawable.ic_menu_gallery); 
       textViewTitle.setText(R.string.fragment_2_title); 
       textViewDesc.setText(R.string.fragment_2_desc); 

       rootView.setBackgroundColor(ContextCompat.getColor(context, R.color.bg_screen2)); 
       btnNext.setText(R.string.next); 
       btnSkip.setVisibility(View.VISIBLE); 

       break; 

      case 3: 

       imageView.setImageResource(R.drawable.ic_menu_manage); 
       textViewTitle.setText(R.string.fragment_3_title); 
       textViewDesc.setText(R.string.fragment_3_desc); 

       rootView.setBackgroundColor(ContextCompat.getColor(context, R.color.bg_screen3)); 
       btnNext.setText(R.string.next); 
       btnSkip.setVisibility(View.VISIBLE); 

       break; 

      case 4: 

       imageView.setImageResource(R.drawable.ic_menu_send); 
       textViewTitle.setText(R.string.fragment_4_title); 
       textViewDesc.setText(R.string.fragment_4_desc); 

       rootView.setBackgroundColor(ContextCompat.getColor(context, R.color.bg_screen4)); 
       btnNext.setText(R.string.start); 
       btnSkip.setVisibility(View.INVISIBLE); 

       break; 
     } 
     return rootView; 
    } 

我是跳跃看到的是4页,对他们的3两个键(跳跃和Next),和4号开始按钮。

我看到:2按钮仅在第2页,和“开始”按钮,在3页和4

当我调试我的应用程序,似乎我的代码经过的情况下1,然后通过案例2,然后我的应用程序显示案例1页面。 这意味着,当我切换时,我的页面似乎应该是+1的位置(我的页面1最终是2,2是3,...)。 奇怪的是,我的texte和图标显示是正确的。

以下,对完成WelcomActivity:

public class WelcomeActivity extends AppCompatActivity { 

    /** 
    * 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; 

    public static final String PREFS_NAME = "StartPref"; 
    private PrefManager prefManager; 
    private static LinearLayout dotsLayout; 
    private static TextView[] dots; 
    private static Button btnSkip, btnNext; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 

     // Checking for first time launch - before calling setContentView() 
/*  prefManager = new PrefManager(this); 
     if (!prefManager.isFirstTimeLaunch()) { 
      goToHomePage(); 
      finish(); 
     } 
*/ 
     if (Build.VERSION.SDK_INT >= 21) { 
      getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); 
     } 

     setContentView(R.layout.activity_welcome); 

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

     dotsLayout = (LinearLayout) findViewById(R.id.layoutDots); 
     btnSkip = (Button) findViewById(R.id.btn_skip); 
     btnNext = (Button) findViewById(R.id.btn_next); 

     // making notification bar transparent 
     changeStatusBarColor(); 

     btnSkip.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       goToHomePage(v); 
      } 
     }); 
    } 


    @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_welcome, 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) { 
      Context context = getContext(); 

      View rootView = inflater.inflate(R.layout.fragment_welcome, container, false); 
      TextView textViewTitle = (TextView) rootView.findViewById(R.id.txtViewWelcomeTitle); 
      TextView textViewDesc = (TextView) rootView.findViewById(R.id.txtViewWelcomeDesc); 
      ImageView imageView = (ImageView) rootView.findViewById(R.id.imageViewWelcome); 

      switch (getArguments().getInt(ARG_SECTION_NUMBER)) 
      { 
       case 1: 

        imageView.setImageResource(R.drawable.ic_menu_camera); 
        textViewTitle.setText(R.string.fragment_1_title); 
        textViewDesc.setText(R.string.fragment_1_desc); 

        rootView.setBackgroundColor(ContextCompat.getColor(context, R.color.bg_screen1)); 
        btnNext.setText(R.string.next); 
        btnSkip.setVisibility(View.VISIBLE); 

        /*btnNext.setOnClickListener(new View.OnClickListener() { 
         @Override 
         public void onClick(View v) { 
          // launch next page of the fragment. 

         } 
        });*/ 

        break; 

       case 2: 

        imageView.setImageResource(R.drawable.ic_menu_gallery); 
        textViewTitle.setText(R.string.fragment_2_title); 
        textViewDesc.setText(R.string.fragment_2_desc); 

        rootView.setBackgroundColor(ContextCompat.getColor(context, R.color.bg_screen2)); 
        btnNext.setText(R.string.next); 
        btnSkip.setVisibility(View.VISIBLE); 

        break; 

       case 3: 

        imageView.setImageResource(R.drawable.ic_menu_manage); 
        textViewTitle.setText(R.string.fragment_3_title); 
        textViewDesc.setText(R.string.fragment_3_desc); 

        rootView.setBackgroundColor(ContextCompat.getColor(context, R.color.bg_screen3)); 
        btnNext.setText(R.string.next); 
        btnSkip.setVisibility(View.VISIBLE); 

        break; 

       case 4: 

        imageView.setImageResource(R.drawable.ic_menu_send); 
        textViewTitle.setText(R.string.fragment_4_title); 
        textViewDesc.setText(R.string.fragment_4_desc); 

        rootView.setBackgroundColor(ContextCompat.getColor(context, R.color.bg_screen4)); 
        btnNext.setText(R.string.start); 
        btnSkip.setVisibility(View.INVISIBLE); 

        break; 
      } 
      return rootView; 
     } 
    } 

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

     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.newInstance(position + 1); 
     } 

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

     @Override 
     public CharSequence getPageTitle(int position) { 
      switch (position) { 
       case 0: 
        return "SECTION 1"; 
       case 1: 
        return "SECTION 2"; 
       case 2: 
        return "SECTION 3"; 
       case 3: 
        return "SECTION 4"; 
      } 
      return null; 
     } 
    } 

    public void goToHomePage(View view) { 
     prefManager.setFirstTimeLaunch(false); 
     Intent intent = new Intent(this, HomePageActivity.class); 
     startActivity(intent); 
    } 

    /** 
    * Making notification bar transparent 
    */ 
    private void changeStatusBarColor() { 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
      Window window = getWindow(); 
      window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); 
      window.setStatusBarColor(Color.TRANSPARENT); 
     } 
    } 

    private void addBottomDots(int currentPage) { 
     dots = new TextView[mSectionsPagerAdapter.getCount()]; 

     int[] colorsActive = getResources().getIntArray(R.array.array_dot_active); 
     int[] colorsInactive = getResources().getIntArray(R.array.array_dot_inactive); 

     dotsLayout.removeAllViews(); 
     for (int i = 0; i < dots.length; i++) { 
      dots[i] = new TextView(this); 
      dots[i].setText(Html.fromHtml("&#8226;")); 
      dots[i].setTextSize(35); 
      dots[i].setTextColor(colorsInactive[currentPage]); 
      dotsLayout.addView(dots[i]); 
     } 

     if (dots.length > 0) 
      dots[currentPage].setTextColor(colorsActive[currentPage]); 
    } 
} 

,这里是在fragment_welcome.xml,那么activity_welcome.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@color/bg_screen1" 
tools:context="com.example.avescera.remindme.WelcomeActivity$PlaceholderFragment" > 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:gravity="center_horizontal" 
    android:orientation="vertical"> 

    <ImageView 
     android:layout_width="@dimen/img_width_height" 
     android:layout_height="@dimen/img_width_height" 
     android:src="@drawable/ic_menu_manage" 
     android:id="@+id/imageViewWelcome" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="@android:color/white" 
     android:textSize="@dimen/slide_title" 
     android:textStyle="bold" 
     android:id="@+id/txtViewWelcomeTitle" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="20dp" 
     android:paddingLeft="@dimen/desc_padding" 
     android:paddingRight="@dimen/desc_padding" 
     android:textAlignment="center" 
     android:textColor="@android:color/white" 
     android:textSize="@dimen/slide_desc" 
     android:id="@+id/txtViewWelcomeDesc" /> 

</LinearLayout> 

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/main_content" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="com.example.avescera.remindme.WelcomeActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/appbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="@dimen/appbar_padding_top" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:layout_scrollFlags="scroll|enterAlways" 
      app:popupTheme="@style/AppTheme.PopupOverlay"> 

     </android.support.v7.widget.Toolbar> 

    </android.support.design.widget.AppBarLayout> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

    <LinearLayout 
     android:id="@+id/layoutDots" 
     android:layout_width="match_parent" 
     android:layout_height="@dimen/dots_height" 
     android:layout_marginBottom="@dimen/dots_margin_bottom" 
     android:gravity="center" 
     android:orientation="horizontal" 
     android:layout_alignParentBottom="false" 
     android:layout_below="@+id/viewDots" 
     android:layout_gravity="bottom|center_horizontal"></LinearLayout> 

    <View 
     android:id="@+id/viewDots" 
     android:layout_width="match_parent" 
     android:layout_height="1dp" 
     android:alpha=".5" 
     android:background="@android:color/white" 
     android:layout_gravity="bottom|center" /> 

    <Button 
     android:id="@+id/btn_next" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:background="@null" 
     android:text="@string/next" 
     android:textColor="@android:color/white" 
     android:layout_gravity="bottom|left" /> 

    <Button 
     android:id="@+id/btn_skip" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:background="@null" 
     android:text="@string/skip" 
     android:textColor="@android:color/white" 
     android:layout_gravity="bottom|right" /> 

</android.support.design.widget.CoordinatorLayout> 

如果您需要任何furhter信息, 不要犹豫。

亚历

+0

为了提供更多信息(因为我一直试图解决我的问题),显示的texte(textView)和按钮之间存在差异。由于按钮位于容器(activity_welcome.xml)中,所以使用的textView位于片段中。试图找到一个解决方法。 – Alexandre

回答

1

终于让我找到如何回答我的问题(对于那些谁也有兴趣在解决这个问题)。

首先,一些更多的细节。我使用了android studio(2.2)提供的内置活动tabbed-activity。这一个允许使用片段分页器,并且它的行为似乎(例如显示第一个片段,但在您的代码中,您已经在第二个片段中,但没有正确理解这是如何工作的)。对我来说,我想有4页(我的片段分页器有4个部分)和2个按钮在底部(附加到活动xml而不是片段xml)。

解决方案是simlpe:在onCreateView()部分添加片段文本更新,在OnCreate()部分添加按钮文本更新(和onClickListener)。

在OnCreate()部分,要知道使用哪个页面片段,您必须使用ViewPager和addOnPageChangeListener。然后就可以了,您可以在其中添加一个开关,以了解哪个页面,检查位置的值(从0到您所拥有的页数-1)。

希望这会有所帮助。