Reputation: 1575
I am setting up ViewPager on my onCreate()
but when I try to set click listener one of the layout tab button, it does not respond. Here is how my onCreate()
is set up.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.join_login);
// Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
adapter = new JoinLoginAdapter(getSupportFragmentManager(),Titles,Numboftabs);
// Assigning ViewPager View and setting the adapter
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
// Assiging the Sliding Tab Layout View
tabs = (SlidingTabLayout) findViewById(R.id.tabs);
tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width
// Setting the ViewPager For the SlidingTabsLayout
tabs.setViewPager(pager);
// Set up the login form.
View inflatedView = getLayoutInflater().inflate(R.layout.login_tab, null);
mEmailView = (AutoCompleteTextView) inflatedView.findViewById(R.id.email);
populateAutoComplete();
mPasswordView = (EditText) inflatedView.findViewById(R.id.login_password);
//mPasswordView = (EditText) findViewById(R.id.login_password);
mPasswordView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
if (id == R.id.login_password || id == EditorInfo.IME_NULL) {
attemptLogin();
return true;
}
return false;
}
});
Button mEmailSignInButton = (Button) inflatedView.findViewById(R.id.email_sign_in_button);
mEmailSignInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
attemptLogin();
}
});
mLoginFormView = findViewById(R.id.login_form);
mProgressView = findViewById(R.id.login_progress);
}
My JoinLoginAdapter
extends FragmentStatePagerAdapter
. I have three layouts, one is the ContentView whereas the other two are the two tabs for my ViewPager
. The problem is mPasswordView
and other buttons currently in the two tabs do not respond on click. I also have the adapter below for my ViewPager.
public class JoinLoginAdapter extends FragmentStatePagerAdapter {
CharSequence Titles[]; // This will Store the Titles of the Tabs which are Going to be passed when ViewPagerAdapter is created
int NumbOfTabs; // Store the number of tabs, this will also be passed when the ViewPagerAdapter is created
// Build a Constructor and assign the passed Values to appropriate values in the class
public JoinLoginAdapter(FragmentManager fm,CharSequence mTitles[], int mNumbOfTabsumb) {
super(fm);
this.Titles = mTitles;
this.NumbOfTabs = mNumbOfTabsumb;
}
//This method return the fragment for the every position in the View Pager
@Override
public Fragment getItem(int position) {
if(position == 0) // if the position is 0 we are returning the First tab
{
LoginFragment logintab = new LoginFragment();
return logintab;
}
else // As we are having 2 tabs if the position is now 0 it must be 1 so we are returning second tab
{
JoinFragment jointab = new JoinFragment();
return jointab;
}
}
// This method return the titles for the Tabs in the Tab Strip
@Override
public CharSequence getPageTitle(int position) {
return Titles[position];
}
// This method return the Number of tabs for the tabs Strip
@Override
public int getCount() {
return NumbOfTabs;
}
}
I have been thinking the ViewPager is interfering with the clicks but I cannot seem to find how to solve this. What exactly am I doing wrong?
Upvotes: 0
Views: 1004
Reputation: 157447
The problem is mPasswordView and other buttons currently in the two tabs do not respond on click. I have been thinking the ViewPager is interfering with the clicks but I cannot seem to find how to solve this. What exactly am I doing wrong?
you are using a inflater
, to get a reference a reference to view handled by the fragments in your ViewPager
. You are doing the wrong assumption that the inflater
is returning you the same view's reference that the Fragments handled by the ViewPager are inflating. What is returning the inflater in your Activity
's onCreate
is different from what you are seeing at screen. You should let the Fragment
s handle their OnClicKListener
Upvotes: 1