I\'m learning how to implement horizontal swiping and I\'m getting the following error while trying to launch my app having ViewPager
in its layout.
<
Hi i have just made some change in your layout file & its working fine now..
you can check this:
below code is just for your refrance---->
your main activity:
package com.example.viewpage;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
public class MainActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager pager = (ViewPager) findViewById(R.id.pager);
/** Instantiating FragmentPagerAdapter */
MyAdapter pagerAdapter = new MyAdapter(getSupportFragmentManager());
/** Setting the pagerAdapter to the pager object */
pager.setAdapter(pagerAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
MyAdapter:
package com.example.viewpage;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
public class MyAdapter extends android.support.v4.app.FragmentPagerAdapter {
final int PAGE_COUNT = 3;
public MyAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
MyFragment myFragment = new MyFragment();
Bundle data = new Bundle();
data.putInt("current_page", arg0 + 1);
myFragment.setArguments(data);
return myFragment;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return PAGE_COUNT;
}
}
MyFragment:
package com.example.viewpage;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MyFragment extends Fragment {
private int mCurrentPage;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/** Getting the arguments to the Bundle object */
Bundle data = getArguments();
/** Getting integer data of the key current_page from the bundle */
mCurrentPage = data.getInt("current_page", 0);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.frag_layout, container, false);
TextView tv = (TextView) v.findViewById(R.id.textView1);
tv.setText("You are viewing the page #" + mCurrentPage + "\n\n"
+ "Swipe Horizontally left / right");
return v;
}
}
frag_layout.xml
NOTE: add support android-support-v4.jar
in lib folder. add this jar in Build Path
. now go to project property-->Build Path--> Order & Export--> select all --> ok
. Clean & RUN.