0

我想要一个像这样在单个活动中有多个页面的应用程序

我正在尝试制作一个包含许多笑话的简单应用程序,如果我点击下一个按钮应该导航到下一个文本(笑话),怎么可能在谷歌和这个网站上搜索很多但我不知道,我Textswitcher也在找,但这不是我真正想要的,请帮助我..

 package com.example.sona;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TabHost th=(TabHost) findViewById(android.R.id.tabhost);
        th.setup();

        TabSpec ts=th.newTabSpec("Tab1");
        ts.setContent(R.id.jokes);
        ts.setIndicator("Jokes");
        th.addTab(ts);

        ts=th.newTabSpec("Tab2");
        ts.setContent(R.id.memories);
        ts.setIndicator("Memories");
        th.addTab(ts);

        ts=th.newTabSpec("Tab3");
        ts.setContent(R.id.About);
        ts.setIndicator("About");
        th.addTab(ts);

        ListView listView=(ListView) findViewById(R.id.list);
        String[] values = new String[] { "Every student gEts opportunity to enjoy their student life.. . . . . . But some misS those beautiful moments.. . Those sTupid people are knwn as . . . . . TOPPERS.","Truth of student life- . . First bench student know to how to answer every problem. . . . . . . But the last bench student knows how to face every problem. ", 
                "Teacher to shere Son Teacher : Babu babu timro gharma sabai vanda sano manche ko cha ????? Reply from ultimate Shere`s son : : Madam madam sabai vanda sano ta Daddy hunu huncha Teacher : Kasari babu ??? Shere's son : daddy jahile mom sanga sutnu huncha ma ta sano xha da nai seperate sutchu but daddy ahile samma ni Mummy sanga sutnu huncha..",
                "Android Example List View" 
               };
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                  android.R.layout.simple_list_item_1, android.R.id.text1, values);
         listView.setAdapter(adapter); 
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}


activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android1="http://schemas.android.com/apk/res/android"
    xmlns:android2="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/sona"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TabHost
        android2:id="@android:id/tabhost"
        android2:layout_width="match_parent"
        android2:layout_height="match_parent"
        android2:layout_alignParentLeft="true"
        android2:layout_alignParentTop="true" >

        <LinearLayout
            android2:layout_width="match_parent"
            android2:layout_height="match_parent"
            android2:orientation="vertical" >

            <TabWidget
                android2:id="@android:id/tabs"
                android2:layout_width="match_parent"
                android2:layout_height="wrap_content" >
            </TabWidget>

            <FrameLayout
                android2:id="@android:id/tabcontent"
                android2:layout_width="match_parent"
                android2:layout_height="match_parent" >

                <LinearLayout
                    android2:id="@+id/jokes"
                    android2:layout_width="match_parent"
                    android2:layout_height="match_parent"
                    android2:orientation="horizontal" >

                    <ScrollView
                        android2:id="@+id/scrollView1"
                        android2:layout_width="wrap_content"
                        android2:layout_height="match_parent" >

                        <LinearLayout
                            android2:layout_width="match_parent"
                            android2:layout_height="match_parent"
                            android2:orientation="vertical" >

                            <ListView
                                android2:id="@+id/list"
                                android2:layout_width="match_parent"
                                android2:layout_height="386dp" >
                            </ListView>

                        </LinearLayout>
                    </ScrollView>



                </LinearLayout>

                <LinearLayout
                    android2:id="@+id/memories"
                    android2:layout_width="match_parent"
                    android2:layout_height="match_parent" 
                    android:orientation="horizontal">
                    <ScrollView
                        android2:id="@+id/scrollView1"
                        android2:layout_width="wrap_content"
                        android2:layout_height="match_parent" >

                        <LinearLayout
                            android2:layout_width="match_parent"
                            android2:layout_height="match_parent"
                            android2:orientation="vertical" >
                     <ImageView

                                android2:id="@+id/imageView1"
                                android2:layout_width="wrap_content"
                                android2:layout_height="wrap_content"
                                android2:src="@drawable/sonapic"
                                 />
                     <ImageView
                                android2:id="@+id/imageView2"
                                android2:layout_width="wrap_content"
                                android2:layout_height="wrap_content"
                                android2:src="@drawable/sab" />
                     <ImageView
                                android2:id="@+id/imageView3"
                                android2:layout_width="wrap_content"
                                android2:layout_height="wrap_content"
                                android2:src="@drawable/sabb" />
                     <ImageView
                                android2:id="@+id/imageView4"
                                android2:layout_width="wrap_content"
                                android2:layout_height="wrap_content"
                                android2:src="@drawable/son" />
                     </LinearLayout>
                    </ScrollView>
                </LinearLayout>

                <LinearLayout
                    android2:id="@+id/About"
                    android2:layout_width="match_parent"
                    android2:layout_height="match_parent" 

                    android:orientation="horizontal">
                      <TextView
                        android2:id="@+id/aboutme"
                        android2:layout_width="wrap_content"
                        android2:layout_height="wrap_content"
                        android2:text="Visiting Card: 

                            http://www.minevisitcard.blogspot.com/


                             My introduction and biodata: 
                             http://nibastuyuu.blogspot.com/" />
                </LinearLayout>
            </FrameLayout>
        </LinearLayout>
    </TabHost>

</RelativeLayout>
4

1 回答 1

0

有一种叫做 ViewPager 的东西将完全适合您的目的!

第一:为笑话页面创建一个xml布局来填充viewpager(即一个带有textviews来填充文本等)。第二:编写一个适配器来用你的内容填充viewpager。第三:在主布局中添加一个 viewpager。第四:将适配器设置为viewpager。

viewpager 适配器的示例代码供您开始使用。

public class ViewPagerAdapter extends PagerAdapter {

Context context;
int[] image;
String[] description;


    LayoutInflater inflater;

    public ViewPagerAdapter(Context context, int[] image,String[] description) {
        this.context = context;
        this.image = image;
        this.description=description;

    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((LinearLayout) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        ImageView img;
        TextView des;

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.slider, container,
                false);

        img = (ImageView) itemView.findViewById(R.id.imageView1);
        img.setBackgroundResource(image[position]);
        des = (TextView) itemView.findViewById(R.id.slider_des);
        des.setText(description[position]);




        ((ViewPager) container).addView(itemView);

        return itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((LinearLayout) object);

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return image.length;
    }

}

上述适配器适用于接受整数数组来存储资源(本例中为图像)的 viewpager。,以及图像标题的字符串数组。

对于适配器的 viewpager 布局,您必须创建一个带有 imageview 和 textview 的布局。

现在在您的活动布局中添加一个 viewpager。

宣布

String[] description={"example 1","example 2","example 3"};
int[] images="R.drawable.one,R.drawable.two,R.drawable.three";

//declare  viewpager
ViewPager vp = (ViewPager) findViewById(R.id.vp);
ViewPagerAdapter adapt= new ViewPagerAdapter(this,images,description);
vp.setAdapter(adapt);

应该是这个!

对于下一个上一个按钮...将它们放在 viewpager 下方的布局中。在 onClick 上写:

vp.setCurrentItem(vp.getCurrentItem() + 1); // for next
vp.setCurrentItem(vp.getCurrentItem() - 1); //for previous
于 2013-10-06T06:57:51.370 回答