我正在使用fragment.replace在聊天应用程序中使用parse和sinch在用户列表中进行午餐,在开始时使用视图,但现在我正在更改为片段,但是地方只是附加而不是替换片段。我正在使用android.support.v4.app.Fragment,FragmentManager和FragmentTransaction。我究竟做错了什么 。 这是我的班级:
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.app.Activity;
import android.os.Bundle;
import com.parse.LogInCallback;
import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseUser;
import com.parse.SignUpCallback;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.paul.mapasxavo.ListUsersActivity;
import com.example.paul.mapasxavo.R;
public class ChatActivity extends Fragment {
private Button signUpButton;
private Button loginButton;
private EditText usernameField;
private EditText passwordField;
private String username;
private String password;
private Intent intent;
private Intent serviceIntent;
Context thiscontext;
private final String TAG="ChatActivity";
public ChatActivity(){
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (currentUser != null) {
}
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_chat, container, false);
loginButton = (Button) rootView.findViewById(R.id.loginButton);
signUpButton = (Button) rootView.findViewById(R.id.signupButton);
usernameField = (EditText) rootView.findViewById(R.id.loginUsername);
passwordField = (EditText) rootView.findViewById(R.id.loginPassword);
loginButton = (Button) rootView.findViewById(R.id.loginButton);
signUpButton = (Button) rootView.findViewById(R.id.signupButton);
usernameField = (EditText) rootView.findViewById(R.id.loginUsername);
passwordField = (EditText) rootView.findViewById(R.id.loginPassword);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
username = usernameField.getText().toString();
password = passwordField.getText().toString();
ParseUser.logInInBackground(username, password, new LogInCallback() {
public void done(ParseUser user, com.parse.ParseException e) {
if (user != null) {
ListUsersActivity listUsersActivity = new ListUsersActivity();
FragmentManager fragmentManager= getFragmentManager();
FragmentTransaction fragmentTransaction= fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.listados,listUsersActivity);
fragmentTransaction.commit();
Log.v(TAG,"boton login");
} else {
Toast.makeText(getActivity(),
getString(R.string.login_error),
Toast.LENGTH_LONG).show();
}
}
});
}
});
signUpButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
username = usernameField.getText().toString();
password = passwordField.getText().toString();
ParseUser user = new ParseUser();
user.setUsername(username);
user.setPassword(password);
user.signUpInBackground(new SignUpCallback() {
public void done(com.parse.ParseException e) {
if (e == null) {
ListUsersActivity listUsersActivity = new ListUsersActivity();
FragmentManager fragmentManager= getFragmentManager();
FragmentTransaction fragmentTransaction= fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.listados,listUsersActivity);
fragmentTransaction.commit();
Log.v(TAG,"boton registrar");
} else {
Toast.makeText(getActivity(),
getString(R.string.sigin_error)
, Toast.LENGTH_LONG).show();
}
}
});
}
});
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
@Override
public void onDetach() {
super.onDetach();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我的片段是这样的:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.paul.mapasxavo.ChatActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:id="@+id/listados"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="@string/txt_usuario"
android:id="@+id/loginUsernameText"
android:layout_gravity="center_horizontal"
android:textSize="20sp"/>
<EditText
android:id="@+id/loginUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:inputType="text"
android:padding="10dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp">
<requestFocus/>
</EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="@string/txt_password"
android:id="@+id/loginPasswordText"
android:layout_gravity="center_horizontal"
android:textSize="20sp"/>
<EditText
android:id="@+id/loginPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="40dp"
android:inputType="textPassword"
android:padding="10dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp">
</EditText>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="40dp"
android:layout_gravity="center">
<Button
style="@style/botones"
android:id="@+id/loginButton"
android:layout_width="168dp"
android:layout_height="wrap_content"
android:text="Login"
/>
<Button
style="@style/botones"
android:id="@+id/signupButton"
android:layout_width="163dp"
android:layout_height="wrap_content"
android:text="Registrar"
/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
我的替换片段是:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.paul.mapasxavo.ListUsersActivity">
<Button
style="@style/botones"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/logout"
android:id="@+id/logoutButton"
android:gravity="center_vertical|center_horizontal"
android:layout_gravity="center_horizontal"
android:textSize="24sp"
android:padding="15dp"
/>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:background="@color/colorPrimary"
android:id="@+id/usersListView">
</ListView>
</LinearLayout>
请提示一下。 非常感谢
答案 0 :(得分:0)
在论坛内搜索,根据此[link] [Android Replace Fragment within a Fragment找到了一些答案。我可以让它在片段事务和xml中进行一些更改。
聊天活动:
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
username = usernameField.getText().toString();
password = passwordField.getText().toString();
ParseUser.logInInBackground(username, password, new LogInCallback() {
public void done(ParseUser user, com.parse.ParseException e) {
if (user != null) {
FragmentManager fragmentManager = myContext.getSupportFragmentManager();
//start transaction
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ListUsersActivity listUsersActivity = new ListUsersActivity();
fragmentTransaction.addToBackStack("xyz"); fragmentTransaction.replace(R.id.listados,listUsersActivity);
fragmentTransaction.commit();
serviceIntent = new Intent(getActivity().getApplicationContext(), MessageService.class);
getActivity().startService(serviceIntent);
} else {
Toast.makeText(getActivity(),
getString(R.string.login_error),
Toast.LENGTH_LONG).show();
}
}
);
}
});
<强> activity_chat.xml:强>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:id="@+id/listados"
tools:context="com.example.paul.mapasxavo.ChatActivity">
现在它显示为最后一层的新图层。