Android: ViewPagerIndicator - Creating different layouts for different pages

Deadly 提交于 2019-12-06 01:28:30

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;
    }

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

Now in your onCreateView() change it as follows,

   @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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!