Friday, 16 August 2013

use menu in one of the class of viewpager

use menu in one of the class of viewpager

I am new in android, so I have made an application where I am swiping
activities from one activity to another. But one of my activities has a
list which itself has some animations, which I select using menu. Now
after doing swiping through view pager, list is being shown but the menu
to this particular list is not showing, which contains the different
animations style on my list. I have used listview. I have used
onCreateOptionMenu() and onOptionsItemSelected() method. My code is given
below for this particular activity.
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;
}
}
}

No comments:

Post a Comment