0

AndroidアニメーションのデモのCardFlipActivityのように、要素を反転できるViewPagerに取り組んでいます。beginTransaction() メソッドに問題があります。

私が得た唯一のクイックフィックスオプションは、フラグメントのタイプをからに変更するように求めますandroid.support.v4.app.Fragment.android.app.Fragment そうすると、別のエラーが発生し、タイプをに戻す必要がありますsupport.v4.app.Fragment.

続行するにはどうすればよいですか? フラグメントとビューページャーのコードですべての support.v4 ライブラリ インポートを使用しています。

コード:

package com.example.flag_o;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class QuizGame extends FragmentActivity 
            implements FragmentManager.OnBackStackChangedListener {     


    static DataClass dc;
    static int numQues;
    static int quizLev; 

    static int FLIP_FOR_MAP = 1;
    static int FLIP_FOR_INFO = 2;

    static int frag_status=0;
    static int questions[] = null;

    MenuItem map;
    MenuItem ques;

    private Handler mHandler = new Handler();
    private boolean mShowingBack = false;    

    QuizPagerAdapter quizAdapter;
    ViewPager quizPager;

    static String RETRIEVE_POS = "position";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.quiz_pager);                

        dc = new DataClass();
        numQues = getIntent().getExtras().getInt("numQ");
        quizLev = getIntent().getExtras().getInt("level");

        java.util.List<Integer> randNums = new java.util.ArrayList<Integer>(dc.flags.length);
        for (int i = 0; i <= dc.flags.length; i++)
        {
           randNums.add(new Integer(i));
        }
        java.util.Collections.shuffle(randNums);

        for(int i=0;i<numQues;i++)
            questions[i] = randNums.get(i);

        quizAdapter = new QuizPagerAdapter(getSupportFragmentManager(),questions);
        quizPager = (ViewPager) findViewById(R.id.quizPager);
        quizPager.setAdapter(quizAdapter);

        if (savedInstanceState == null) {
            // If there is no saved instance state, add a fragment representing the
            // front of the card to this activity. If there is saved instance state,
            // this fragment will have already been added to the activity.
            getFragmentManager()
                    .beginTransaction()
                    .add(R.id.container, new FlagFragment())
                    .commit();
        } else {
            mShowingBack = (getFragmentManager().getBackStackEntryCount() > 0);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        // Add either a "photo" or "finish" button to the action bar, depending on which page
        // is currently selected.
        map = menu.add(Menu.NONE, R.id.action_map, Menu.NONE,
                mShowingBack
                        ? R.string.action_photo
                        : R.string.action_map);
        map.setIcon(mShowingBack
                ? R.drawable.ic_action_photo
                : android.R.drawable.ic_menu_mapmode);
        map.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

        ques = menu.add(Menu.NONE, R.id.action_ques, Menu.NONE,
                mShowingBack
                        ? R.string.action_photo
                        : R.string.action_ques);
        ques.setIcon(mShowingBack
                ? R.drawable.ic_action_photo
                : android.R.drawable.ic_menu_help);
        ques.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                // Navigate "up" the demo structure to the launchpad activity.
                // See http://developer.android.com/design/patterns/navigation.html for more.
                //NavUtils.navigateUpTo(this, new Intent(this, MainActivity.class));
                frag_status=0;
                return true;

            case R.id.action_map:
                if(mShowingBack)
                    frag_status=0;
                else
                    frag_status=1;
                flipCard(FLIP_FOR_MAP);               
                return true;

            case R.id.action_info:
                if(mShowingBack)
                    frag_status=0;
                else
                    frag_status=2;
                flipCard(FLIP_FOR_INFO);
                return true;                
        }

        return super.onOptionsItemSelected(item);
    }    

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        switch(frag_status)
        {
        case 1:
            map.setVisible(true);
            ques.setVisible(false);
            break;
        case 2:
            map.setVisible(false);
            ques.setVisible(true);
            break;
        }
        return super.onPrepareOptionsMenu(menu);
    }    

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
        map.setVisible(true);
        ques.setVisible(true);
    }

    private void flipCard(int flipType) {
        if (mShowingBack) {
            getFragmentManager().popBackStack();
            return;
        }

        mShowingBack = true;        
        Fragment mapFrag;
        InfoFragment infoFrag;
        Bundle args = new Bundle();

        args.putInt(RETRIEVE_POS, questions[quizPager.getCurrentItem()]);       

        switch(flipType) {
            case 1 :
                mapFrag = new Fragment();
                mapFrag.setArguments(args);
                getFragmentManager()                        
                .beginTransaction()              
                .setCustomAnimations(
                        R.animator.card_flip_right_in, R.animator.card_flip_right_out,
                        R.animator.card_flip_left_in, R.animator.card_flip_left_out)
                .replace(R.id.container, mapFrag)               
                .addToBackStack(null)
                .commit();
                break;
            case 2 :
                frag = new InfoFragment();
                frag.setArguments(args);
                getFragmentManager()
                .beginTransaction()              
                .setCustomAnimations(
                    R.animator.card_flip_right_in, R.animator.card_flip_right_out,
                    R.animator.card_flip_left_in, R.animator.card_flip_left_out)
                .replace(R.id.container, frag)               
                .addToBackStack(null)
                .commit();
            break;        
        }

        mHandler.post(new Runnable() {
            @Override
            public void run() {
                invalidateOptionsMenu();
            }
        });
    }

    @Override
    public void onBackStackChanged() {
        mShowingBack = (getFragmentManager().getBackStackEntryCount() > 0);

        // When the back stack changes, invalidate the options menu (action bar).
        invalidateOptionsMenu();
    }

    public static class FlagFragment extends Fragment {
        int position;
        Bundle args;

        public FlagFragment() {  
            position = args.getInt(RETRIEVE_POS);  
        }        

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.pic_fragment, container, false);

            ImageView flipImg = (ImageView)rootView.findViewById(R.id.flipFlagImage);            
            try {
                flipImg.setImageResource(dc.flags[position]);
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return rootView;
        }
    }

    public static class MapFragment extends Fragment {

        int position;
        Bundle args;

        public MapFragment() {
            position = args.getInt(RETRIEVE_POS);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.map_fragment, container, false);

            ImageView flipImg = (ImageView)rootView.findViewById(R.id.flipMapImage);            
            try {
                flipImg.setImageResource(dc.ortho[position]);
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return rootView;
        }
    }

    public static class InfoFragment extends Fragment {

        public InfoFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            return inflater.inflate(R.layout.info_fragment, container, false);
        }
    }

    public class QuizPagerAdapter extends FragmentStatePagerAdapter {    

        int fragDet[];
        Fragment frag;
        Bundle args;

            public QuizPagerAdapter(FragmentManager fragmentManager, int [] q) {
                super(fragmentManager);
                fragDet = q;
            }

            @Override
            public Fragment getItem(int i) {                

                frag = new FlagFragment();
                args.putInt(RETRIEVE_POS, fragDet[i]);
                frag.setArguments(args);
                return frag;
            }

            @Override
            public int getCount() {
                return numQues;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                String title = null;
                switch(position)
                {
                    case 0 : title = "Paired Devices";
                            break;
                    case 1 : title = "Chats";
                            break;
                    case 2 : title = "New Devices";
                            break;
                }
                return (CharSequence)title;
            }
    }
}
4

1 に答える 1

0

サポート ライブラリの Fragment クラスは、トランザクションでのアニメーションをサポートしていません。

API が 11 を超えるデバイス用のアプリのアニメーション化とビルドの間のトレードオフです。 http://developer.android.com/reference/android/app/FragmentTransaction.html

これに関する別の方法は、Jake Wharton の NineOldAndroids ライブラリを使用して、自分でトランザクションをコーディングすることです。

于 2013-08-22T04:30:14.797 に答える