【问题标题】:OnClick for button not updating the fragmentOnClick 按钮不更新片段
【发布时间】:2014-05-28 18:47:17
【问题描述】:

我创建了以下主要类,它向用户显示欢迎片段。这个欢迎片段有 2 个按钮:注册和登录。当用户单击注册按钮时,我希望他们路由到注册片段,当他们单击登录按钮时,用户需要转到登录片段。我面临的问题是单击按钮,没有任何反应。 我的主要活动如下所示:

import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.TypedArray;

import android.os.Bundle;
import android.support.v4.app.*;
import com.parse.Parse;
import com.parse.ParseAnalytics;
import android.util.Log;
import android.view.*;
import android.widget.*;

import java.util.ArrayList;

import android.support.v4.widget.DrawerLayout;
import com.parse.ParseObject;
import com.project.iandwe.Adaptor.NavigationDrawerListAdapter;
import com.project.iandwe.Fragments.UserLogIn;
import com.project.iandwe.Fragments.UserSignUp;
import com.project.iandwe.Fragments.WelcomePage;
import com.project.iandwe.Menu.*;
import com.project.iandwe.Navigation.NavigationDrawer;

public class MainActivity extends FragmentActivity implements Communicator {
@Override
    protected void onCreate(Bundle savedInstanceState) {
WelcomePage welcomePage = new WelcomePage();
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fragmentWelcome,welcomePage);
        fragmentTransaction.show(welcomePage);
        fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentLogin));
        fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentISignUp));
        fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentRegister));
        fragmentTransaction.commit();
}

@Override
    public void respond(int button) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        if (button == 1) {
            UserSignUp userSignUp = (UserSignUp) fragmentManager.findFragmentById(R.id.fragmentISignUp);
            fragmentTransaction.replace(R.id.fragmentISignUp, userSignUp);
            fragmentTransaction.show(userSignUp);
            fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentLogin));
            fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentWelcome));
            fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentRegister));
        } else if (button == 2) {
            UserLogIn userLogIn = (UserLogIn) fragmentManager.findFragmentById(R.id.fragmentLogin);
            fragmentTransaction.replace(R.id.fragmentLogin, userLogIn);
            fragmentTransaction.show(userLogIn);
            fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentISignUp));
            fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentWelcome));
            fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentRegister));
        }
        fragmentTransaction.commit();

    }

我的片段如下所示:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import com.project.iandwe.Communicator;
import com.project.iandwe.R;

/**
 * Created by NathanDrake on 5/28/2014.
 */
public class WelcomePage extends Fragment implements View.OnClickListener {
    Button login,signUp;
    int buttonFlag =0;
    Communicator communicator;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
           View layoutInflater;
           layoutInflater=inflater.inflate(R.layout.welcome_page,container, false );
      /*  login = (Button) getActivity().findViewById(R.id.buttonLogin);
        signUp = (Button) getActivity().findViewById(R.id.buttonSignUp);
        communicator = (Communicator) getActivity();

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buttonFlag =1;
                communicator.respond(buttonFlag);
            }
        });

        signUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buttonFlag =2;
                communicator.respond(buttonFlag);
            }
        });
        communicator.respond(buttonFlag); */
           return layoutInflater;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        login = (Button) getActivity().findViewById(R.id.buttonLogin);
        signUp = (Button) getActivity().findViewById(R.id.buttonSignUp);
        communicator = (Communicator) getActivity();
        login.setOnClickListener(this);
        signUp.setOnClickListener(this);
        /*

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             buttonFlag =1;
                communicator.respond(buttonFlag);
            }
        });

        signUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buttonFlag =2;
                communicator.respond(buttonFlag);
            }
        });

        communicator.respond(buttonFlag); */
     }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.buttonLogin:
                buttonFlag =1;
                break;
            case R.id.buttonSignUp:
                buttonFlag =2;
            break;

        }
        communicator.respond(buttonFlag);
    }
}

Communicator 是一个用于片段间通信的普通接口,如下所示:

public interface Communicator {

    public void respond(int button);
}

我尝试了这里给出的方法,但仍然没有来自片段的响应:How to handle button clicks using the xml onClick within Fragments

请看一下,让我知道我在这里缺少什么。

更新:

我更改了我的代码以包含这样的片段引用:

   welcomePage = new WelcomePage();
            FragmentManager fragmentManager = getSupportFragmentManager();
            userLogIn = (UserLogIn) fragmentManager.findFragmentById(R.id.fragmentLogin);
            userSignUp = (UserSignUp) fragmentManager.findFragmentById(R.id.fragmentISignUp);
            registerUsingEmail = (RegisterUsingEmail) fragmentManager.findFragmentById(R.id.fragmentRegister);

fragmentTransaction.hide(userLogIn);
        //fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentISignUp));
        fragmentTransaction.hide(userSignUp);
        //fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentRegister));
        fragmentTransaction.hide(registerUsingEmail);
        fragmentTransaction.commit();

并更新了我的通讯器响应功能:

@Override
    public void respond(int button) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.hide(welcomePage);
        fragmentTransaction.remove(welcomePage);
        fragmentTransaction.commit();
        fragmentManager.executePendingTransactions();
        fragmentTransaction = fragmentManager.beginTransaction();
        if (button == 1) {
           // userSignUp = (UserSignUp) fragmentManager.findFragmentById(R.id.fragmentISignUp);

            fragmentTransaction.remove(userSignUp);
            fragmentTransaction.commit();
            fragmentManager.executePendingTransactions();
            fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.add(R.id.fragmentISignUp, userSignUp);
            Toast.makeText(getApplicationContext(),"You are inside user signup Button",Toast.LENGTH_LONG).show();
            fragmentTransaction.show(userSignUp);
            //fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentLogin));
            fragmentTransaction.hide(userLogIn);
            //fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentWelcome));

            //fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentRegister));
            fragmentTransaction.hide(registerUsingEmail);
        } else if (button == 2) {
         //   userLogIn = (UserLogIn) fragmentManager.findFragmentById(R.id.fragmentLogin);

            fragmentTransaction.remove(userLogIn);
            fragmentTransaction.commit();
            fragmentManager.executePendingTransactions();
            fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.add(R.id.fragmentLogin, userLogIn);
            Toast.makeText(getApplicationContext(),"You are inside user login Button",Toast.LENGTH_LONG).show();
            fragmentTransaction.show(userLogIn);
            //fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentISignUp));
            fragmentTransaction.hide(userSignUp);
            //fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentWelcome));

          //  fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentRegister));
            fragmentTransaction.hide(registerUsingEmail);
        }
        fragmentTransaction.commit();

    }

我现在面临的问题是我没有看到现有的欢迎片段被用户签名或用户登录片段替换。我包含了 toast 消息以查看代码是否正在运行,当响应方法为时我看到这些消息由按钮调用,但我没有看到用户界面得到更新。我检查了move fragments 并尝试了那里的所有内容,但没有任何效果

【问题讨论】:

    标签: android android-fragments


    【解决方案1】:

    从表面上看,你从来没有真正将Fragments 添加到你的Activity,你只是调用了 hide 方法。

    案例并指出您的MainActivity

    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(R.id.fragmentWelcome,welcomePage);
    fragmentTransaction.show(welcomePage);
    
    // These are never added, you just hide them
    fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentLogin));
    fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentISignUp));
    fragmentTransaction.hide(fragmentManager.findFragmentById(R.id.fragmentRegister));
    fragmentTransaction.commit();
    

    如果我们在FragmentTransaction 中检查hide 方法的documentation,我们可以看到简单地隐藏未附加的东西不会做任何事情。您应该创建这些Fragments 的实例,并以与WelcomePage 类似的方式添加它们,然后隐藏它们。

    我还看到您错误地使用了一些FragmentTransaction 方法。作为一个快速总结hideshow 用于简单地更改Fragment 是否仍然可见(换句话说,它没有与Activity 分离)。但是,replace 方法会分离它在给定 id 中找到的任何 Fragment,并将输入的 `Fragment 附加到它的位置。

    【讨论】:

    • 我更新了我的代码以添加片段,但是当我现在点击按钮时,新片段不会替换旧片段.. 请查看我的新代码
    • 为您的MainActivity发布xml
    • 对不起,我的时间不多了,所以不得不将所有内容从片段更改为活动(虽然我不喜欢它,但目前没有其他选择)
    【解决方案2】:

    问题是您使用了不同的 Communicator 接口,没有响应您应该只使用 Communicator 的一个实例,因此您需要将它从您的主要活动传递给您的片段。..

    并且在 mainactivity 接口中只会被调用.. 也不要将 Communicator 实现到 Fragments 只有 mainactivity 将由 Communicator 实现.. 并将其传递给您的 Fragments 构造函数

    【讨论】:

    • 是的,我也这样做了.. 通信器只在主要活动中实现,片段只调用活动方法
    猜你喜欢
    • 1970-01-01
    • 2013-07-25
    • 2018-10-08
    • 2014-06-22
    • 2016-07-28
    • 2020-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多