ActionBar で Fragments とスワイプを使用しようとしています。私はそれをすべてやりましたが、リストビューまたはフラグメント内のボタンにアクセスする方法がわかりません。
アクティビティは次のとおりです。
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 ファイル:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/pageAccueil"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/coursesRelativePart1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
>
<Button
android:id="@+id/coursesAjoutListe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:drawableTop="@drawable/folder_icon"
android:layout_alignParentRight="true" />
<LinearLayout
android:id="@+id/coursesLinearTexteDescr"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/coursesAjoutListe"
android:orientation="vertical">
<TextView
android:id="@+id/textViewCoursesTitre"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="@dimen/TitleSize"
android:text="@string/coursesTextTitre"
android:paddingBottom="5dp"/>
<TextView
android:id="@+id/textViewCoursesDescr"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:textSize="@dimen/TextSize"
android:text="@string/coursesTextDescr" />
</LinearLayout>
</RelativeLayout>
<ListView
android:id="@+id/listViewCoursesListe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
(フラグメントに関する) ページで onAttach() メソッドを見ましたが、どこで使用すればよいか、または正しいメソッドであるかどうかがわかりません。リスナーをボタン @+id/coursesAjoutListe にアタッチし、listView @+id/listViewCoursesListe に入力したいだけです。
ありがとうございます:)