I am using the following code, MAX is 2 pages. By default the position is 0 and adds a new page to the right. I inflate two layout files.
How can I show the page1 when the app starts and add a new page to the left ?
Thanks.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v4.view.ViewPager
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="#+id/pagerView" />
</LinearLayout>
Java code
public class MyPagerActivity extends Activity {
private Context context;
private int pageNumber;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = this;
ViewPager pagerView = (ViewPager)findViewById(R.id.pagerView);
pagerView.setAdapter(new AwesomePagerAdapter());
}
private class AwesomePagerAdapter extends PagerAdapter{
#Override
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((View)view);
}
#Override
public void finishUpdate(View arg0) {
//setPageTitles(getPageNumber());
}
#Override
public int getCount() {
return 2;
}
#Override
public Object instantiateItem(View collection, int position) {
/* TextView tv = new TextView(MyPagerActivity.this);
tv.setText("Bonjour PAUG " + position);
tv.setTextColor(Color.WHITE);
tv.setTextSize(30);
((ViewPager) collection).addView(tv,0);
return tv;*/
View view = getViewToShow(position);
((ViewPager) collection).addView(view,0);
return view;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view==((View)object);
}
#Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
// TODO Auto-generated method stub
}
#Override
public Parcelable saveState() {
// TODO Auto-generated method stub
return null;
}
#Override
public void startUpdate(View arg0) {
// TODO Auto-generated method stub
}
}
private View getViewToShow(int position){
View view = null;
View layout;
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
switch(position){
case 0:
layout = mInflater.inflate(R.layout.elements, null);
view = layout;
break;
case 1: view =
layout = mInflater.inflate(R.layout.elements2, null);
view = layout;
break;
}
return view;
}
}
Have you tried using the setCurrentItem method?
The accepted solution is fine, but it's important to call this method after setting up the adapter.
viewPager.setAdapter(adapterViewPager);
viewPager.setCurrentItem(1);
By default, you have to set the current value 1 to show the just one default view. If you do not set setCurrentItem to 1, you can not see anything.
viewPager.setAdapter(adapterViewPager);
viewPager.setCurrentItem(1)
This solution gives the ability of setting default page without interference from outside of the Pager adapter class.
#Override
public Object instantiateItem(ViewGroup container, int position) {
View currentPage = null;
switch(position){
case 0:
currentPage = LayoutInflater.from(context).inflate(R.layout.page0, null)
break;
case 1:
currentPage = LayoutInflater.from(context).inflate(R.layout.page1, null)
///////////// This page will be default ////////////////////
((ViewPager)container).setCurrentItem(position);
////////////////////////////////////////////////////////////
break;
case 2:
currentPage = LayoutInflater.from(context).inflate(R.layout.page2, null)
break;
return currentPage;
}
You need to set page only during first initialisation of the fragment. Later it will restore a selected by user position from a "saved instance state". So this looks a little better:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
pager.adapter = AwesomePagerAdapter()
if (savedInstanceState == null) {
pager.currentItem = 1
}
}
Related
Good morning,
I have a Fragment which its layout has a ViewPager, I provide a custom PageAdapter to show two child layouts. One layout is to fill with personal data and another layout is to fill with Address data by the user. On my fragment I want to retrieve both layouts data and fill a object Pessoa(Person) with an attribute Endereco (Address). So far my ViewPager is working and both layout are displayed, however when I try to get the fields on both layouts, either on onCreateView or onResume fragment's method, using :
viewpager.getChildAt(0).findViewById(R.id.txt1)
I got an NullPointerException, but if I add an OnPageChangeListener this works, I not sure if it's the best approach. Is There a best place to retrieve a reference to the viewpager child's layout?
Ps. I don't want to use two other Fragments in order to display each of the layouts.
PageAdapter
public class ViewPagerCadastroPessoaAdapter extends PagerAdapter {
private Context context;
private int[] views = {R.layout.layout_cadastro_dados_pessoais,R.layout.layout_cadastro_endereco };
public ViewPagerCadastroPessoaAdapter(Context context) {
this.context = context;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater inflater = LayoutInflater.from(this.context);
View layout = inflater.inflate(views[position], container, false);
container.addView(layout, position);
return layout;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0:
return "Dados Pessoais";
case 1:
return "Endereço";
}
return super.getPageTitle(position);
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}}
ViewPager's holder Fragment
public class CadastroPessoaFragment extends Fragment {
private ViewPager viewPagerCadastroPessoa;
private TabLayout tabLayoutCadastroPessoa;
public CadastroPessoaFragment() {
}
public static CadastroPessoaFragment newInstance() {
return new CadastroPessoaFragment();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_cadastro_pessoa, container, false);
tabLayoutCadastroPessoa = root.findViewById(R.id.tabLayoutCadastroPessoa);
viewPagerCadastroPessoa = root.findViewById(R.id.viewPagerCadastroPessoa);
viewPagerCadastroPessoa.setOffscreenPageLimit(2);
viewPagerCadastroPessoa.setAdapter(new ViewPagerCadastroPessoaAdapter(getContext()));
tabLayoutCadastroPessoa.setupWithViewPager(viewPagerCadastroPessoa);
int cor = ContextCompat.getColor(getContext(), android.R.color.white);
tabLayoutCadastroPessoa.setTabTextColors(cor, cor);
//get a null pointer
viewPagerCadastroPessoa.getChildAt(0).findViewById(R.id.txt1);
viewPagerCadastroPessoa.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
//works fine
viewPagerCadastroPessoa.getChildAt(0).findViewById(R.id.txt1);
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
return root;
}
#Override
public void onResume() {
super.onResume();
//get a null pointer
viewPagerCadastroPessoa.getChildAt(0).findViewById(R.id.txt1);
}
}
Thank you all in advance
UPDATE
According with
Anton Malyshev answer
The way Viewpager creates its pages is kind of "unsynchronized", so I used the following approach to solve my problem:
On my custom adapter I definied an interface with a single method onViewPagerReady(), as my Viewpager will always have only two pages, inside instantiateItem I check when position is 1 (once position starts with 0), if position == 1 I call the method onViewPagerReady that my fragment viewpager's holder implements, so inside this method I retrieve the layout of viewpager's children
public class ViewPagerCadastroPessoaAdapter extends PagerAdapter {
public interface ViewPagerObserver{
void onViewPagerReady();
}
private Context context;
private int[] views = {R.layout.layout_cadastro_dados_pessoais,R.layout.layout_cadastro_endereco };
private ViewPagerObserver observer;
public ViewPagerCadastroPessoaAdapter(Context context, ViewPagerObserver observer) {
this.context = context;
this.observer = observer;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater inflater = LayoutInflater.from(this.context);
View layout = inflater.inflate(views[position], container, false);
container.addView(layout, position);
if(position == 1){
observer.onViewPagerReady();
}
return layout;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0:
return "Dados Pessoais";
case 1:
return "Endereço";
}
return super.getPageTitle(position);
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
Fragment Viewpager's Holder
#Override
public void onViewPagerReady() {
View rootCadastroDadosPessoais = viewPagerCadastroPessoa.getChildAt(0);
View rootCadastroEnderecos =viewPagerCadastroPessoa.getChildAt(1);
}
Thank you all.
Just wait until viewPagerCadastroPessoa.getChildCount() will be greater than 0. The pages in ViewPager are not created yet in onResume (they are created asynchronously).
With the help of answer of the this question in StackOverflow(Remove Fragment Page from ViewPager in Android), I successfully created my project and did some modifications according to my needs.
I am able to add N number of fragments(viewpages) in my main layout and delete them until page count becomes zero.
Now, I struck with an issue.i.e. ones, if I move from my current activity which is holding this pages to other activity or if I close my app and come back,I am only able to see initial statically added fragment.
I would like to see not only initial page but also previously added pages. Should i use SharedPreferences to store dynamically added fragments?
Here is my source code:
My main layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="900dp"
/>
<Button
android:id="#+id/show_items"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_marginTop="200dp"
android:textColor="#ffffff"
android:background="#49a942"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="+" />
<Button
android:id="#+id/grid_apps_show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/pager"
android:layout_alignParentBottom="true"
android:text="Apps"
/>
</RelativeLayout>
My MainActivity class:
public class MainActivity extends FragmentActivity implements TextProvider{
Button home;
private ViewPager mPager;
private MyPagerAdapter mAdapter;
private ArrayList<String> mEntries = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
home = (Button) findViewById(R.id.show_items);
mEntries.add("pos 1");
mPager = (ViewPager) findViewById(R.id.pager);
home.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
MainActivity.this);
alertDialogBuilder.setTitle("Add/Delete Screens");
alertDialogBuilder
.setCancelable(false)
.setNegativeButton("+",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
addNewItem();
}
})
.setPositiveButton("-",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
removeCurrentItem();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
mAdapter = new MyPagerAdapter(this.getSupportFragmentManager(), this);
mPager.setAdapter(mAdapter);
}
private void addNewItem() {
mEntries.add("Pages");
mAdapter.notifyDataSetChanged();
}
private void removeCurrentItem() {
int position = mPager.getCurrentItem();
if(position != 0){
mEntries.remove(position);
mAdapter.notifyDataSetChanged();
}
else{
Toast.makeText(getApplicationContext(), "Minimum Screens are one!", Toast.LENGTH_LONG).show();
}
}
#Override
public String getTextForPosition(int position) {
return mEntries.get(position);
}
#Override
public int getCount() {
return mEntries.size();
}
private class MyPagerAdapter extends FragmentPagerAdapter {
private TextProvider mProvider;
private long baseId = 0;
public MyPagerAdapter(FragmentManager fm, TextProvider provider) {
super(fm);
this.mProvider = provider;
}
#Override
public Fragment getItem(int position) {
if(position == 0){
return ScreenOne.newInstance(mProvider.getTextForPosition(position));
}
if(position == 1){
return ScreenTwo.newInstance(mProvider.getTextForPosition(position));
}
return ScreenTwo.newInstance(mProvider.getTextForPosition(position));
}
#Override
public int getCount() {
return mProvider.getCount();
}
//this is called when notifyDataSetChanged() is called
#Override
public int getItemPosition(Object object) {
// refresh all fragments when data set changed
return PagerAdapter.POSITION_NONE;
}
#Override
public long getItemId(int position) {
// give an ID different from position when position has been changed
return baseId + position;
}
/**
* Notify that the position of a fragment has been changed.
* Create a new ID for each position to force recreation of the fragment
* #param n number of items which have been changed
*/
public void notifyChangeInPosition(int n) {
// shift the ID returned by getItemId outside the range of all previous fragments
baseId += getCount() + n;
}
}
}
my fragment one:
public class ScreenOne extends Fragment {
private String mText;
public static ScreenOne newInstance(String text) {
ScreenOne f = new ScreenOne(text);
return f;
}
public ScreenOne() {
}
public ScreenOne(String text) {
this.mText = text;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.screen_one, container, false);
((TextView) root.findViewById(R.id.position_one)).setText(mText);
return root;
}
}
my fragment two:
public class ScreenTwo extends Fragment {
private String mText;
public static ScreenTwo newInstance(String text) {
ScreenTwo f = new ScreenTwo(text);
return f;
}
public ScreenTwo() {
}
public ScreenTwo(String text) {
this.mText = text;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.screen_two, container, false);
((TextView) root.findViewById(R.id.position_two)).setText(mText);
return root;
}
}
Thanks & Regards,
Aditya. J
Just override this method in FragmentpagerAdapter:
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(ViewGroup container, int position, Object object);
}
And remove:
super.destroyItem(ViewGroup container, int position, Object object);
I'm working with viewpager inside fragments. I'm using pagerAdapter to switch between two pages via XML. Here is the code:
public class DemoPagerAdapter extends FragmentPagerAdapter {
public DemoPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
Fragment f;
switch (index) {
case 0:
f = new FirstFragment();
break;
case 1:
f = new SecondFragment();
break;
}
return f;
}
#Override
public int getCount() {
return 2;
}
}
And I'm calling it like this:
public class DemoFragment1 extends Fragment {
private ViewPager viewPager;
private DemoPagerAdapter mAdapter;
public DemoFragment1() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.security, container, false);
mAdapter = new DemoPagerAdapter(getActivity().getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
showPages(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
return rootView;
}
Everything works fine. But if I move to DemoFragment2 and then come back to DemoFragment1, PagerAdapter is not called.
Your ViewPager is nested in a Fragment, you should use the ChildFragmentManager, modify:
mAdapter = new DemoPagerAdapter(getChildFragmentManager());
I also assume the ViewPager will keep the off screen fragment in memory, thus it might not call getItem(int index) again. Add
viewPager.setOffscreenPageLimit(0);
return rootView;
Well, I encontered this issue last week.
When I put the ViewPager in the fragment with a FragmentPagerAdapter, after I move forward to anoter Fragment and back, the pager' s view not exists.
I supposed is the FragmentPagerAdapter won' t retain its view in the ViewPager ' s host Fragment, Their life cycle is not the same. Though you put the VewPager' s host Fragment in the back stack, but the FragmentPagerAdapter' s fragment not. And Then, I gave up FragmentPagerAdapter and tried android.support.v4.view.PagerAdapter, (note, it wont' t use fragment to inject view!), everything is fine!
below is the sample:
private final PagerAdapter coverPager = new PagerAdapter() {
private LayoutInflater mLayoutInflater;
#Override
public int getCount() {
return 2;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
if (mLayoutInflater == null) {
mLayoutInflater = LayoutInflater.from(getActivity());
}
switch (position) {
case 0:
View frontPage = mLayoutInflater.inflate(R.layout.profile_cover, container, false);
ImageView avatar = (ImageView) frontPage.findViewById(R.id.avatar);
Picasso.with(getActivity())
.load(mAvatarUrl)
.placeholder(R.drawable.error)
.error(R.drawable.error)
.into(avatar);
TextView screenName = (TextView) frontPage.findViewById(R.id.screen_name);
screenName.setText("#" + mScreenName);
TextView remark = (TextView) frontPage.findViewById(R.id.remark);
remark.setText(TextUtils.isEmpty(mRemark) ? mScreenName : mRemark);
if (mVerified) {
frontPage.findViewById(R.id.verified).setVisibility(View.VISIBLE);
}
container.addView(frontPage);
return frontPage;
case 1:
View introPage = mLayoutInflater.inflate(R.layout.profile_intro, container, false);
CatnutUtils.setText(introPage, R.id.description, TextUtils.isEmpty(mDescription)
? getString(R.string.no_description) : mDescription);
CatnutUtils.setText(introPage, R.id.location, mLocation);
CatnutUtils.setText(introPage, R.id.profile_url, Constants.WEIBO_DOMAIN + mProfileUrl);
container.addView(introPage);
return introPage;
default:
return null;
}
}
};
and then in your onViewCreate or OnCreateView, put mViewPager.setAdapter(coverPager);
the instantiateItem is the place to put the page' s view, similar like the FragmentPagerAdapter!
It' s just like put the FragmentAdapter' s view int the ViewPager' s host Fragment, not the other fragment!
Hope it' s helpful!
One of my layouts inside of my ViewPager has a button. "R.layout.add_site". I would like the option of hitting the button and it scroll to the specific page for me. I already have the option to swipe to the specific page, but I would like to have both.
Now I'm sure there's a way to do that, but for some reason, I cannot figure it out.
You'll see that I've made an attempt , but just dont know what method to call in order to make it scroll to the desired page. Which is R.layout.main.
Here's my code.
public class fieldsActivity extends Activity {
Button addSiteButton;
Button cancelButton;
Button signInButton;
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// to create a custom title bar for activity window
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.fields);
// use custom layout title bar
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.topbar);
Pager adapter = new Pager();
ViewPager mPager = (ViewPager) findViewById(R.id.fieldspager);
mPager.setAdapter(adapter);
mPager.setCurrentItem(1);
addSiteButton = (Button) findViewById(R.id.addSiteButton);
addSiteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Don't know what method to call!?
}
});
cancelButton = (Button) findViewById(R.id.cancel_button);
signInButton = (Button) findViewById(R.id.sign_in_button);
}
private class Pager extends PagerAdapter {
public int getCount() {
return 3;
}
#Override
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0:
resId = R.layout.field01;
break;
case 1:
resId = R.layout.add_site;
break;
case 2:
resId = R.layout.main;
break;
}
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
}
#Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
#Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
#Override
public Parcelable saveState() {
return null;
}
}
}
Any help is appreciated!
call :
mPager.setCurrentItem(2);
in your onClick() method, or for a smoother scroll:
mPager.setCurrentItem(2, true);
I can't really answer WHY exactly this happens, but if you delay the setCurrentItem call for a few milliseconds it should work. My guess is that because during onResume there hasn't been a rendering pass yet, and the ViewPager needs one or something like that.
private ViewPager viewPager;
#Override
public void onResume() {
final int pos = 3;
viewPager.postDelayed(new Runnable() {
#Override
public void run() {
viewPager.setCurrentItem(pos);
}
}, 100);
}
Original Answer: here
I'm new to this android and java.
My viewpager now can slide all my xml layout, but i don't know how to access item in them.
I tried using Button mybutton = (Button) findViewById (R.layout.result), then set the onClickListener in mybutton, but then it was forced to close every time I run it.
Here is my code:
public class main extends Activity {
private ViewPager myviewpager;
private ViewPagerAdapter myviewpageradapter;
private static int NUM_AWESOME_VIEWS = 12;
Button button_1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myviewpageradapter = new ViewPagerAdapter();
myviewpager = (ViewPager) findViewById(R.id.myviewpager);
myviewpager.setAdapter(myviewpageradapter);
button_1 = (Button) myviewpager.findViewById(R.id.first_button);
button_1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
private class ViewPagerAdapter extends PagerAdapter {
#Override
public int getCount() {
return NUM_AWESOME_VIEWS;
}
#Override
public Object instantiateItem(View collection, int position) {
LayoutInflater layoutinflater = getLayoutInflater();
View view;
view = layoutinflater.inflate(R.layout.page1, null);
switch (position)
{
case 0 : view = layoutinflater.inflate(R.layout.page1, null); break;
case 1 : view = layoutinflater.inflate(R.layout.page2, null); break;
case 2 : view = layoutinflater.inflate(R.layout.page3, null); break;
case 3 : view = layoutinflater.inflate(R.layout.page4, null); break;
case 4 : view = layoutinflater.inflate(R.layout.page5, null); break;
case 5 : view = layoutinflater.inflate(R.layout.page6, null); break;
case 6 : view = layoutinflater.inflate(R.layout.page7, null); break;
case 7 : view = layoutinflater.inflate(R.layout.page8, null); break;
case 8 : view = layoutinflater.inflate(R.layout.page9, null); break;
case 9 : view = layoutinflater.inflate(R.layout.page10, null); break;
case 10 : view = layoutinflater.inflate(R.layout.page11, null); break;
case 11 : view = layoutinflater.inflate(R.layout.result, null);break;
}
((ViewPager) collection).addView(view,0);
return view;
}
#Override
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((View) view);
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view==((View)object);
}
#Override
public void finishUpdate(View arg0) {}
#Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {}
#Override
public Parcelable saveState() {
return null;
}
#Override
public void startUpdate(View arg0) {}
}
It may just be the Problem, that your items are not found probably so try this(If your view is no LinearLayout change it)
LayoutInflater getView = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout view1 =(LineaLayout)getView.inflate(R.layout.your_view,null);
But Your Button may throw Exception furthermore there is an onClickListener set to it.
If it's just the Button define it in XML:
android:onClick"buttonClick"
then in Code
public void buttonClick(View v){
// do something
}
If this is not possible because you need to reference the Button later, I stuck with the same Problem and would like to know how it works.
I think the problem here is that you are trying to find a view (findViewById) that wasn't instatiated (inflated) yet. Try to set the button listener inside the instatiateItem method of ViewPagerAdapter type.
Example:
switch(position){
case 0 :
view = layoutinflater.inflate(R.layout.page1, null);
button_1 = (Button) view.findViewById(R.id.first_button);
button_1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
break;
(...)
}