[컴][안드로이드] ViewPager 예제



ViewPagerTestActivity.java

/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.com.mugallery;







import android.support.v4.app.FragmentActivity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;


import android.app.Activity;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

/**
 * Demonstrates a fragment that can be configured through both Bundle arguments
 * and layout attributes.
 */
public class ViewPagerTestActivity extends FragmentActivity {

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.viewpager_main);

  ViewPager pager=(ViewPager)findViewById(R.id.pager);

  pager.setAdapter(new SampleAdapter(getSupportFragmentManager()));
 }

  

    public class SampleAdapter extends FragmentPagerAdapter {
     public SampleAdapter(FragmentManager mgr) {
      super(mgr);
     }

     @Override
     public int getCount() {
      /** max value **/
      return(10);
     }

     @Override
     public Fragment getItem(int position) {
      return(MyFragment.newInstance(position));
     }
    }

    
    
    
    public static class MyFragment extends Fragment {
     CharSequence mLabel;

     /**
      * Create a new instance of MyFragment that will be initialized
      * with the given arguments.
      */
     static MyFragment newInstance(int position) {
      MyFragment frag=new MyFragment();
      Bundle args=new Bundle();

      args.putInt("position", position);
      frag.setArguments(args);

      return(frag);

     }


     /**
      * Create the view for this fragment, using the arguments given to it.
      */
     @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
       Bundle savedInstanceState) {

      View result=inflater.inflate(R.layout.viewpager_editor, container, false);
      EditText editor=(EditText)result.findViewById(R.id.editor);
      int position=getArguments().getInt("position", -1);

      editor.setHint(String.format("Editor #%d", position + 1));

      return(result);


     }
    }// end of MyFragment

}

viewpager_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/pager"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:paddingLeft="10dp"
 android:paddingRight="10dp"
 android:background="@android:color/black">
</android.support.v4.view.ViewPager>

viewpager_editor.xml

<EditText xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/editor"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:inputType="textMultiLine"
 android:gravity="left|top"
 />


NotifyDataSetChanged


ViewPager 에서 notifyDataChangeSet() 이 동작하도록 하는 방법,
ViewPager 를 refresh 하는 방법


http://stackoverflow.com/questions/10849552/android-viewpager-cant-update-dynamically/10852046#10852046


See Also

  1. https://github.com/commonsguy/cw-omnibus/tree/master/ViewPager/Fragments

댓글 없음:

댓글 쓰기