2014-10-04 143 views
0

我是Android编程的初学者,我使用eclipse luna。我创建了一项新活动,并选择了选项卡式活动而不是空白活动。它已经有刷卡代码。但有些代码我无法理解。当我滑动时,我不知道如何更改活动。在案例中,我想知道为什么它是字符串而不是活动的参考。使用选项卡式活动刷卡

下面是从标签活动代码:

package com.e_learningforchildren.sarah; 

import java.util.Locale; 

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

public class TopicsActivity extends ActionBarActivity { 

/** 
* 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}. 
*/ 
SectionsPagerAdapter mSectionsPagerAdapter; 

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

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

    // 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.pager); 
    mViewPager.setAdapter(mSectionsPagerAdapter); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.topics, 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(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

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

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

    @Override 
    public CharSequence getPageTitle(int position) { 
     Locale l = Locale.getDefault(); 
     switch (position) { 
     case 0: 
     return getString(R.string.title_section1).toUpperCase(l); 

     case 1: 
      return getString(R.string.title_section2).toUpperCase(l); 
     case 2: 
      return getString(R.string.title_section3).toUpperCase(l); 
     } 
     return null; 
    } 
} 

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

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

    public PlaceholderFragment() { 
    } 

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

} 

回答

0

其实你不刷卡活动,你只是刷片段和字符串是网页标题。

我想你应该了解片段以及如何管理或替换它们。

添加你的链接 http://developer.android.com/guide/components/fragments.html

+0

好吧,以便其Activity不会碎片。这是我在创建新活动时所做的:新建>其他> Android活动>选项卡活动>完成。我只是感到困惑,因为选项卡活动已写入,但在选择选项卡式活动后出现片段。 – 2014-10-06 12:19:26

0

如果你刚开始,我不建议使用刷卡例如去第一,刷卡是“片段”不是“活动”,这是相当先进如果您是Android开发人员的初学者,请联系主题。

我建议您阅读本教程,因为它会教会您需要了解的活动,片段,操作栏,菜单,布局组件等等。最终,您将拥有一个坚实的基础,你只需要建立在顶部。 (自从使用Eclipse以来,忽略关于命令行的部分)。

https://developer.android.com/training/basics/firstapp/creating-project.html

希望这会有所帮助,祝你好运。

+0

我做了另一项研究,是的,它是片段而不是活动。感谢您的链接,这是很大的帮助。 @NightwareSystems – 2014-10-05 13:37:11

1

这里是我的刷卡标签的代码片段 不包括进口

public class MainActivity extends FragmentActivity { 

    ViewPager viewpager; 
    ActionBar actionbar; 
    String[] tabs={"temperature","information"}; 
    tabpageadapter adapterm; 




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

     viewpager=(ViewPager)findViewById(R.id.pager); 

     actionbar=getActionBar(); 

     adapterm= new tabpageadapter(getSupportFragmentManager()); 

     viewpager.setAdapter(adapterm); 

     actionbar.setHomeButtonEnabled(false); 
     actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

     actionbar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#1F2230"))); 

     actionbar.setStackedBackgroundDrawable(new ColorDrawable(Color.parseColor("#323445"))); 


     for(String tabb:tabs) 
     { 
      actionbar.addTab(actionbar.newTab().setText(tabb).setTabListener(this)); 
     } 


     viewpager.setOnPageChangeListener(this); 




    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 
} 

这是viewpager页适配器

public class tabpageadapter extends FragmentPagerAdapter{ 

public tabpageadapter(FragmentManager fm) { 
    super(fm); 
    // TODO Auto-generated constructor stub 
} 

@Override 
public Fragment getItem(int index) { 
    // TODO Auto-generated method stub 
    switch(index) 

    { 
    case 0: 
     return new temperature_fragment(); 

    case 1: 
     return new information_fragment(); 




    } 


    return null; 
} 

@Override 
public int getCount() { 
    // TODO Auto-generated method stub 
    return 2; 
} 

} 

这是一个片段文件只是创建另一个差异的名称,以便2个选项卡将可用

public class temperature_fragment extends Fragment{ 

    TextView tvtemp,farenhit; 


    LinearLayout llfortemp; 
    Intent inte; 

    BatteryManager bm; 
    double i,j; 
    int l,k; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 

     View rootview=inflater.inflate(R.layout.temperature_xml, container,false); 


     Context cc=getActivity(); 

     tvtemp=(TextView)rootview.findViewById(R.id.texttemp); 
     farenhit=(TextView)rootview.findViewById(R.id.textViewfarenhit); 
     llfortemp=(LinearLayout)rootview.findViewById(R.id.llfortemp); 






     return rootview; 
    } 











     } 
+0

我搜索了一下滑动,并且有很多方法可以做到。感谢您的代码,我会尝试。 – 2014-10-05 13:40:25

相关问题