【问题标题】:trigger Button onClick on ViewPager触发按钮 onClick ViewPager
【发布时间】:2015-09-26 10:07:35
【问题描述】:

我正在我的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 会干扰点击,但我似乎找不到解决方法。我到底做错了什么?

【问题讨论】:

    标签: android android-viewpager onclicklistener


    【解决方案1】:

    问题是mPasswordView和其他两个按钮目前在 标签在点击时没有响应。我一直在想 ViewPager 是 干扰点击,但我似乎找不到解决方法 这。我到底做错了什么?

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

    【讨论】:

    • 你能用一小段代码解释一下吗?你的意思是我应该找到像这样的按钮mPasswordView = (EditText) findViewById(R.id.login_password); 因为这样做时,按钮是空的。
    • 我不是那个意思。你能发布你的适配器吗?
    • 您在 Activity 中处理的所有逻辑都转到您的 LoginFragment。
    猜你喜欢
    • 2022-09-03
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-02
    相关资源
    最近更新 更多