Android Studio 1-5 Fragment 基础知识与高级进阶 未完待续……
一、fragment 给 activty传值
1、MainActivty的代码
package com. example. day05ex;
import androidx. appcompat. app. AppCompatActivity;
import androidx. fragment. app. FragmentManager;
import androidx. fragment. app. FragmentTransaction;
import android. os. Bundle;
import android. widget. TextView;
public class MainActivity extends AppCompatActivity implements ShowFragment. MyListener {
private TextView aTv;
@Override
public void sendMessage ( String string) {
aTv. setText ( string) ;
}
@Override
protected void onCreate ( Bundle savedInstanceState) {
super . onCreate ( savedInstanceState) ;
setContentView ( R. layout. activity_main) ;
aTv = ( TextView) findViewById ( R. id. a_tv) ;
FragmentManager supportFragmentManager = getSupportFragmentManager ( ) ;
FragmentTransaction fragmentTransaction = supportFragmentManager. beginTransaction ( ) ;
ShowFragment showFragment= new ShowFragment ( ) ;
fragmentTransaction. add ( R. id. a_fm, showFragment) ;
fragmentTransaction. commit ( ) ;
}
}
2、MainActivty的布局
< ? xml version= "1.0" encoding= "utf-8" ? >
< LinearLayout xmlns: android= "http://schemas.android.com/apk/res/android"
xmlns: app= "http://schemas.android.com/apk/res-auto"
xmlns: tools= "http://schemas.android.com/tools"
android: layout_width= "match_parent"
android: layout_height= "match_parent"
android: orientation= "vertical"
android: id= "@+id/LL"
tools: context= ".MainActivity"
>
< TextView
android: id= "@+id/a_tv"
android: layout_width= "match_parent"
android: layout_height= "wrap_content"
android: textSize= "30sp"
android: text= "展示文本"
/ >
< fragment
android: id= "@+id/a_fm"
android: name= "com.example.day05ex.ShowFragment"
android: layout_width= "match_parent"
android: layout_height= "wrap_content" >
< / fragment>
< / LinearLayout>
3 、Frament的布局
< ? xml version= "1.0" encoding= "utf-8" ? >
< LinearLayout 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= "vertical"
tools: context= ".ShowFragment" >
< EditText
android: id= "@+id/f_et"
android: layout_width= "match_parent"
android: layout_height= "wrap_content"
android: hint= "写点东西"
/ >
< Button
android: id= "@+id/f_btn"
android: layout_width= "wrap_content"
android: layout_height= "wrap_content"
android: text= "发送一下"
/ >
< / LinearLayout>
4、Frament的代码
package com. example. day05ex;
import android. content. Context;
import android. os. Bundle;
import androidx. fragment. app. Fragment;
import androidx. fragment. app. FragmentActivity;
import android. view. LayoutInflater;
import android. view. View;
import android. view. ViewGroup;
import android. widget. Button;
import android. widget. EditText;
public class ShowFragment extends Fragment {
private EditText fEt;
private Button fBtn;
private MyListener myListener;
@Override
public void onAttach ( Context context) {
super . onAttach ( context) ;
myListener = ( MyListener) getActivity ( ) ;
}
public ShowFragment ( ) {
}
@Override
public View onCreateView ( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View inflate = inflater. inflate ( R. layout. fragment_show, container, false ) ;
fEt = ( EditText) inflate. findViewById ( R. id. f_et) ;
fBtn = ( Button) inflate. findViewById ( R. id. f_btn) ;
fBtn. setOnClickListener ( new View. OnClickListener ( ) {
@Override
public void onClick ( View view) {
String string = fEt. getText ( ) . toString ( ) ;
if ( string != null) {
myListener. sendMessage ( string) ;
}
}
} ) ;
return inflate;
}
public interface MyListener {
void sendMessage ( String string) ;
}
}
二、fragment 给 fragment 传值
1、activty布局与代码
< ? xml version= "1.0" encoding= "utf-8" ? >
< LinearLayout xmlns: android= "http://schemas.android.com/apk/res/android"
xmlns: app= "http://schemas.android.com/apk/res-auto"
xmlns: tools= "http://schemas.android.com/tools"
android: layout_width= "match_parent"
android: layout_height= "match_parent"
tools: context= ".activity.doubleF_Activity"
>
< LinearLayout
android: id= "@+id/left"
android: layout_width= "0dp"
android: layout_height= "match_parent"
android: layout_weight= "1" / >
< FrameLayout
android: id= "@+id/right"
android: layout_width= "0dp"
android: layout_height= "match_parent"
android: layout_weight= "1" / >
< / LinearLayout>
package com. example. day05ex. activity;
import androidx. appcompat. app. AppCompatActivity;
import androidx. fragment. app. FragmentManager;
import androidx. fragment. app. FragmentTransaction;
import android. os. Bundle;
import com. example. day05ex. R;
import com. example. day05ex. fragment. LeftFragment;
import com. example. day05ex. fragment. RightFragment;
public class doubleF_Activity extends AppCompatActivity {
@Override
protected void onCreate ( Bundle savedInstanceState) {
super . onCreate ( savedInstanceState) ;
setContentView ( R. layout. activity_double_f_) ;
FragmentManager supportFragmentManager = getSupportFragmentManager ( ) ;
FragmentTransaction fragmentTransaction = supportFragmentManager. beginTransaction ( ) ;
LeftFragment leftFragment = new LeftFragment ( ) ;
RightFragment rightFragment = new RightFragment ( ) ;
fragmentTransaction. add ( R. id. left, leftFragment) ;
fragmentTransaction. add ( R. id. right, rightFragment, "right" ) ;
fragmentTransaction. commit ( ) ;
}
}
2、 fragment布局和代码
< ? xml version= "1.0" encoding= "utf-8" ? >
< LinearLayout 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= "vertical"
tools: context= ".fragment.LeftFragment" >
< EditText
android: id= "@+id/ed_left_edit"
android: layout_width= "wrap_content"
android: layout_height= "wrap_content"
android: hint= "输入内容传到右边"
/ >
< Button
android: id= "@+id/btn_left_send"
android: layout_width= "wrap_content"
android: layout_height= "wrap_content"
android: text= "点击按钮"
/ >
< / LinearLayout>
package com. example. day05ex. fragment;
import android. os. Bundle;
import androidx. annotation. Nullable;
import androidx. fragment. app. Fragment;
import android. view. LayoutInflater;
import android. view. View;
import android. view. ViewGroup;
import android. widget. Button;
import android. widget. EditText;
import android. widget. Toast;
import com. example. day05ex. R;
public class LeftFragment extends Fragment {
private EditText edLeftEdit;
private Button btnLeftSend;
private MyListener myListener;
public LeftFragment ( ) {
}
@Override
public void onActivityCreated ( @Nullable Bundle savedInstanceState) {
super . onActivityCreated ( savedInstanceState) ;
myListener = ( MyListener) getActivity ( ) . getSupportFragmentManager ( ) . findFragmentByTag ( "right" ) ;
}
@Override
public View onCreateView ( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View inflate = inflater. inflate ( R. layout. fragment_left, container, false ) ;
edLeftEdit = ( EditText) inflate. findViewById ( R. id. ed_left_edit) ;
btnLeftSend = ( Button) inflate. findViewById ( R. id. btn_left_send) ;
btnLeftSend. setOnClickListener ( new View. OnClickListener ( ) {
@Override
public void onClick ( View view) {
String text = edLeftEdit. getText ( ) . toString ( ) ;
myListener. sendMessage ( text) ;
}
} ) ;
return inflate;
}
public interface MyListener {
void sendMessage ( String string) ;
}
}
< ? xml version= "1.0" encoding= "utf-8" ? >
< FrameLayout 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= ".fragment.RightFragment" >
< TextView
android: id= "@+id/tv_right_show"
android: layout_width= "match_parent"
android: layout_height= "match_parent"
android: text= "显示文本"
/ >
< / FrameLayout>
package com. example. day05ex. fragment;
import android. os. Bundle;
import androidx. annotation. Nullable;
import androidx. fragment. app. Fragment;
import android. view. LayoutInflater;
import android. view. View;
import android. view. ViewGroup;
import android. widget. TextView;
import com. example. day05ex. R;
public class RightFragment extends Fragment implements LeftFragment. MyListener {
private TextView tvRightShow;
public RightFragment ( ) {
}
@Override
public View onCreateView ( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View inflate = inflater. inflate ( R. layout. fragment_right, container, false ) ;
tvRightShow = ( TextView) inflate. findViewById ( R. id. tv_right_show) ;
return inflate;
}
@Override
public void sendMessage ( String string) {
tvRightShow. setText ( string) ;
}
}