我创建了以下主要类,向用户显示了欢迎片段。这个欢迎片段有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();
}
我现在面临的问题是我没有看到现有的欢迎片段被usersign或userlogin片段替换..我包含了toast消息以查看代码是否正在运行,并且我在响应方法时看到这些消息按钮调用,但我没有看到用户界面更新。我检查了move fragments并尝试了所有内容,但没有任何工作
答案 0 :(得分:0)
问题是您使用的是不同响应的Communicator接口,您只应使用Communicator的一个实例,因此需要将其传递给主活动中的片段。
并且在mainactivity接口中只会被调用..也不会对片段实现Communicator,只有Communicator将实现mainactivity ..并将其传递给片段构造函数
答案 1 :(得分:0)
根据它的外观,您实际上从未将Fragments
添加到Activity
,只需调用隐藏方法。
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();
如果我们在hide
中检查FragmentTransaction
方法的documentation,我们可以看到只是隐藏未附加的内容就不会做任何事情。您应该创建Fragments
的实例并以与WelcomePage
类似的方式添加它们,然后隐藏它们。
我也发现您错误地使用了某些FragmentTransaction
方法。快速摘要hide
和show
用于简单地更改Fragment
是否仍然可见(换句话说,它不会与Activity
分离) 。但是,replace
方法会分离它在给定Fragment
处找到的id
,并将输入的“片段”附加到其位置。