我正在研究一个可以翻转其元素的ViewPager,就像android动画演示的CardFlipActivity一样。 我遇到了beginTransaction()方法的问题。
我得到的唯一快速修复选项让我将片段的类型从android.support.v4.app.Fragment
更改为android.app.Fragment
当我这样做时,会出现另一个错误,我必须将类型更改回support.v4.app.Fragment.
我该怎么办? 我在我的代码中使用所有support.v4库导入片段和viewPagers。
代码:
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;
}
}
}
答案 0 :(得分:0)
支持库的Fragment类不支持事务中的动画。
在为API高于11的设备制作动画或构建应用程序之间进行权衡。 http://developer.android.com/reference/android/app/FragmentTransaction.html
另一种方法是使用Jake Wharton的NineOldAndroids库并自行编写交易代码。