fragment高级进阶
 {
this.context = context;
}
public libai() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View inflate = inflater.inflate(R.layout.fragment_libai, container, false);
TextView textView=inflate.findViewById(R.id.lb_text);
textView.setText("我是李白");
Button button=inflate.findViewById(R.id.lb_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "我是李白", Toast.LENGTH_SHORT).show();
}
});
return inflate;
}
}
package com.example.myday04rikao;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private RadioButton button,button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=findViewById(R.id.radio_hx);
button1=findViewById(R.id.radio_lb);
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.add(R.id.frame_layout,new hanxin(MainActivity.this));
fragmentTransaction.commit();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
hanxin hanxin = new hanxin(MainActivity.this);
LayoutInflater layoutInflater = getLayoutInflater();
View inflate = layoutInflater.inflate(R.layout.fragment_hanxin, null);
Button hh=inflate.findViewById(R.id.hx_button);
hh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "我是韩信...", Toast.LENGTH_SHORT).show();
}
});
fragmentTransaction.replace(R.id.frame_layout,hanxin);
fragmentTransaction.commit();
}
});
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
libai libai = new libai(MainActivity.this);
LayoutInflater layoutInflater = getLayoutInflater();
View inflate = layoutInflater.inflate(R.layout.fragment_libai, null);
Button ll=inflate.findViewById(R.id.lb_button);
ll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "我是李白...", Toast.LENGTH_SHORT).show();
}
});
fragmentTransaction.replace(R.id.frame_layout,libai);
fragmentTransaction.commit();
}
});
}
}
fragment 传值给Activity
package com.example.myday05;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.io.BufferedReader;
public class MainActivity extends AppCompatActivity implements BlankFragment.AAA {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=findViewById(R.id.m_text);
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.add(R.id.frame_layour,new BlankFragment());
fragmentTransaction.commit();
}
@Override
public void show(String s) {
textView.setText(s);
}
}
package com.example.myday05;
import android.content.Context;
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 android.widget.EditText;
import android.widget.TextView;
/**
* A simple {@link Fragment} subclass.
*/
public class BlankFragment extends Fragment {
private AAA aaa;
private EditText text;
public BlankFragment() {
// Required empty public constructor
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
aaa= (AAA) getActivity();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View inflate = inflater.inflate(R.layout.fragment_blank, container, false);
text = inflate.findViewById(R.id.two_text);
Button button=inflate.findViewById(R.id.two_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
aaa.show(text.getText().toString());
}
});
return inflate;
}
public interface AAA{
void show(String s);
}
}
fragment传值给fragment
package com.example.myday05lainxi02;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
/**
* A simple {@link Fragment} subclass.
*/
public class Left extends Fragment {
private EditText editText;
private AAA aaa;
public Left() {
// Required empty public constructor
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
aaa= (AAA) getActivity().getSupportFragmentManager().findFragmentByTag("right");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View inflate = inflater.inflate(R.layout.fragment_left, container, false);
editText=inflate.findViewById(R.id.left_text);
Button button=inflate.findViewById(R.id.left_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s = editText.getText().toString();
aaa.show(s);
}
});
return inflate;
}
public interface AAA{
void show(String s);
}
}
package com.example.myday05lainxi02;
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.TextView;
/**
* A simple {@link Fragment} subclass.
*/
public class Right extends Fragment implements Left.AAA {
private TextView textView;
public Right() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View inflate = inflater.inflate(R.layout.fragment_right, container, false);
textView=inflate.findViewById(R.id.right_text);
return inflate;
}
@Override
public void show(String s) {
textView.setText(s);
}
}
package com.example.myday05lainxi02;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
Left left = new Left();
Right right = new Right();
fragmentTransaction.add(R.id.left_frame,left,"left");
fragmentTransaction.add(R.id.right_frame,right,"right");
fragmentTransaction.commit();
}
}