我对Android开发还很陌生,所以我有点失落。我正在尝试使用滑动标签类型导航制作应用程序以进入不同的屏幕。
我一直在谷歌搜索它几个小时,我似乎找不到任何打破它足以让我理解的东西。从我读到的,我将不得不使用片段?现在,我只是将我的所有屏幕都作为单独的活动,如果我想这样做,我可能不得不改变它,对吗?
除了搞乱xml之外,我还没有真正编写任何代码,所以我真的没什么可说的。
无论如何,如果有人能向我解释如何做到这一点会很棒!
答案 0 :(得分:0)
使用新项目向导并选择“选项卡式活动”有什么问题?
答案 1 :(得分:0)
您可以将 Viewpager 与 PagerTitleStrip 一起使用..
<FrameLayout 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"
tools:context="com.example.myapp.ContainerFragment">
<!-- TODO: Update blank fragment layout -->
<android.support.v4.view.ViewPager
android:id="@+id/mPager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.PagerTitleStrip
android:id="@+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="@android:color/transparent"
android:textColor="#fff"
android:paddingTop="4dp"
android:paddingBottom="4dp" />
</android.support.v4.view.ViewPager>
这是ContainerFragment ..
import android.os.Bundle;
import android.app.Fragment;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class ContainerFragment extends android.support.v4.app.Fragment {
ViewPager mViewPager;
PagerAdapter pagerAdapter;
public NewsContainerFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_container, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
pagerAdapter = new PagerAdapter(getChildFragmentManager(), this);
mViewPager = (ViewPager) getActivity().findViewById(R.id.mPager);
mViewPager.setAdapter(pagerAdapter);
}
}
这是适配器..
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.util.Log;
public class PagerAdapter extends FragmentStatePagerAdapter {
public PagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new FirstFrag();
break;
case 1:
fragment = new SecondFrag();
break;
case 2:
fragment = new ThirdFrag();
break;
default:
break;
}
return fragment;
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
if (position == 0) {
return "FIRST FRAG";
} else if (position == 1) {
return "SECOND FRAG";
} else if (position == 2) {
return "THIRD FRAG";
} else {
return super.getPageTitle(position);
}
}
}