java android上的滑动导航示例?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14208211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 15:28:18  来源:igfitidea点击:

Example of swipe navigation on android?

javaandroid

提问by lisovaccaro

I'm trying to figure out how to create three views for lateral navigation but the examples provided by developer.android.comare useless since they are only for API 11 forward. I've been looking for an easy copy/paste example of swipe navigation but I haven't had any luck.

我试图弄清楚如何为横向导航创建三个视图,但是developer.android.com提供的示例没有用,因为它们仅用于 API 11 转发。我一直在寻找滑动导航的简单复制/粘贴示例,但我没有任何运气。

I have found tutorials to swype between activities, or different layouts but it's not what I want to do, I need to generate each "screen" programatically. This is because I'm working on something that should work like the home screen, and the number of screens may vary.

我找到了在活动或不同布局之间切换的教程,但这不是我想要做的,我需要以编程方式生成每个“屏幕”。这是因为我正在处理一些应该像主屏幕一样工作的东西,并且屏幕的数量可能会有所不同。

Could somebody provide some example/tutorial on how to create three views with lateral navigation programatically? Anything will help.

有人可以提供一些关于如何以编程方式创建带有横向导航的三个视图的示例/教程吗?任何事情都会有所帮助。

回答by lisovaccaro

This is the main activity:

这是主要活动:

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MyPagerAdapter adapter = new MyPagerAdapter();
        ViewPager myPager = (ViewPager) findViewById(R.id.home_pannels_pager);
        myPager.setAdapter(adapter);
        myPager.setCurrentItem(0);
    }

}

Create another class called MyPagerAdapter in the same package:

在同一个包中创建另一个名为 MyPagerAdapter 的类:

    public class MyPagerAdapter extends PagerAdapter {

        // State number of pages
        public int getCount() {
            return 5;
        }

        // Set each screen's content
        @Override
        public Object instantiateItem(View container, int position) {
            Context context = container.getContext();
            LinearLayout layout = new LinearLayout(context);
            // Add elements
            TextView textItem = new TextView(context);

            buttonItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent("com.phone");
                   // myFancyMethod(v);
                }
            });


            switch (position) {
            case 0:
               textItem.setText("First Screen");
               break;
            case 1:
               textItem.setText("Second Screen");
               break;
            case 2:
                textItem.setText("Third Screen");
                break;


  case 3:
            textItem.setText("Fourth Screen");
            break;
        case 4:
            textItem.setText("Fifth Screen");
            break;
        }
        layout.addView(textItem);
        ((ViewPager) container).addView(layout, 0); // This is the line I added
        return layout;
    }
    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
        ((ViewPager) arg0).removeView((View) arg2);
    }
    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == ((View) arg1);
    }
    @Override
    public Parcelable saveState() {
        return null;
    }
}

Finally create activity_main.xml inside /res/layout:

最后在 /res/layout 中创建 activity_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.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/home_pannels_pager"/>
</LinearLayout>

回答by bhavesh N

Visit the siteits give better library for swipe view navigation

访问该站点,它为滑动视图导航提供了更好的库