1

这里我的 activity_main.xml 出现在 /res/layout

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <!--
    This title strip will display the currently visible page title, as well as the page
    titles for adjacent pages.


    -->

    <android.support.v4.view.PagerTitleStrip
        android:id="@+id/pager_title_strip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="#33b5e5"
        android:paddingBottom="4dp"
        android:paddingTop="4dp"
        android:textColor="#fff" />

</android.support.v4.view.ViewPager>

这是我的 MainActivity.java 文件

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;

import android.view.ViewGroup;

public class MainActivity extends FragmentActivity {

    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_main);

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the app.
        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.activity_main, menu);
        return true;
    }

    /**
     * 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 DummySectionFragment (defined as a static inner class
            // below) with the page number as its lone argument.
            Fragment fragment = new DummySectionFragment();
            Bundle args = new Bundle();
            args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
            fragment.setArguments(args);
            return fragment;
        }

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

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

    /**
     * A dummy fragment representing a section of the app, but that simply
     * displays dummy text.
     */
    public static class DummySectionFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        public static final String ARG_SECTION_NUMBER = "section_number";

        public DummySectionFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View view;
            String i = Integer.toString(getArguments().getInt(
                    ARG_SECTION_NUMBER));

            if (i.toString() == Integer.toString(1)) {
                view = inflater.inflate(R.layout.section2, container, false);
            } else {
                view = inflater.inflate(R.layout.section1, container, false);


                // Button button = (Button) view.findViewById(R.id.button1);
                // button.setOnClickListener(new OnClickListener() {
                // if i uncomment this code, button click works but i want it to handle in section1.java file
                // @Override
                // public void onClick(View v) {
                // Activity activity = getActivity();
                //
                // if (activity != null) {
                // Toast.makeText(activity, "sdfsdfsdfds",
                // Toast.LENGTH_LONG).show();
                // }
                // }
                //
                // });

            }

            return view;

        }
    }

}

我有一个名为“Section1”的活动,当我向右滑动时会显示该活动。它上面有一个按钮。如何处理 section1.java 文件中的按钮单击事件?

这是我的 section.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Section1" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="sendMessage"
        android:text="Button" />

</RelativeLayout>

这是 Section1.Java 文件。即使没有触发,也可以单击此处的按钮。

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class Section1 extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.section1);

    }

    public void sendMessage(View view) {
        Toast t = Toast.makeText(getApplicationContext(), "sdfsdfsdf",
                Toast.LENGTH_LONG);
        t.show();
    }

}
4

2 回答 2

4

我和你有同样的问题,今天终于解决了,所以我想我会和你分享

public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int i) {
        switch (i) {
            case 2:
                // The first section of the app is the most interesting -- it offers
                // a launchpad into the other demonstrations in this example application.
                return new LessonFragment();

            default:
                // The other sections of the app are dummy placeholders.
                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;
    }   
}




/**
 * A fragment that launches other parts of the demo application.
 */
public static class LessonFragment extends Fragment {

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

        // Demonstration of a collection-browsing activity.
        rootView.findViewById(R.id.button1)
                .setOnClickListener(new View.OnClickListener() {
                    public void onClick(View view) {
                        Intent intent = new Intent(getActivity(), Test.class);
                        startActivity(intent);
                    }
                });



        return rootView;
    }
}
于 2013-03-24T17:23:33.057 回答
0
    @Override
    public Fragment getItem(int position) {
     switch(position){
        case 0 : 
        case 1 : 
                  // and so on.
       }
        Fragment fragment = new DummySectionFragment();
        Bundle args = new Bundle();
        args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
        fragment.setArguments(args);
        return fragment;
    }
于 2013-07-07T18:27:10.030 回答