1

我正在尝试将片段和滑动与 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"
        />

我在一个页面(关于 Fragments)中看到了 onAttach() 方法,但我不知道应该在哪里使用,或者它是否是正确的方法。我只想将一个监听器附加到按钮@+id/coursesAjoutListe 并填充我的listView @+id/listViewCoursesListe。

非常感谢:)

4

1 回答 1

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



    }

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

于 2013-01-24T07:01:39.150 回答