2012-07-12 3 views
1

나는 조각 모음과 스 와이프로 ActionBar를 사용하려고합니다. 나는 그 모든 것을했지만, 나는 ListView 나 Fragment의 버튼에 접근하는 방법을 이해하지 못한다.이해하기 쉬움 조각/탭 + ActionBar + 스 와이프 (ICS)

public class MainActivity extends FragmentActivity implements ActionBar.TabListener { 


SectionsPagerAdapter mSectionsPagerAdapter; 

ViewPager mViewPager; 
private static int SCAN = 1; 

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

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 

    // Set up the action bar. 
    final ActionBar actionBar = getActionBar(); 
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

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


    // -------------------- Swipe ----------------------- // 

    // When swiping between different sections, select the corresponding tab. 
    // We can also use ActionBar.Tab#select() to do this if we have a reference to the 
    // Tab. 
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { 
     @Override 
     public void onPageSelected(int position) { 
      actionBar.setSelectedNavigationItem(position); 
     } 
    }); 

    // For each of the sections in the app, add a tab to the action bar. 
    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) { 
     actionBar.addTab(
       actionBar.newTab() 
         .setText(mSectionsPagerAdapter.getPageTitle(i)) 
         .setTabListener(this)); 
    } 

} 

// ------------------------- Methodes On du menu --------------------------- // 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 

// Ecouteurs qui correspondent à chaque bouton présent dans le menu 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.menu_camera: 

      // app icon in action bar clicked; go home 
      Intent intent = new Intent(this, ScanBarActivity.class); 
      intent.putExtra("METHODE", "SCAN"); 
      startActivityForResult(intent, SCAN); 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
    } 
} 


// ------------------------- Methodes On des Tab ----------------------------// 

public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 
} 

public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 
    mViewPager.setCurrentItem(tab.getPosition()); 
} 

public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 
} 

public class SectionsPagerAdapter extends FragmentPagerAdapter { 

    public SectionsPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int i) { 
     Fragment fragment = new DummySectionFragment(); 
     Bundle args = new Bundle(); 
     args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    @Override 
    public int getCount() { 
     return 4; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     switch (position) { 
      case 0: return getString(R.string.title_section1).toUpperCase(); 
      case 1: return getString(R.string.title_section2).toUpperCase(); 
      case 2: return getString(R.string.title_section3).toUpperCase(); 
      case 3: return getString(R.string.title_section4).toUpperCase(); 
     } 
     return null; 
    } 
} 

}

DummySectionFragment :

public class DummySectionFragment extends Fragment { 
public DummySectionFragment() { 
} 

public static final String ARG_SECTION_NUMBER = "section_number"; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    Bundle args = getArguments(); 
    switch (args.getInt(ARG_SECTION_NUMBER)){ 
    case 1: 

     return inflater.inflate(R.layout.page_accueil, container, false); 
    case 2: 
     return inflater.inflate(R.layout.page_courses, container, false); 
    case 3: 

     break; 
    case 4: 

     break; 
    } 
    TextView textView = new TextView(getActivity()); 
    textView.setGravity(Gravity.CENTER); 
    textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER))); 
    return textView; 

} 

}

그리고 page_courses.xml에 대한 XML 파일 :

여기

는 활동입니다

나는 (조각에 대한) 페이지에서 onAttach() 메소드를 보았다하지만 올바른 방법인지 나는 사용해야 어디 있는지, 또는하지 않습니다. 난 그냥 @ + id/coursesAjoutListe 버튼에 리스너를 연결하고 내 listView @ + id/listViewCoursesListe를 채우고 싶습니다.)이 DummyFragment의 ONSTART을 (오버라이드 (override) 방법 및 getActivity()를 사용하는 모든 :)

답변

1

시도

감사합니다. findViewById를() .. 당신이 분리 할 수 ​​있지만 조언의

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     ... 
    } 

    @Override 
    public void onStart() { 
     // TODO Auto-generated method stub 
     super.onStart(); 
     switch (args.getInt(ARG_SECTION_NUMBER)){ 
     case 1: 

      break; 
     case 2: 
      Button btncoursesAjoutListe = (Button) getActivity().findViewById(R.id.coursesAjoutListe); 
      ListView lvlistViewCoursesListe = (ListView) getActivity().findViewById(R.id.listViewCoursesListe); 

      btncoursesAjoutListe.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        PopulateListview(lvlistViewCoursesListe) 
       } 
      }); 
     case 3: 

      break; 
     case 4: 

      break; 
     } 



    } 

비트 당신의 페이지를 다른 조각을 통해 .. 그런 식으로 당신의 모든 재정의 스위치를 사용하지 않아도됩니다.