Day04Fragment传值

1.MainActivity传Fragment

public class MainActivity extends AppCompatActivity {
    private LinearLayout ll;
    private EditText edTx;
    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ll = (LinearLayout) findViewById(R.id.ll);
        edTx = (EditText) findViewById(R.id.ed_tx);
        btn = (Button) findViewById(R.id.btn);
    }

    public void btn(View view) {
        String s = edTx.getText().toString();

        FragmentManager supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        ContentFragment contentFragment = new ContentFragment();

        Bundle bundle = new Bundle();
        bundle.putString("name",s);
        contentFragment.setArguments(bundle);
        fragmentTransaction.replace(R.id.ll,contentFragment);
        fragmentTransaction.commit();
    }
}
------------------------------
public class ContentFragment extends Fragment {
    private TextView fgView;
    public ContentFragment() {
        // 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_content, container, false);
        fgView = (TextView) inflate.findViewById(R.id.fg_view);
        Bundle arguments = getArguments();
        if (arguments!=null){
            String name = arguments.getString("name");
            fgView.setText(name);
        }
        return inflate;
    }
}
Main布局
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/ed_tx">
</EditText>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/btn"
    android:onClick="btn"
    android:text="send">

</Button>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ll"
        android:orientation="vertical">
    </LinearLayout>
    Fragment布局
        <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fg_view"
        android:text="@string/hello_blank_fragment" />

2.Fragment传MainActivity

Main页面
 @Override
    public void send(String s) {
        tv1.setText(s);
    }
Fragment
 interface MyListener{
        void send(String s);
    }
    private MyListener myListener;
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_send, container, false);
        fraEdit = (EditText) inflate.findViewById(R.id.fra_edit);
        btn = (Button)inflate.findViewById(R.id.btn);

        myListener = (MyListener) getActivity();

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s = fraEdit.getText().toString();

                myListener.send(s);
            }
        });
        return inflate;
    }

3. listViewMain传值Fragment

Main页面----------
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String food_str = food.getData().get(position).getFood_str();
                String title = food.getData().get(position).getTitle();
                String collect_num = food.getData().get(position).getCollect_num();
                fragmentManager = getSupportFragmentManager();
                fragmentTransaction = fragmentManager.beginTransaction();
                ListFragment listFragment = new ListFragment();

                Bundle bundle = new Bundle();
                bundle.putString("key",food_str);
                bundle.putString("key1",title);
                bundle.putString("key2",collect_num+"");

                listFragment.setArguments(bundle);

                fragmentTransaction.replace(R.id.ll,listFragment);
                fragmentTransaction.commit();
            }
        });
Fragment页面-------------------
 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_list, container, false);
        fraTv = inflate.findViewById(R.id.fra_tv);
        fraTv1 = inflate.findViewById(R.id.fra_tv1);
        fraTv2 = inflate.findViewById(R.id.fra_tv2);
        Bundle arguments = getArguments();
        if (arguments!=null){
            String key = arguments.getString("key");
            String key1 = arguments.getString("key1");
            String key2 = arguments.getString("key2");
            fraTv1.setText(key1);
            fraTv.setText(key);

            fraTv2.setText(key2);
        }
        return inflate;
    }
Frament.xml -------------------
 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="1">
    <TextView
        android:id="@+id/fra_tv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/hello_blank_fragment" />
        <TextView
            android:id="@+id/fra_tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/hello_blank_fragment" />
    <TextView
        android:id="@+id/fra_tv2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/hello_blank_fragment" />
    </LinearLayout>
Main页面-----------
  <ListView
        android:id="@+id/listView"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
        android:layout_weight="6">
    </ListView>
    <LinearLayout
        android:id="@+id/ll"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_weight="1"
        android:orientation="vertical">
    </LinearLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值