在 ViewPager 上触发按钮 onClick

trigger Button onClick on ViewPager

我正在 onCreate() 上设置 ViewPager,但是当我尝试设置布局选项卡按钮之一的点击侦听器时,它没有响应。这是我的 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 会干扰点击,但我似乎找不到解决方法。我到底做错了什么?

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?

您正在使用 inflater,以获取对 ViewPager 中片段处理的视图的引用。您正在做错误的假设,即 inflater 返回给您的视图引用与 ViewPager 处理的 Fragments 正在膨胀的视图引用相同。 ActivityonCreate 中返回的充气器与您在屏幕上看到的不同。你应该让 Fragment 处理他们的 OnClicKListener