郑州app开发Fragment之间进行通信实例。
布局代码主要展示一个,就是activity_main.xml
{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"
tools:context=".MainActivity" }
{LinearLayout
android:id="@+id/ll_left"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal" /}
{LinearLayout
android:id="@+id/ll_right"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal" /}
{/LinearLayout}
布局代码一共三个一个是main另外两个没有什么特别,主要是一个textview,做信息展示用。在这里展示main主要是因为布局都是LinearLayout,用约束布局,老显示不了Fragment的信息。
下面是java代码
MainActivity.java
package cn.xhhkj.videoview;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import android.app.FragmentTransaction;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "结果是";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager manager = getSupportFragmentManager();
androidx.fragment.app.FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.ll_left, new LeftFragment(),"left");
transaction.replace(R.id.ll_right, new RightFragment(),"right");
transaction.commit();
}
}
LeftFragment.java
package cn.xhhkj.videoview;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class LeftFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_left, null);
Button btn_call = view.findViewById(R.id.btn_call);
btn_call.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RightFragment right = (RightFragment) getActivity().getSupportFragmentManager().findFragmentByTag("right");
right.changeText("hello");
}
});
return view;
}
}
RightFragment.java
package cn.xhhkj.videoview;
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 RightFragment extends Fragment {
private TextView text;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_right, null);
text = view.findViewById(R.id.tv_text);
return view;
}
public void changeText(String s){
text.setText(s);
}
}
总结,作为这个实例的最重要的一点在这句话 RightFragment right = (RightFragment) getActivity().getSupportFragmentManager().findFragmentByTag("right");尤其是后半句getActivity().getSupportFragmentManager().findFragmentByTag("right");这句话,就是从这个Fragment跟另外一个Fragment之间的通信。