迷失在理解碎片/标签+ ActionBar +刷卡(ICS)

时间:2012-07-12 13:08:53

标签: java android android-actionbar android-4.0-ice-cream-sandwich swipe

我正在尝试使用ActionBar片段和滑动。我做了所有这些,但我不明白如何访问listView或片段中的按钮。

这是活动:

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。

全部谢谢:)

1 个答案:

答案 0 :(得分:1)

尝试覆盖DummyFragment的onStart()方法,并使用getActivity()。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;
        }



    }

虽然有点建议..你可以通过不同的片段分隔你的页面..这样你就不必在所有覆盖上使用那个开关。