java
package com.zx.tab;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private final List<Fragment> fragmentList = new ArrayList<>();
private final int[] tabIds = {
R.id.ll_home,
R.id.ll_mine
};
private final int[] iconIds = {
R.id.im_home,
R.id.im_mine
};
private final int[] textIds = {
R.id.tv_home,
R.id.tv_mine
};
private ViewPager2 viewPager2=null;
private ImageView[] tabIcons=null;
private TextView[] tabTexts=null;
private ImageView imCurrent=null;
private TextView tvCurrent=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.initializeViews();
this.initViewPager();
this.changeTab(0);
}
private void initializeViews() {
this.viewPager2 = findViewById(R.id.viewPager2);
LinearLayout[] tabLayouts = new LinearLayout[2];
this.tabIcons = new ImageView[2];
this. tabTexts = new TextView[2];
for (int i = 0; i < 2; i++) {
tabLayouts[i] = findViewById(tabIds[i]);
tabIcons[i] = findViewById(iconIds[i]);
tabTexts[i] = findViewById(textIds[i]);
final int index = i;
tabLayouts[i].setOnClickListener(view -> {
viewPager2.setCurrentItem(index, false);
changeTab(index);
});
}
}
private void initViewPager() {
this. fragmentList.add(ClientHomeNewFragment.newInstance(getString(R.string.tv_home)));
this.fragmentList.add(ClientMeFragment.newInstance(getString(R.string.tv_mine)));
this.viewPager2.setAdapter(new FragmentAdapter(getSupportFragmentManager(), getLifecycle(), fragmentList));
this. viewPager2.setUserInputEnabled(false);//是否滑动
this.viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
super.onPageScrolled(position, positionOffset, positionOffsetPixels);
changeTab(position);
}
});
}
private void changeTab(int position) {
if (imCurrent != null && tvCurrent != null) {
this.imCurrent.setSelected(false);
this.tvCurrent.setSelected(false);
}
this.imCurrent = tabIcons[position];
this.tvCurrent = tabTexts[position];
this.imCurrent.setSelected(true);
this.tvCurrent.setSelected(true);
}
}
Fragment适配器
java
package com.zx.tab;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import java.util.List;
/****
* 用于管理Fragment。
*/
public class FragmentAdapter extends FragmentStateAdapter {
private List<Fragment> fragmentList;
/**
* Instantiates a new Fragment adapter.
* @param fragmentManager
* the fragment manager
* @param lifecycle
* the lifecycle
* @param fragments
* the fragments
*/
public FragmentAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle, List<Fragment> fragments) {
super(fragmentManager, lifecycle);
fragmentList = fragments;
}
@NonNull
@Override
public Fragment createFragment(int position) {
return fragmentList.get(position);
}
@Override
public int getItemCount() {
if (fragmentList!= null && !fragmentList.isEmpty()) {
return fragmentList.size();
} else {
return 0 ;
}
}
}
下面是fragment,2个我我这点给出一一个,另外一个都是一样的
java
package com.zx.tab;
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 ClientHomeNewFragment extends Fragment {
private static final String ARG_PARAM1 = "首页";
public static ClientHomeNewFragment newInstance(String param1) {
ClientHomeNewFragment quoteFragment = new ClientHomeNewFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
quoteFragment.setArguments(args);
return quoteFragment;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View contentView = inflater.inflate(R.layout.home, container, false);
return contentView;
}
}
下面是布局
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:background="@color/white"
android:orientation="vertical">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPager2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<View
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:background="#f0f0f0" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="@color/white"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/ll_home"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/im_home"
android:layout_width="24dp"
android:layout_height="24dp"
android:background="@drawable/tab_menu_home" />
<TextView
android:id="@+id/tv_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tv_home"
android:textColor="@drawable/tabar_title_text"
android:textSize="12sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll_mine"
android:layout_width="0dp"
android:layout_height="56dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/im_mine"
android:layout_width="24dp"
android:layout_height="24dp"
android:background="@drawable/tab_menu_mine" />
<TextView
android:id="@+id/tv_mine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tv_mine"
android:textColor="@drawable/tabar_title_text"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
效果图
详情请见详情案例
https://download.csdn.net/download/qq_41733851/88879647?spm=1001.2101.3001.9500