4

我正在按照这个链接做一个 ViewPager http://blog.stylingandroid.com/archives/537 我设法得到了各个页面。但是如何使用按钮填充或填充这些页面?

实例化项目()

这将为给定位置创建视图。对于真正的应用程序,我们会在这里使用 Fragment 或扩展布局,但为了保持示例简单,我们创建一个 TextView,将文本设置为我们的标题数组中的正确值,并将其添加到 ViewPager

我想在不同的页面上有不同的按钮功能。我该怎么做呢?通过使用不同的布局?我如何将布局放在页面中(这就是你所说的膨胀)?

编辑:我有由 ViewPager 组成的 main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1">

    <com.viewpagerindicator.TitlePageIndicator
        android:id="@+id/indicator"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />        
</LinearLayout>

我还有其他几个 xml,其中包含我想将其膨胀到 ViewPage 的不同页面中的按钮。xml 示例。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button android:layout_width="match_parent" android:text="Button 1" android:onClick="onClick" android:id="@+id/button1" android:layout_height="100dp"></Button>
    <Button android:text="Button 2" android:onClick="onClick" android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="100dp"></Button>
    <Button android:text="Button 3" android:onClick="onClick" android:id="@+id/button3" android:layout_width="match_parent" android:layout_height="100dp"></Button>
    <Button android:text="Button 4" android:onClick="onClick" android:id="@+id/button4" android:layout_width="match_parent" android:layout_height="100dp"></Button>
    <Button android:text="Button 5" android:onClick="onClick" android:id="@+id/button5" android:layout_height="match_parent" android:layout_width="match_parent"></Button>

</LinearLayout>

在 ViewPagerAdapter.java 中,我设法将各种 XML 膨胀到页面中。

@Override
public Object instantiateItem( View pager, int position )
{
    //Inflate the correct layout
    LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    //layouts[] is an int[] that points to resources such as R.layout.start_page
    View inflatedView = layoutInflater.inflate(layouts[position], null);

    ((ViewPager)pager).addView(inflatedView,0);

    return inflatedView;

}

这需要更改以防止错误。

@Override
public void destroyItem( View pager, int position, Object view )
{
    ((ViewPager)pager).removeView( (View)view );
}

现在,我在不同的页面上有多个按钮。如何对不同页面上不同按钮的 ONCLICK 功能进行编程?这能行吗?

public void onClick(View v) {
        switch (v.getId()) {
        case R.id.button1:
            // do something
            break;
        case R.id.button2:
            // do something
            break;
        case R.id.button3:
            // do something
            break;
        case R.id.button4:
            // do something
            break;
        case R.id.button5:
            // do something
            break;
        }
    }
4

2 回答 2

2

这是一个使用按钮膨胀/以编程方式添加视图的示例:

@Override
//Instantiate items based on corresponding layout IDs that were passed in
public Object instantiateItem(View container, int position)
{
    //Inflate the correct layout
    LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    //layouts[] is an int[] that points to resources such as R.layout.start_page
    View inflatedView = layoutInflater.inflate(layouts[position], null);

    ((ViewPager)container).addView(inflatedView,0);

    //If you can't/don't want to have the above inflated layout include everything up front, you can add them now...
    LinearLayout ll = (LinearLayout)inflatedView.findViewById(R.id.linearLayout1);
    Button newButton = new Button(context);
    newButton.setText(R.string.button_text);
    ll.addView(newButton);

    return inflatedView;
}
于 2012-05-14T15:57:12.900 回答
1

我最近建立了一个带有 ViewPage r 的系统。我使用操作栏选项卡(带有兼容性库)、TabAdapter 和 ViewPager。每个选项卡都是它自己的片段,并通过主要活动添加到栏中。下面是一些关键代码:

public class Polling extends FragmentActivity {
private ViewPager mViewPager;
private TabsAdapter mTabsAdapter;
private final static String TAG = "21st Polling:";

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mViewPager = new ViewPager(this);
mViewPager.setId(R.id.pager);
setContentView(mViewPager);
final ActionBar bar = getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayShowTitleEnabled(false);
bar.setDisplayShowHomeEnabled(false);

mTabsAdapter = new TabsAdapter(this, mViewPager);
mTabsAdapter.addTab(bar.newTab().setText(R.string.login),
LoginFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText(R.string.economics),
EconFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText(R.string.elections),
ElectionsFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText(R.string.politics),
PoliticsFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText(R.string.science),
ScienceFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText(R.string.finance),
FinanceFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText(R.string.religion),
ReligionFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText(R.string.military),
MilitaryFragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText(R.string.international),
InternationalFragment.class, null);
}

和适配器:

public static class TabsAdapter extends FragmentPagerAdapter
    implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
        private final Context mContext;
        private final ActionBar mActionBar;
        private final ViewPager mViewPager;
        private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

        static final class TabInfo {
            private final Class<?> clss;
            private final Bundle args;

            TabInfo(Class<?> _class, Bundle _args) {
                clss = _class;
                args = _args;
            }
        }

        public TabsAdapter(FragmentActivity activity, ViewPager pager) {
            super(activity.getSupportFragmentManager());
            mContext = activity;
            mActionBar = activity.getActionBar();
            mViewPager = pager;
            mViewPager.setAdapter(this);
            mViewPager.setOnPageChangeListener(this);
        }

        public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
            TabInfo info = new TabInfo(clss, args);
            tab.setTag(info);
            tab.setTabListener(this);
            mTabs.add(info);
            mActionBar.addTab(tab);
            notifyDataSetChanged();
        }


        public int getCount() {
            return mTabs.size();
        }

        public Fragment getItem(int position) {
            TabInfo info = mTabs.get(position);
            return Fragment.instantiate(mContext, info.clss.getName(), info.args);
        }


        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }


        public void onPageSelected(int position) {
            mActionBar.setSelectedNavigationItem(position);
        }


        public void onPageScrollStateChanged(int state) {
        }


        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            mViewPager.setCurrentItem(tab.getPosition());
            Log.v(TAG, "clicked");
            Object tag = tab.getTag();
            for (int i=0; i<mTabs.size(); i++) {
                if (mTabs.get(i) == tag) {
                    mViewPager.setCurrentItem(i);
                }
            }
        }

        public void onTabUnselected(Tab tab, FragmentTransaction ft) {}

        public void onTabReselected(Tab tab, FragmentTransaction ft) {}

        public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {}

        @Override
        public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {    
            Object tag = tab.getTag();
            for (int i=0; i<mTabs.size(); i++) {
                if (mTabs.get(i) == tag) {
                    mViewPager.setCurrentItem(i);
                }
            }
        }

        public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {}
    }

这是一个在每个选项卡上制作按钮和小部件的类:

public class EconFragment extends SherlockFragment {

Polling activity;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    activity = (Polling)getActivity();

    View v = inflater.inflate(R.layout.categoryfragment, container, false);
    this.mainLayout = v;
    return v;
}

只要您编辑在上述代码中扩展的 XML 文件(categoryfragment.xml),每个选项卡都会加载您想要放在页面上的任何内容(TextView、Button 等)。

于 2012-05-15T01:44:37.760 回答