1、基本介绍
- FragmentPagerAdapter 是 Android 中用于 ViewPager 与 Fragment 结合使用的适配器类
2、演示
(1)Fragment Layout
- fragment_home.xml
xml
复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="首页页面"
android:textSize="24sp" />
</LinearLayout>
- fragment_discover.xml
xml
复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发现页面"
android:textSize="24sp" />
</LinearLayout>
- fragment_profile.xml
xml
复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="个人页面"
android:textSize="24sp" />
</LinearLayout>
(2)Fragment Code
- HomeFragment.java
java
复制代码
public class HomeFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_home, container, false);
}
}
- DiscoverFragment.java
java
复制代码
public class DiscoverFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_discover, container, false);
}
}
- ProfileFragment.java
java
复制代码
public class ProfileFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_profile, container, false);
}
}
(3)Adapter
java
复制代码
public class MyViewPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public MyViewPagerAdapter(@NonNull FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
@NonNull
@Override
public Fragment getItem(int position) {
if (fragments == null) return null;
return fragments.get(position);
}
@Override
public int getCount() {
return fragments == null ? 0 : fragments.size();
}
}
(4)Activity Layout
- activity_fragment_pager_adapter_test.xml
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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".FragmentPagerAdapterTestActivity">
<androidx.viewpager.widget.ViewPager
android:id="@+id/vp_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/black" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_home"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="首页"
android:textSize="18sp" />
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@color/black" />
<TextView
android:id="@+id/tv_discover"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="发现"
android:textSize="18sp" />
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@color/black" />
<TextView
android:id="@+id/tv_profile"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="我的"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
(5)Activity Code
- FragmentPagerAdapterTestActivity.java
java
复制代码
public class FragmentPagerAdapterTestActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_fragment_pager_adapter_test);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
test();
}
private void test() {
ViewPager vpContent = findViewById(R.id.vp_content);
TextView tvHome = findViewById(R.id.tv_home);
TextView tvDiscover = findViewById(R.id.tv_discover);
TextView tvProfile = findViewById(R.id.tv_profile);
List<Fragment> fragments = new ArrayList<>();
fragments.add(new HomeFragment());
fragments.add(new DiscoverFragment());
fragments.add(new ProfileFragment());
MyViewPagerAdapter myViewPagerAdapter = new MyViewPagerAdapter(getSupportFragmentManager(), fragments);
vpContent.setAdapter(myViewPagerAdapter);
tvHome.setOnClickListener(v -> {
tvHome.setTextColor(0xFF0000FF);
tvDiscover.setTextColor(0xFF000000);
tvProfile.setTextColor(0xFF000000);
vpContent.setCurrentItem(0);
});
tvDiscover.setOnClickListener(v -> {
tvDiscover.setTextColor(0xFF0000FF);
tvHome.setTextColor(0xFF000000);
tvProfile.setTextColor(0xFF000000);
vpContent.setCurrentItem(1);
});
tvProfile.setOnClickListener(v -> {
tvProfile.setTextColor(0xFF0000FF);
tvHome.setTextColor(0xFF000000);
tvDiscover.setTextColor(0xFF000000);
vpContent.setCurrentItem(2);
});
tvHome.setTextColor(0xFF0000FF);
tvDiscover.setTextColor(0xFF000000);
tvProfile.setTextColor(0xFF000000);
vpContent.setCurrentItem(0);
}
}
Pair
1、基本介绍
android.util.Pair 是 Android 提供的一个简单的数据容器类,它用于存储两个相关联的对象,一对键值或两个相关值,类似于 Java 中的 Map.Entry
2、演示
- 创建 Pair
java
复制代码
Pair<String, Integer> userAge = new Pair<>("张三", 25);
Pair<Integer, String> errorCode = Pair.create(404, "Not Found");
Log.i(TAG, userAge.toString());
Log.i(TAG, errorCode.toString());
复制代码
# 输出结果
Pair{张三 25}
Pair{404 Not Found}
- 访问 Pair
java
复制代码
Pair<String, Integer> userAge = new Pair<>("张三", 25);
String name = userAge.first;
int age = userAge.second;
ClipboardManager
1、基本介绍
- ClipboardManager 是 Android 系统中管理剪贴板操作的类,负责应用程序之间和内部的文本、图像、URI 等数据的复制粘贴功能
2、演示
(1)Activity Layout
xml
复制代码
<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ClipboardTestActivity">
<Button
android:id="@+id/btn_copy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="复制"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读取"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn_copy" />
</androidx.constraintlayout.widget.ConstraintLayout>
(2)Activity Code
java
复制代码
Button btnCopy = findViewById(R.id.btn_copy);
Button btnRead = findViewById(R.id.btn_read);
btnCopy.setOnClickListener(v -> {
ClipboardManager clipboardManager = getSystemService(ClipboardManager.class);
ClipData clipData = ClipData.newPlainText("text", "hello clipboard");
clipboardManager.setPrimaryClip(clipData);
});
btnRead.setOnClickListener(v -> {
ClipboardManager clipboardManager = getSystemService(ClipboardManager.class);
if (clipboardManager.hasPrimaryClip()) {
ClipData clipData = clipboardManager.getPrimaryClip();
if (clipData != null && clipData.getItemCount() > 0) {
CharSequence text = clipData.getItemAt(0).getText();
if (text != null) {
Toast.makeText(this, "读取到的文本为:" + text, Toast.LENGTH_SHORT).show();
}
}
}
});
1、基本介绍
- PopupWindow 是 Android 中用于在任意位置显示弹出窗口的组件,类似于对话框但更灵活
2、演示
xml
复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/popup_background"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提示"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="这是一个弹出窗口" />
<Button
android:id="@+id/btn_confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="确定" />
</LinearLayout>
xml
复制代码
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/white" />
<corners android:radius="8dp" />
<stroke
android:width="1dp"
android:color="@android:color/darker_gray" />
</shape>
(2)Activity Layout
xml
复制代码
<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PopupWindowTestActivity">
<Button
android:id="@+id/btn_open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
(3)Activity Code
java
复制代码
View popupView = LayoutInflater.from(this).inflate(R.layout.popup_layout, null);
PopupWindow popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setOutsideTouchable(false);
Button btnConfirm = popupView.findViewById(R.id.btn_confirm);
btnConfirm.setOnClickListener(v -> {
Toast.makeText(this, "点击了确认按钮", Toast.LENGTH_SHORT).show();
popupWindow.dismiss();
});
Button btnOpen = findViewById(R.id.btn_open);
btnOpen.setOnClickListener(v -> {
popupWindow.showAsDropDown(v);
});