安卓:ViewPagerIndicator - 创建不同的布局不同的页面 [英] Android: ViewPagerIndicator - Creating different layouts for different pages

查看:175
本文介绍了安卓:ViewPagerIndicator - 创建不同的布局不同的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建四个不同的布局,每一个ViewPagerIndicator的页面。 我怎样才能做到这一点?该ViewPagerIndicator已经工作,但我使用的 http://viewpagerindicator.com 样品和有创建了一个简单TextView中的所有网页。

I need to create four different layouts, each one to a page of ViewPagerIndicator. How can I do this? The ViewPagerIndicator is already working, but I'm using the sample from http://viewpagerindicator.com and there is created a simple TextView for all pages.

请参阅样本(TestFragment.java):

See the sample (TestFragment.java):

  @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    TextView text = new TextView(getActivity());
    text.setGravity(Gravity.CENTER);
    text.setText(mContent);
    text.setTextSize(20 * getResources().getDisplayMetrics().density);
    text.setPadding(20, 20, 20, 20);

    LinearLayout layout = new LinearLayout(getActivity());
    layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    layout.setGravity(Gravity.CENTER);
    layout.addView(text);

    return layout;
}

我需要找出当前页面(位置),并参考相关资源的布局(XML)。这可能吗?

I need to identify the current page (position) and refer to a related resource layout (XML). Is it possible?

我还需要确保能够一次只有一次加载所有页面的所有视图,当我创建活动,使值稍后更新。

I also need to ensure that all Views of all pages to be loaded at once only one time when I create the activity, allowing values ​​to be updated later.

我AP preciate任何帮助! 谢谢

I appreciate any help!! Thanks

推荐答案

我发现了一个简单的解决方案,为我工作。这是我做的。

I found a simple solution which works for me. Here is what I did.

TestFragment.class

      public static TestFragment newInstance(String content) {
        TestFragment fragment = new TestFragment();
//
//        StringBuilder builder = new StringBuilder();
//        for (int i = 0; i < 20; i++) {
//            builder.append(content).append(" ");
//        }
//        builder.deleteCharAt(builder.length() - 1);
        fragment.mContent = content;

        return fragment;
    }

注释掉的for循环在这里。刚刚获得在 mContent ,我们会作为一个标志使用。

Comment out the for loop here. Just get the mContent which we are going to use as a Flag.

现在在你的 onCreateView()如下更改,

   @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//        TextView text = new TextView(getActivity());
//        text.setGravity(Gravity.CENTER);
//        text.setText(mContent);
//        text.setTextSize(20 * getResources().getDisplayMetrics().density);
//        text.setPadding(20, 20, 20, 20);
//
//        LinearLayout layout = new LinearLayout(getActivity());
//        layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
//        layout.setGravity(Gravity.CENTER);
//        layout.addView(text);
        Log.i("mContent",mContent);
        View view=null;
        if(mContent.equalsIgnoreCase("title1"))
        {
            view = inflater.inflate(R.layout.one, container, false);

        }
        else if(mContent.equalsIgnoreCase("title2"))
        {
            view = inflater.inflate(R.layout.two, container, false);
        }
        else if(mContent.equalsIgnoreCase("title3"))
        {
            view = inflater.inflate(R.layout.three, container, false);
        }
        return view;

    }

这是所有需要。现在,你就可以在此基础上,我们作为标志的标题名称抬高你的看法。

That is all it takes. Now you will be able to inflate your views based on the Title name which we have used as the flag.

这篇关于安卓:ViewPagerIndicator - 创建不同的布局不同的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆