Mình đang xây dựng nhóm "anh Thầy & Đồng bọn (Hỏi đáp, chia sẻ kiến thức)" mục đích để các bạn có một nơi giao lưu trao đổi.
Đội MOD của group và mọi người sẽ hỗ trợ bạn ngay lập tức.
Join ở đây nè: https://www.facebook.com/groups/anhthayvadongbon
Lượt xem: 1272 lượt xem
1. Tổng quan về Fragment, một số khái niệm cơ bản
<fragment
android:id="@+id/listFragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
class="com.example.android.fragment3.MyFragment1"
tools:layout="@layout/fragment1">
</fragment>
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.fragment1, container, false);
return view;
}
package com.vncoder.fragment;
import androidx.appcompat.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);
}
}
package com.vncoder.fragment;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class fragment1 extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater,container,savedInstanceState);
View view = inflater.inflate(R.layout.activity_fragment1,container,false);
return view;
}
}
package com.vncoder.fragment;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class fragment2 extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater,container,savedInstanceState);
View view = inflater.inflate(R.layout.activity_fragment2,container,false);
return view;
}
}
<?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:orientation="vertical"
android:layout_height="match_parent"
tools:context=".fragment1">
<TextView
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="fragment 1"
android:textSize="30dp"
android:layout_gravity="center"
/>
</LinearLayout>
<?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"
tools:context=".fragment2">
<TextView
android:background="@color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="fragment 2"
android:textSize="30dp"
android:layout_gravity="center"
/>
</LinearLayout>
<?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=".MainActivity">
<fragment
android:id="@+id/listFragment"
class="com.vncoder.fragment.fragment1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@layout/activity_fragment1">
</fragment>
<fragment
android:id="@+id/detailFragment"
class="com.vncoder.fragment.fragment2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@layout/activity_fragment2">
</fragment>
</LinearLayout>
2. Quản lý Fragment
FragmentManager fm = getFragmentManager();
// add
FragmentTransaction ft_add = fm.beginTransaction();
ft_add.add(R.id.your_placehodler,new YourFragment());
ft_add.commit();
// replace
FragmentTransaction ft_rep = fm.beginTransaction();
ft_rep.replace(R.id.your_placehodler, new YourFragment());
ft_rep.commit();
// remove
Fragment fragment = fm.findFragmentById(R.id.your_placehodler);
FragmentTransaction ft_remo = fm.beginTransaction();
ft_remo.remove(fragment);
ft_remo.commit();
3. Truyền dữ liệu giữa các Fragment
package com.vncoder.fragment;
public interface DemoFragmentInterface {
void sendData(String name, String age);
}
package com.vncoder.fragment;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements DemoFragmentInterface {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
Fragment1 fragment1 = new Fragment1();
transaction.add(R.id.containerTop,fragment1);
Fragment2 fragment2 = new Fragment2();
transaction.add(R.id.containerBottom,fragment2);
transaction.commit();
}
@Override
public void sendData(String name, String age) {
Fragment2 fragment2 = (Fragment2) getSupportFragmentManager().findFragmentById(R.id.containerBottom);
if (fragment2 != null || fragment2.isInLayout()){
fragment2.showInfor(name,age);
}else {
Toast.makeText(getApplicationContext(),"???",Toast.LENGTH_LONG).show();
}
}
}
package com.vncoder.fragment;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Fragment1 extends Fragment implements View.OnClickListener{
private EditText edtName, edtAge;
private Button btnSend;
private DemoFragmentInterface listener;
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof MainActivity)
this.listener = (DemoFragmentInterface) context;
else
throw new RuntimeException(context.toString() + "must implement onViewSelected!");
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater,container,savedInstanceState);
View view = inflater.inflate(R.layout.activity_fragment1,container,false);
edtName = view.findViewById(R.id.edtName);
edtAge = view.findViewById(R.id.edtAge);
btnSend = view.findViewById(R.id.btnSend);
btnSend.setOnClickListener(this);
return view;
}
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.btnSend:
String name = edtName.getText().toString().trim();
String age = edtAge.getText().toString().trim();
if (!name.equals("") && !age.equals("")) {
listener.sendData(name, age);
} else if (name.equals("")) {
edtName.requestFocus();
} else {
edtAge.requestFocus();
}
break;
}
}
}
package com.vncoder.fragment;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class Fragment2 extends Fragment {
private TextView tvName, tvAge;
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater,container,savedInstanceState);
View view = inflater.inflate(R.layout.activity_fragment2,container,false);
tvName = (TextView) view.findViewById(R.id.tvName);
tvAge = (TextView) view.findViewById(R.id.tvAge);
return view;
}
public void showInfor(String name, String age){
tvName.setText(name);
tvAge.setText(age);
}
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:background="@color/colorPrimary"
tools:context=".Fragment1">
<LinearLayout
android:gravity="center"
android:layout_gravity="center"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:textSize="30dp"
android:id="@+id/edtName"
android:hint="Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:textSize="30dp"
android:id="@+id/edtAge"
android:hint="Age"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btnSend"
android:text="SEND"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".Fragment2">
<LinearLayout
android:gravity="center"
android:layout_gravity="center"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvName"
android:textSize="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tvAge"
android:textSize="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</FrameLayout>
<?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:weightSum="2"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="@+id/containerTop"
class="com.vncoder.fragment.Fragment1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@layout/activity_fragment1">
</fragment>
<fragment
android:id="@+id/containerBottom"
class="com.vncoder.fragment.Fragment2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@layout/activity_fragment2">
</fragment>
</LinearLayout>
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Trang web này không lưu trữ bất kỳ tệp (files) nào trên máy chủ (server). Chúng tôi chỉ lập chỉ mục và liên kết đến nội dung được cung cấp bởi các trang web khác. Vui lòng liên hệ với các nhà cung cấp nội dung để xóa nội dung bản quyền nếu có và gửi email cho chúng tôi, chúng tôi sẽ xóa các liên kết hoặc nội dung có liên quan ngay lập tức.
Bạn có muốn xác nhận hoàn thành bài học này không?