<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/vv_fragContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<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"
android:background="@android:color/holo_orange_light"
tools:context=".FragmentOne">
<TextView
android:id="@+id/vv_tvOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="88dp"
android:text="Hello First Fragment"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/vv_btnOne01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginBottom="116dp"
android:text="Upper"
app:layout_constraintBottom_toTopOf="@+id/vv_btnOne02"
app:layout_constraintStart_toStartOf="@+id/vv_btnOne02" />
<Button
android:id="@+id/vv_btnOne02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="253dp"
android:text="Lower"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
android:background="@android:color/holo_blue_bright" @+id/vv_tvTwo @+id/vv_btnTwo01 @+id/vv_btnTwo02
public interface MyFragmentDataPassListener {
public void cf_firedByFragment(String str, int source);
}
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity implements MyFragmentDataPassListener {
FragmentOne frag01;
FragmentTwo frag02;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
frag01 = new FragmentOne();
frag02 = new FragmentTwo();
setContentFragment(1);
}
public static void loadFragment(AppCompatActivity activity, int containerId, Fragment fragment, String tag) {
activity.getSupportFragmentManager().beginTransaction().
replace(containerId, fragment,tag).commitAllowingStateLoss();
}
public void setContentFragment(int id) {
switch (id) {
case 1: loadFragment(this, R.id.vv_fragContainer,frag01,"fragment1");
break;
case 2: loadFragment(this, R.id.vv_fragContainer,frag02,"fragment2");
break;
}
}
@Override
public void cf_firedByFragment(String str, int source) {
if (source == 1) {
setContentFragment(2);
}
else if (source == 2) {
setContentFragment(1);
}
}
import android.content.Context;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class FragmentOne extends Fragment {
TextView cv_tv;
private MyFragmentDataPassListener cv_listener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof MyFragmentDataPassListener) {
cv_listener = (MyFragmentDataPassListener) context;
} else {
throw new ClassCastException(context.toString() + " must MyFragmentDataPassListener");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_one, container, false);
cv_tv = (TextView) v.findViewById(R.id.vv_tvOne);
Button lv_btn01 = (Button) v.findViewById(R.id.vv_btnOne01);
lv_btn01.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cv_listener.cf_firedByFragment("UPPER", 1);
}
});
Button lv_btn02 = (Button) v.findViewById(R.id.vv_btnOne02);
lv_btn02.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cv_listener.cf_firedByFragment("LOWER", 1);
}
});
return v;
}
}
almost identical
cv_listener.cf_firedByFragment("#FF0000", 2);
cv_listener.cf_firedByFragment("#00FF00", 2);
The important thing to keep in mind is that fragments should not directly communicate with each other and should generally only communicate with their parent activity.
public void cf_firedByFragment(String str, int source) {
if (source == 1) {
setContentFragment(2);
// this will wait until frag02 onCreateView() done, otherwise null
getSupportFragmentManager().executePendingTransactions();
frag02.cf_setTextCase(str);
}
else if (source == 2) {
setContentFragment(1);
getSupportFragmentManager().executePendingTransactions();
frag01.cf_setTextColor(str);
}
}
public void cf_setTextColor(String str) {
cv_tv.setTextColor(Color.parseColor(str));
}
public void cf_setTextCase(String str) {
if (str.equals("UPPER")) {
cv_tv.setText(cv_tv.getText().toString().toUpperCase());
}
else {
cv_tv.setText(cv_tv.getText().toString().toLowerCase());
}
}
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.043"/>
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="49dp" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/images"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
private ViewPager2 viewPager2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager2 = findViewById(R.id.viewpager);
viewPager2.setAdapter(new MyViewPager2Adapter(this));
}
public class MyViewPager2Adapter extends RecyclerView.Adapter<MyViewPager2Adapter.MyViewHolder> {
private int[] images = {R.drawable.ic_launcher_background, R.drawable.ic_launcher_foreground};
private Context ctx;
MyViewPager2Adapter(Context ctx) {
this.ctx = ctx;
}
// This method returns our layout
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(ctx).inflate(R.layout.images_holder, parent, false);
return new MyViewHolder(view);
}
// This method binds the screen with the view
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
// This will set the images in imageview
holder.images.setImageResource(images[position]);
}
// This Method returns the size of the Array
@Override
public int getItemCount() {
return images.length;
}
// The ViewHolder class holds the view
public static class MyViewHolder extends RecyclerView.ViewHolder {
ImageView images;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
images = itemView.findViewById(R.id.images);
}
}
}
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_orange_dark">
</androidx.constraintlayout.widget.ConstraintLayout>
android:background="@android:color/holo_green_light">
android:background="@android:color/holo_blue_light">
private int[] pages = {R.layout.page01, R.layout.page02, R.layout.page03};
...
viewPager2 = findViewById(R.id.viewpager);
viewPager2.setAdapter(new MyViewPager2Adapter(pages));
...
...
private int[] layouts;
MyViewPager2Adapter(int[] layouts) {
this.layouts = layouts;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(viewType, parent, false);
return new MyViewHolder(view);
}
...
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="112dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="184dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent" />
...
private String[] pages = {"First Screen", "Second Screen", "Third Screen"};
viewPager2 = findViewById(R.id.viewpager);
viewPager2.setAdapter(new MyViewPager2Adapter(pages));
...
private String[] layouts;
private int[] colorArray = new int[]{android.R.color.holo_orange_light, android.R.color.holo_green_light,
android.R.color.holo_blue_light};
MyViewPager2Adapter(String[] layouts) {
this.layouts = layouts;
}
// This method returns our layout
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.single, parent, false);
return new MyViewHolder(view);
}
// This method binds the screen with the view
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
//// HERE
holder.textview.setText(layouts[position]);
holder.constraintlayout.setBackgroundResource(colorArray[position]);
}
@Override
public int getItemViewType(int position) {
return colorArray[position];
}
// This Method returns the size of the Array
@Override
public int getItemCount() {
return layouts.length;
}
// The ViewHolder class holds the view
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView textview;
Button button;
ConstraintLayout constraintlayout;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
textview = itemView.findViewById(R.id.textview);
button = itemView.findViewById(R.id.button);
constraintlayout = itemView.findViewById(R.id.constraintlayout);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textview.setTextSize(textview.getTextSize() + 0.1f);
}
});
}
}