我需要开发一个 ListView,其中项目是一个片段(FRONT),单击它会翻转到另一个片段(BACK)。
我制作了一个自定义适配器,我在其中使用片段填充列表的每个项目(在 getView 中)。单击后,我将用后视图替换前视图。如果只有一个项目开始随机运行,这是可以理解的,当 onClick 总是翻转列表的最顶部片段时,当有多个项目时,这可以正常工作。
有没有一种方法可以让我聆听、识别和对列表中的各个项目进行翻转动画?
以下是我编写的自定义适配器的代码
public class CustomCardAdapter extends ArrayAdapter<String> implements OnClickListener, OnBackStackChangedListener{
private int textViewResourceId;
private Context mContext;
private List<String> list;
private FragmentManager fm;
private Handler mHandler;
private boolean mShowingBack = false;
public CustomCardAdapter(FragmentManager fm, Context context, int textViewResourceId, List<String> list) {
super(context, textViewResourceId, list);
mContext = context;
this.textViewResourceId = textViewResourceId;
this.list = list;
this.fm = fm;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = convertView;
if(rowView == null)
rowView = mInflater.inflate(textViewResourceId, parent, false);
rowView.setOnClickListener(this);
rowView.setTag(String.valueOf(position));
rowView.setId(position);
fm.beginTransaction()
.add(R.id.container, new CardFrontFragment(list.get(position)))
.commit();
fm.addOnBackStackChangedListener(this);
return rowView;
}
@Override
public void onClick(View v) {
FlipCard(v);
}
@Override
public void onBackStackChanged() {
mShowingBack = (fm.getBackStackEntryCount() > 0);
}
private void FlipCard(View view){
if(mShowingBack){
fm.popBackStack(view.getTag().toString(),0);
return;
}
mShowingBack = true;
fm.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,new CardBackFragment("Advertise Here","describe here"))
.addToBackStack(null)
.commit();
}
}
请帮助我并告诉我需要更改什么或此代码有什么问题?