如何将片段包含在活动中?

时间:2016-08-08 16:05:03

标签: java android android-layout android-fragments

我无法将我的片段包含在活动中。我尝试使用FragmentManager,但没有很好的结果。请帮帮我!我想在Google按钮上登录ParrentActivity类。 的 BlankFragment

    package com.qoobico.gplus; 


import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.annotation.NonNull; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 

import com.google.android.gms.auth.api.Auth; 
import com.google.android.gms.auth.api.signin.GoogleSignInAccount; 
import com.google.android.gms.auth.api.signin.GoogleSignInOptions; 
import com.google.android.gms.auth.api.signin.GoogleSignInResult; 
import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.SignInButton; 
import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.common.api.ResultCallback; 
import com.google.android.gms.common.api.Status; 


/** 
* A simple {@link Fragment} subclass. 
*/ 
public class BlankFragment extends Fragment implements View.OnClickListener, GoogleApiClient.OnConnectionFailedListener { 

TextView tv_username; 
GoogleApiClient mGoogleApiClient; 
private static final int RC_SIGN_IN = 9001; 
private boolean isAuthenticated = false; 
SignInButton sign_in_button; 
private Context mContext; 
private Activity mActivity; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
mActivity = getActivity(); 
mContext = getActivity().getApplicationContext(); 
signIn(); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
Bundle savedInstanceState) { 

View view = inflater.inflate(R.layout.fragment_blank, 
container, false); 


GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) 
.requestEmail() 
.build(); 

mGoogleApiClient = new GoogleApiClient.Builder(getActivity()) 
.enableAutoManage(getActivity() /* FragmentActivity */, this /* OnConnectionFailedListener */) 
.addApi(Auth.GOOGLE_SIGN_IN_API,gso) 
.build(); 
setGooglePlusButtonText(this.sign_in_button, "Sign in"); 
return view; 
} 


@Override 
public void onClick(View v) { 

switch (v.getId()) { 
case R.id.sign_in_button: 
if (!isAuthenticated) { 
signIn(); 
//this.sign_in_button.setVisibility(View.INVISIBLE); 
setGooglePlusButtonText(this.sign_in_button, "Sign out"); 
isAuthenticated = true; 
} else { 
signOut(); 
setGooglePlusButtonText(this.sign_in_button, "Sign in"); 
isAuthenticated = false; 
} 

break; 
} 

} 

@Override 
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 

} 

public void signIn() { 
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); 
startActivityForResult(signInIntent, RC_SIGN_IN); 
} 

public void signOut() { 
Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback( 
new ResultCallback<Status>() { 
@Override 
public void onResult(Status status) { 
tv_username.setText(""); 
} 
}); 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
super.onActivityResult(requestCode, resultCode, data); 

// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...); 
if (requestCode == RC_SIGN_IN) { 
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); 
handleSignInResult(result); 
} 
} 

private void handleSignInResult(GoogleSignInResult result) { 

if (result.isSuccess()) { 
// Signed in successfully, show authenticated UI. 
GoogleSignInAccount acct = result.getSignInAccount(); 
tv_username.setText(getString(R.string.signed_in_fmt, acct.getDisplayName())); 

} else { 
// Signed out, show unauthenticated UI. 
// updateUI(false); 
} 
} 


protected void setGooglePlusButtonText(SignInButton signInButton, String buttonText) { 
// Find the TextView that is inside of the SignInButton and set its text 
for (int i
Select this message
= 0; i < signInButton.getChildCount(); i++) { 
View v = signInButton.getChildAt(i); 

if (v instanceof TextView) { 
TextView tv = (TextView) v; 
tv.setText(buttonText); 
return; 
} 
} 
} 
}

ParrentActivity

package com.qoobico.gplus; 

import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
public class ParrentActivity extends AppCompatActivity implements View.OnClickListener { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_parrent); 




} 

@Override 
public void onClick(View view) { 
switch (view.getId()){ 


} 
} 
}

BlankFragment.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" 
tools:context="com.qoobico.gplus.BlankFragment"> 

<TextView 
android:id="@+id/tv_username" 
android:layout_marginTop="50dp" 
android:textColor="#ffffff" 
android:layout_centerHorizontal="true" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
/> 


<com.google.android.gms.common.SignInButton 
android:id="@+id/btnSignIn" 
android:layout_width="120dp" 
android:layout_height="wrap_content" 
android:layout_marginBottom="190dp" 
android:layout_alignParentBottom="true" 
android:layout_centerHorizontal="true" /> 

<Button 
android:id="@+id/btnSignOut" 
android:layout_centerHorizontal="true" 
android:layout_alignParentBottom="true" 
android:text="Sign out" 
android:layout_marginBottom="50dp" 
android:layout_width="120dp" 
android:layout_height="50dp" /> 
</RelativeLayout>

ParrentActivity.xml

    <?xml version="1.0" encoding="utf-8"?> 
<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:orientation="horizontal" 
tools:context="com.qoobico.gplus.ParrentActivity" 
android:weightSum="1"> 


<EditText 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:inputType="textPassword" 
android:ems="10" 
android:id="@+id/editText2" 
android:layout_weight="1" 
android:layout_below="@+id/editText" 
android:layout_alignLeft="@+id/editText" 
android:layout_alignStart="@+id/editText" /> 

<EditText 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:inputType="textPersonName" 
android:text="Name" 
android:ems="10" 
android:id="@+id/editText" 
android:layout_weight="1" 
android:layout_alignParentTop="true" 
android:layout_centerHorizontal="true" /> 

<EditText 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:inputType="textEmailAddress" 
android:ems="10" 
android:id="@+id/editText3" 
android:layout_below="@+id/editText2" 
android:layout_alignLeft="@+id/editText2" 
android:layout_alignStart="@+id/editText2" /> 

<Button 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="New Button" 
android:id="@+id/button" 
android:layout_below="@+id/editText3" 
android:layout_centerHorizontal="true" /> 

<fragment 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:name="com.qoobico.gplus.BlankFragment" 
android:id="@+id/fragmentB" 
android:layout_alignParentBottom="true" 
android:layout_centerHorizontal="true" 
android:layout_marginBottom="67dp" /> 

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

通常我使用SupportFragmentManager,但基本上可以这样。

    FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
    Fragment fragment = Fragment.instantiate(this, class, bundle);
    ft.add(viewIdHolder, fragment, tag);
    ft.commit();