我是 android 新手,所以我制作了一个应用程序,我在其中将活动从一个活动滑到另一个活动。但是我的一个活动有一个列表,它本身有一些动画,我使用菜单选择。现在在通过视图寻呼机滑动之后,正在显示列表,但未显示此特定列表的菜单,其中包含我列表中不同的动画样式。我用过列表视图。我使用过 onCreateOptionMenu() 和 onOptionsItemSelected() 方法。我的代码在下面给出了这个特定的活动。
public class TabFragmentClass extends Fragment {
DisplayMetrics metrics;
ListView list_view=null;
Animation animation=null;
int mode=R.id.TranslateAnimation1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_main,null);
metrics=new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
list_view=(ListView) view.findViewById(R.id.list_view);
list_view.setFadingEdgeLength(0);
ArrayList<String> ar_li=new ArrayList<String>();
for(int i=0;i<100;i++)
{
ar_li.add("item: "+(i+1));
}
MainAdapter mAdapter=new MainAdapter(getActivity(),ar_li,metrics);
list_view.setAdapter(mAdapter);
return view;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
//super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.activity_main, menu);
menu.add(Menu.NONE,1,0,"TranslateAnimation1");
menu.add(Menu.NONE, 2, 0, "TranslateAnimation2");
menu.add(Menu.NONE, 3, 0, "ScaleAnimation");
menu.add(Menu.NONE, 4, 0, "fade_in");
menu.add(Menu.NONE, 5, 0, "hyper_space_out");
menu.add(Menu.NONE, 6, 0, "wave_scale");
menu.add(Menu.NONE, 7, 0, "push_left_in");
menu.add(Menu.NONE, 8, 0, "push_left_out");
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
mode=item.getItemId();
return super.onOptionsItemSelected(item);
}
class MainAdapter extends ArrayAdapter<String>
{
Context context;
LayoutInflater minflator;
ArrayList<String> ar_string;
DisplayMetrics metrics;
TextView txt_view;
public MainAdapter(Context context, ArrayList<String> ar_string,
DisplayMetrics metrics) {
super(context, 0, ar_string);
this.context=context;
this.minflator=(LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.ar_string=ar_string;
this.metrics=metrics;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final String str=this.ar_string.get(position);
if(convertView==null)
{
convertView=minflator.inflate(android.R.layout.simple_list_item_1,null);
convertView.setBackgroundColor(0xFF202020);
txt_view=(TextView) convertView.findViewById(android.R.id.text1);
txt_view.setTextColor(0xFFFFFFFF);
convertView.setTag(txt_view);
}
else
{
convertView.getTag();
}
txt_view.setText(str);
switch (mode) {
case R.id.TranslateAnimation1:
animation=new TranslateAnimation(metrics.widthPixels/2,0,0,0);
System.out.println("animated value:::::"+animation);
break;
case R.id.TranslateAnimation2:
animation=new TranslateAnimation(0,0,metrics.heightPixels, 0);
break;
case R.id.ScaleAnimation:
animation=new ScaleAnimation((float)1.0,(float)1.0, (float)0, (float)1.0);
break;
case R.id.fade_in:
animation=AnimationUtils.loadAnimation(context, R.anim.anim_fade_in);
break;
case R.id.hyper_space_out:
animation=AnimationUtils.loadAnimation(context, R.anim.hyper_space_out);
break;
case R.id.wave_scale:
animation=AnimationUtils.loadAnimation(context, R.anim.wave_scale);
break;
case R.id.push_left_in:
animation=AnimationUtils.loadAnimation(context, R.anim.push_left_in);
break;
case R.id.push_left_out:
animation=AnimationUtils.loadAnimation(context, R.anim.push_left_out);
break;
default:
break;
}
animation.setDuration(500);
convertView.startAnimation(animation);
animation=null;
return convertView;
}
}
}