使用 ViewPager 和 Fragment 做一个简单版的好友列表界面 1. 使用 ViewPager 和 Fragment 做个可滑动界面 2. 使用 TabLayout 添加 Tab 支持给出文件的所有代码。要求可以直接运行,一定要给出import。一定要告诉我配置
时间: 2024-02-25 21:57:24 浏览: 105
好的,下面是代码:
FriendListActivity.java
```
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import android.os.Bundle;
import com.google.android.material.tabs.TabLayout;
import java.util.ArrayList;
import java.util.List;
public class FriendListActivity extends AppCompatActivity {
private ViewPager viewPager;
private TabLayout tabLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_friend_list);
viewPager = findViewById(R.id.view_pager);
tabLayout = findViewById(R.id.tab_layout);
List<Fragment> fragmentList = new ArrayList<>();
fragmentList.add(new OnlineFriendFragment());
fragmentList.add(new OfflineFriendFragment());
List<String> titleList = new ArrayList<>();
titleList.add("在线好友");
titleList.add("离线好友");
viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager(), fragmentList, titleList));
tabLayout.setupWithViewPager(viewPager);
}
private static class MyPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> fragmentList;
private List<String> titleList;
public MyPagerAdapter(FragmentManager fm, List<Fragment> fragmentList, List<String> titleList) {
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
this.fragmentList = fragmentList;
this.titleList = titleList;
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return titleList.get(position);
}
}
}
```
OnlineFriendFragment.java
```
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 OnlineFriendFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_online_friend, container, false);
}
}
```
OfflineFriendFragment.java
```
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 OfflineFriendFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_offline_friend, container, false);
}
}
```
activity_friend_list.xml
```
<?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">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:tabMode="fixed"
android:tabTextColor="#ffffff" />
<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/tab_layout" />
</RelativeLayout>
```
fragment_online_friend.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="20sp" />
</LinearLayout>
```
fragment_offline_friend.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="20sp" />
</LinearLayout>
```
配置说明:
1. 在 build.gradle (Module: app) 文件中添加以下依赖:
```
implementation 'androidx.viewpager:viewpager:1.0.0'
implementation 'com.google.android.material:material:1.0.0'
```
2. 在 activity_friend_list.xml 文件中添加 ViewPager 和 TabLayout 控件。
3. 创建 OnlineFriendFragment 和 OfflineFriendFragment 两个 Fragment。
4. 在 MyPagerAdapter 中添加 Fragment 和 Title。
5. 在 FriendListActivity 中设置 ViewPager 的 Adapter 和 TabLayout。
6. 运行程序即可。
阅读全文