ViewPager实现导航效果
下面展示一些 内联代码片
。
package com.example.myapplication3;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
public class TabViewPagerActivity extends AppCompatActivity implements TabHost.TabContentFactory {
private TabHost mTabHost;
@SuppressLint("ResourceType")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_viewpager);
mTabHost = findViewById(R.id.tab_host);
mTabHost.setup();
int[] titlesIDs = {R.string.home, R.string.message, R.string.me};
int[] drawableIDs = {R.drawable.main_tab_icon_home, R.drawable.main_tab_icon_message, R.drawable.main_tab_icon_me};
for (int index = 0; index < titlesIDs.length; index++) {
View view = getLayoutInflater().inflate(R.layout.main_tab_layout, null);
ImageView icon = view.findViewById(R.id.main_tab_icon);
TextView title = view.findViewById(R.id.main_tab_txt);
View tab = view.findViewById(R.id.tab_bg);
icon.setImageResource(drawableIDs[index]);
title.setText(titlesIDs[index]);
tab.setBackgroundColor(getResources().getColor(R.color.white));
mTabHost.addTab(mTabHost.newTabSpec(getString(titlesIDs[index])).setIndicator(view).setContent(this)
);
}
Fragment[] fragments = new Fragment[]{
TestFragment.newInstance("home"),
TestFragment.newInstance("message"),
TestFragment.newInstance("me"),
};
ViewPager viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@NonNull
@Override
public Fragment getItem(int position) {
return fragments[position];
}
@Override
public int getCount() {
return fragments.length;
}
});
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (mTabHost != null) {
mTabHost.setCurrentTab(position);
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
@Override
public void onTabChanged(String s) {
if (mTabHost != null) {
int position = mTabHost.getCurrentTab();
viewPager.setCurrentItem(position);
}
}
});
}
@Override
public View createTabContent(String s) {
View view = new View(this);
view.setMinimumHeight(0);
view.setMinimumWidth(0);
return view;
}
}
<?xml version="1.0" encoding="utf-8"?>
<TabHost 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/tab_host"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TabViewPagerActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/tab_divider" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/tab_divider"
android:visibility="gone"></FrameLayout>
<View
android:id="@+id/tab_divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_above="@android:id/tabs"
android:background="#dfdfdf" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:showDividers="none"></TabWidget>
</RelativeLayout>
</TabHost>
下面展示一些 内联代码片
。
<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="first"
android:textSize="40sp" />
</RelativeLayout>
<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="second"
android:textSize="40sp" />
</RelativeLayout>
下面展示一些 内联代码片
。
<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="third"
android:textSize="40sp" />
</RelativeLayout>
package com.example.myapplication3;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import android.icu.text.CaseMap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class TestFragment extends Fragment {
private static String TITLE = "title";
private String mTitle;
public static TestFragment newInstance(String title) {
TestFragment fragment = new TestFragment();
Bundle bundle = new Bundle();
bundle.putString(TITLE, title);
fragment.setArguments(bundle);
return fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mTitle = getArguments().getString(TITLE);
}
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_test, null);
TextView textView = view.findViewById(R.id.text_view);
textView.setText(mTitle);
return view;
}
}
下面展示一些 内联代码片
。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".TestFragment">
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:gravity="center"
android:text="@string/app_name"
android:textSize="36sp" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tab_bg"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/main_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/main_tab_icon"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="4dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/main_tab_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textColor="@color/color_main_tab_txt" />
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
</RelativeLayout>
下面展示一些 内联代码片
。
package com.example.myapplication3;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import java.util.ArrayList;
import java.util.List;
public class ImageViewPagerAdapter extends AppCompatActivity {
private ViewPager mViewPager;
private int[] mLayoutIDs = {R.layout.view_first, R.layout.view_second, R.layout.view_third};
private List<View> mViews;
private ViewGroup mDotViewGroup;
private List<ImageView> mDotViews = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_view_adapter);
mViewPager = findViewById(R.id.view_pager);
mDotViewGroup = findViewById(R.id.dot_layout);
// mViews = new ArrayList<>();
// for (int index = 0; index < mLayoutIDs.length; index++) {
// View view = getLayoutInflater().inflate(mLayoutIDs[index], null);
// mViews.add(view);
// }
mViews = new ArrayList<>();
for (int index = 0; index < mLayoutIDs.length; index++) {
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.mipmap.ic_launcher);
mViews.add(imageView);
ImageView dot = new ImageView(this);
dot.setImageResource(R.mipmap.ic_launcher);
dot.setMaxHeight(100);
dot.setMaxWidth(100);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(80, 80);
layoutParams.leftMargin = 20;
dot.setLayoutParams(layoutParams);
dot.setEnabled(false);
mDotViewGroup.addView(dot);
mDotViews.add(dot);
}
mViewPager.setAdapter(mPagerAdapter);
mViewPager.setOffscreenPageLimit(4);
mViewPager.setCurrentItem(1);
setDotViews(1);
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
setDotViews(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
PagerAdapter mPagerAdapter = new PagerAdapter() {
@Override
public int getCount() {
return mLayoutIDs.length;
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
View child = mViews.get(position);
container.addView(child);
return child;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView(mViews.get(position));
}
};
private void setDotViews(int position) {
for (int index = 0; index < mDotViews.size(); index++) {
mDotViews.get(index).setImageResource(position == index ? R.mipmap.me1 : R.mipmap.ic_launcher);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#cccccc" />
<item android:color="#4dd0c8" android:state_selected="true" />
<item android:color="#4dd0c8" android:state_pressed="true" />
</selector>