gpt4 book ai didi

Android fragment 选项卡主机+ viewpager滑动?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:45:38 24 4
gpt4 key购买 nike

我目前有一个 fragment 选项卡主机,由本教程制作 http://maxalley.wordpress.com/2013/05/18/android-creating-a-tab-layout-with-fragmenttabhost-and-fragments/

唯一的区别是我在 fragment 中使用它。所以我有一个带有选项卡的 fragment ,每个选项卡都有另一个 fragment ..

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />

<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />


</LinearLayout>

fragment 类

public class AddServiceFragment extends Fragment {




public AddServiceFragment() {
// Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


}


public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment_add_service, container, false);
FragmentTabHost mTabHost = (FragmentTabHost) view.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(), android.R.id.tabcontent);


String[] tabs = new String[]{"text1", "text2"};
mTabHost.addTab(
mTabHost.newTabSpec("tab1").setIndicator(tabs[0], null),
MedicalHistoryFragment.class, null);


mTabHost.addTab(
mTabHost.newTabSpec("tab2").setIndicator(tabs[1], null),
MedicalHistoryFragment.class, null);

return view;
}

这有效,我可以看到我的标签。现在我想实现一个 viewpager,以便能够向左或向右滑动。

我从 android 开发者页面上读到这个 http://developer.android.com/training/animation/screen-slide.html

但我想不通,我应该把 viewpager 放在哪里?我希望我的 fragment 独立于我的 Activity ...

最佳答案

TabHost 不提供 ViewPager 支持。
另外,我建议你不要使用 TabHost——这是旧的风格。
使用 PagerTabStrip

看这个Gist .

更新:我添加了一些代码,例如,如果这个 Gist 被删除;

主 Activity .java

package ch.pboos.android.sample.viewpager;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(new SampleFragmentPagerAdapter());
}

public class SampleFragmentPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 5;

public SampleFragmentPagerAdapter() {
super(getSupportFragmentManager());
}

@Override
public int getCount() {
return PAGE_COUNT;
}

@Override
public Fragment getItem(int position) {
return PageFragment.create(position + 1);
}
}

public static class PageFragment extends Fragment {
public static final String ARG_PAGE = "ARG_PAGE";

private int mPage;

public static PageFragment create(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
PageFragment fragment = new PageFragment();
fragment.setArguments(args);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARG_PAGE);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_page, container, false);
TextView textView = (TextView) view;
textView.setText("Fragment #" + mPage);
return view;
}
}
}

activity_main.xml(没有 PagerTabStrip)

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>

activity_main_2.xml(带 PagerTabStrip)

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<android.support.v4.view.PagerTabStrip
android:id="@+id/pager_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#000"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#fff" />

</android.support.v4.view.ViewPager>

fragment _页面.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />

获取页面标题

public CharSequence getPageTitle(int position) {
return "Page " + (position + 1);
}

UPD 2带 fragment :

public class MainFragment extends Fragment{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_main_2, container, false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(new SampleFragmentPagerAdapter());
}

public class SampleFragmentPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 5;

public SampleFragmentPagerAdapter() {
super(getSupportFragmentManager());
}

@Override
public int getCount() {
return PAGE_COUNT;
}

@Override
public Fragment getItem(int position) {
return PageFragment.create(position + 1);
}
}

public static class PageFragment extends Fragment {
public static final String ARG_PAGE = "ARG_PAGE";

private int mPage;

public static PageFragment create(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
PageFragment fragment = new PageFragment();
fragment.setArguments(args);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARG_PAGE);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_page, container, false);
TextView textView = (TextView) view;
textView.setText("Fragment #" + mPage);
return view;
}
}
}

希望对你有帮助

关于Android fragment 选项卡主机+ viewpager滑动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26381850/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com