触发按钮onClick在ViewPager上

时间:2015-09-26 10:07:35

标签: android android-viewpager onclicklistener

我在我的onCreate()上设置了ViewPager但是当我尝试设置Click listener时,其中一个布局标签按钮,它没有响应。以下是我onCreate()的设置方式。

    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);
}

我的JoinLoginAdapter延长了FragmentStatePagerAdapter。我有三个布局,一个是ContentView,另外两个是我ViewPager的两个标签。问题是mPasswordView,当前两个标签中的其他按钮在点击时不响应。我的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;
}
}

我一直认为ViewPager干扰了点击,但我似乎无法找到解决方法。我究竟做错了什么?

1 个答案:

答案 0 :(得分:1)

  

问题是mPasswordView和其他按钮目前在两者中   标签在点击时不响应。我一直在想ViewPager是   干扰点击但我似乎无法找到如何解决   这个。我究竟做错了什么?

您正在使用inflater,以获取对您ViewPager中的片段处理的视图的引用的引用。您正在做错误的假设,即inflater返回的是ViewPager处理的碎片正在膨胀的相同视图的参考。在Activity onCreate中返回inflater的内容与您在屏幕上看到的内容有所不同。您应该让Fragment处理他们的OnClicKListener