【ANDROID STUDIO】 Fragment Communication

package com.example.fragmentcommunication;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class DetailFragment extends Fragment {

public static String KEY_COUNTRY_NAME = "KEY_COUNTRY_NAME";


@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_detail, container, false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

Bundle bundle = getArguments();

if (bundle != null && bundle.containsKey(KEY_COUNTRY_NAME)) {
showSelectedCountry(bundle.getString(KEY_COUNTRY_NAME));
}
}

public void showSelectedCountry(String countryName) {
((TextView) getView().findViewById(R.id.textViewCountryName)).setText(countryName);
}
}
package com.example.fragmentcommunication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.widget.FrameLayout;

public class MainActivity extends AppCompatActivity {

boolean mDualPane;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

MasterFragment masterFragment = null;
FrameLayout frameLayout = findViewById(R.id.frameLayout);
if (frameLayout != null) {
mDualPane = false;
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
masterFragment = (MasterFragment) getSupportFragmentManager()
.findFragmentByTag("MASTER");
if (masterFragment == null) {
masterFragment = new MasterFragment();
fragmentTransaction.add(R.id.frameLayout, masterFragment, "MASTER");
}
DetailFragment detailFragment = (DetailFragment)
getSupportFragmentManager().findFragmentById(R.id.frameLayoutDetail);
if (detailFragment != null) {
fragmentTransaction.remove(detailFragment);
}
fragmentTransaction.commit();
} else {
mDualPane = true;
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
masterFragment = (MasterFragment) getSupportFragmentManager()
.findFragmentById(R.id.frameLayoutMaster);
if (masterFragment == null) {
masterFragment = new MasterFragment();
fragmentTransaction.add(R.id.frameLayoutMaster, masterFragment);
}
DetailFragment detailFragment = (DetailFragment) getSupportFragmentManager()
.findFragmentById(R.id.frameLayoutDetail);
if (detailFragment == null) {
detailFragment = new DetailFragment();
fragmentTransaction.add(R.id.frameLayoutDetail, detailFragment);
}
fragmentTransaction.commit();
}
masterFragment.setOnMasterSelectedListener(new MasterFragment.OnMasterSelectedListener() {
@Override
public void onItemSelected(String countryName) {
sendCountryName(countryName);
}
});
}


private void sendCountryName(String countryName) {
DetailFragment detailFragment;
if (mDualPane) {
//Two pane layout
detailFragment = (DetailFragment) getSupportFragmentManager().findFragmentById(R.id.frameLayoutDetail);
detailFragment.showSelectedCountry(countryName);
} else {
// Single pane layout
detailFragment = new DetailFragment();
Bundle bundle = new Bundle();
bundle.putString(DetailFragment.KEY_COUNTRY_NAME, countryName);
detailFragment.setArguments(bundle);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frameLayout, detailFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
}
package com.example.fragmentcommunication;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import androidx.fragment.app.ListFragment;

public class MasterFragment extends ListFragment {

public interface OnMasterSelectedListener {
public void onItemSelected(String countryName);
}

private OnMasterSelectedListener mOnMasterSelectedListener = null;

public void setOnMasterSelectedListener(OnMasterSelectedListener listener) {
mOnMasterSelectedListener = listener;
}

public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

String[] countries = new String[]{"China", "France",
"Germany", "India", "Russia", "United Kingdom",
"United States"};

ListAdapter countryAdapter = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_list_item_1,
countries);

setListAdapter(countryAdapter);

getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);

getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (mOnMasterSelectedListener != null) {
mOnMasterSelectedListener.onItemSelected(((
TextView) view).getText().toString());
}
}
});
}
}
//layout

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

<FrameLayout android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="@+id/textViewCountryName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
//layout-land

<?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="horizontal">
<FrameLayout android:id="@+id/frameLayoutMaster"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
<FrameLayout android:id="@+id/frameLayoutDetail"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"/>
</LinearLayout>

No comments:

Quench Your Thirst for Savings: Unbeatable Water Filter Black Friday Deals in 2024

Black Friday 2024 is just around the corner, and it's the perfect time to snag incredible deals on essential home appliances. Among the ...

Contact Form

Name

Email *

Message *