Fragment
左侧菜单的布局文件 fragment_left.xml
<?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="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Left Fragmet" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="苹果"
android:id="@+id/btn1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="香蕉"
android:id="@+id/btn2"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="橘子"
android:id="@+id/btn3"/>
</LinearLayout>
左侧菜单的LeftFragment
使用New Class方式创建,继承系统的Fragment
package com.example.demo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
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, container, false);
return view;
}
}
右侧内容区域的布局fragment_right.xml
<?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="vertical"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:src="@drawable/appale"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="苹果是蔷薇科苹果亚科苹果属植物,其树为落叶乔木。苹果营养价值很高,富含矿物质和维生素,含钙量丰富,有助于代谢掉体内多余盐分,苹果酸可代谢热量"
/>
</LinearLayout>
右侧内容区域的RightFragment
package com.example.demo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class RightFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_right, container, false);
return view;
}
}
在主界面的布局文件中添加这两个Fragment
<?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"
android:orientation="horizontal">
<fragment
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:name="com.example.demo.LeftFragment"
android:id="@+id/left_fragment"/>
<fragment
android:layout_width="0dp"
android:layout_weight="4"
android:layout_height="match_parent"
android:name="com.example.demo.RightFragment"
android:id="@+id/right_fragment"/>
</LinearLayout>
事件处理
第一种 所有组件定义在MainActivity中
package com.example.demo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
String bananaText = " 香蕉的功效与作用 香蕉含有丰富的营养,香蕉在古代称它为甘蕉。它的果肉软糯,美味可口。香蕉是一种常见的水果,富含丰富的维生素和微量元素";
String orangeText = "橘子富含维生素C与柠檬酸,前者具有美容作用,后者则具有消除疲劳的作用。";
private Button btn2;
private ImageView ivFruit;
private TextView tvFruit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ivFruit = findViewById(R.id.iv_fruit);
tvFruit = findViewById(R.id.tv_fruit);
btn2 = findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("MainActivity", "btn2 clicked");
ivFruit.setImageResource(R.drawable.banana);
tvFruit.setText(bananaText);
}
});
}
}
第二种处理方式 组件分别定义在各自的Fragment中 MainActivity只负责事件的转发
1、在LeftFragment中定义和初始化Button 并实现点击事件
package com.example.demo;
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 {
private Button btn3;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_left, container, false);
btn3 = view.findViewById(R.id.btn3);
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 这里是看不到RightFragment的
MainActivity mainActivity = (MainActivity) getActivity();
mainActivity.changFruit("orange");
}
});
return view;
}
}
2.在MainActivity中定义和初始化RightFragment 添加changFruit方法进行转发
package com.example.demo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
// 把右侧Fragmet定义出来
private RightFragment rightFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rightFragment = (RightFragment) getSupportFragmentManager().findFragmentById(R.id.right_fragment);
}
public void changFruit(String name) {
Log.e("MainActivity", "changFruit");
// 让RightFragment实现内容切换
rightFragment.changeFruit(name);
}
}
3、在RightFragment中定义和初始化图片和文字组件,实现changFruit方法
package com.example.demo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class RightFragment extends Fragment {
String orangeText = "橘子富含维生素C与柠檬酸,前者具有美容作用,后者则具有消除疲劳的作用。";
private ImageView ivFruit;
private TextView tvFruit;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_right, container, false);
ivFruit = view.findViewById(R.id.iv_fruit);
tvFruit = view.findViewById(R.id.tv_fruit);
return view;
}
public void changeFruit(String name) {
if (name.equals("orange")) {
ivFruit.setImageResource(R.drawable.orange);
tvFruit.setText(orangeText);
}
}
}