11-23-2015, 10:14 AM
Cung cấp một tin nhắn tới một Fragment
Các hoạt động chủ có thể chuyển tải thông điệp đến một mảnh bằng cách bắt dụ Fragment với findFragmentById(), sau đó trực tiếp gọi các phương thức công cộng của mảnh.
Ví dụ, hãy tưởng tượng rằng các hoạt động hiển thị ở trên có thể chứa một mảnh đó được sử dụng để hiển thị các mục theo quy định của dữ liệu trả về trong phương thức callback trên. Trong trường hợp này, hoạt động này có thể vượt qua các thông tin nhận được trong phương pháp gọi lại để các mảnh khác sẽ hiển thị các mục:
Code:public static class MainActivity extends Activity
implements HeadlinesFragment.OnHeadlineSelectedListener{
...
public void onArticleSelected(int position) {
// The user selected the headline of an article from the HeadlinesFragment
// Do something here to display that article
ArticleFragment articleFrag = (ArticleFragment)
getSupportFragmentManager().findFragmentById(R.id.article_fragment);
if (articleFrag != null) {
// If article frag is available, we're in two-pane layout...
// Call a method in the ArticleFragment to update its content
articleFrag.updateArticleView(position);
} else {
// Otherwise, we're in the one-pane layout and must swap frags...
// Create fragment and give it an argument for the selected article
ArticleFragment newFragment = new ArticleFragment();
Bundle args = new Bundle();
args.putInt(ArticleFragment.ARG_POSITION, position);
newFragment.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
}
}
Thường thì bạn sẽ muốn một Fragment để giao tiếp với nhau, ví dụ như thay đổi nội dung dựa trên một sự kiện người dùng. Tất cả các thông tin liên lạc Fragment-to-Fragment được thực hiện thông qua các hoạt động liên quan. Hai mảnh vỡ sẽ không bao giờ liên lạc trực tiếp.lập trình android
Xác định một giao diện
Để cho phép một Fragment để giao tiếp lên đến Hoạt động của nó, bạn có thể định nghĩa một giao diện trong lớp Fragment và thực hiện nó trong các hoạt động. Các Fragment chụp thực hiện giao diện trong suốt onAttach () phương pháp vòng đời của nó và sau đó có thể gọi các phương pháp giao diện để giao tiếp với các hoạt động.
Dưới đây là một ví dụ của Fragment để truyền thông Hoạt động:
Code:public class HeadlinesFragment extends ListFragment {
OnHeadlineSelectedListener mCallback;
// Container Activity must implement this interface
public interface OnHeadlineSelectedListener {
public void onArticleSelected(int position);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (OnHeadlineSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
...
}>> Khóa học lập trình android cơ bản nâng cao tại VietPro!
Bây giờ các mảnh vỡ có thể gửi tin nhắn đến các hoạt động bằng cách gọi onArticleSelected () phương pháp (hoặc các phương pháp khác trong giao diện) sử dụng các ví dụ mCallback của giao diện OnHeadlineSelectedListener.
Ví dụ, các phương pháp sau đây trong đoạn được gọi khi người dùng nhấp vào một mục danh sách. Đoạn sử dụng giao diện gọi lại để cung cấp các sự kiện để các hoạt động của phụ huynh.
Code:@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Send the event to the host activity
mCallback.onArticleSelected(position);
}
Thực hiện các giao diện
Để nhận được callbacks sự kiện từ các mảnh vỡ, hoạt động mà tổ chức đó phải thực hiện các giao diện được định nghĩa trong lớp mảnh.
Ví dụ, các hoạt động sau đây thực hiện các giao diện từ các ví dụ trên.
Code:public static class MainActivity extends Activity
implements HeadlinesFragment.OnHeadlineSelectedListener{
...
public void onArticleSelected(int position) {
// The user selected the headline of an article from the HeadlinesFragment
// Do something here to display that article
}
}
Tham khảo thêm Những khóa học photoshop cơ bản tại đây.
Code:
public static class MainActivity extends Activity
implements HeadlinesFragment.OnHeadlineSelectedListener{
...
public void onArticleSelected(int position) {
// The user selected the headline of an article from the HeadlinesFragment
// Do something here to display that article
ArticleFragment articleFrag = (ArticleFragment)
getSupportFragmentManager().findFragmentById(R.id.article_fragment);
if (articleFrag != null) {
// If article frag is available, we're in two-pane layout...
// Call a method in the ArticleFragment to update its content
articleFrag.updateArticleView(position);
} else {
// Otherwise, we're in the one-pane layout and must swap frags...
// Create fragment and give it an argument for the selected article
ArticleFragment newFragment = new ArticleFragment();
Bundle args = new Bundle();
args.putInt(ArticleFragment.ARG_POSITION, position);
newFragment.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
}
}Code:
public class HeadlinesFragment extends ListFragment {
OnHeadlineSelectedListener mCallback;
// Container Activity must implement this interface
public interface OnHeadlineSelectedListener {
public void onArticleSelected(int position);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (OnHeadlineSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
...
}Code:
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Send the event to the host activity
mCallback.onArticleSelected(position);
}Code:
public static class MainActivity extends Activity
implements HeadlinesFragment.OnHeadlineSelectedListener{
...
public void onArticleSelected(int position) {
// The user selected the headline of an article from the HeadlinesFragment
// Do something here to display that article
}
}