gpt4 book ai didi

android - 如何使用不同的 fragment/布局实现 ViewPager

转载 作者:IT老高 更新时间:2023-10-28 12:53:32 25 4
gpt4 key购买 nike

当我启动一个实现 viewpager 的 Activity 时,viewpager 创建了各种 fragment 。我想为每个 fragment 使用不同的布局,但问题是 viewpager 最多只显示两个布局(1 之后所有剩余 fragment 的第二个布局)。

这是实现 viewpager 的 SwipeActivity 的代码:

public class SwipeActivity extends FragmentActivity
{

MyPageAdapter pageAdapter;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_swipe);
pageAdapter = new MyPageAdapter(getSupportFragmentManager());
ViewPager pager=(ViewPager)findViewById(R.id.pager);
pager.setAdapter(pageAdapter);
ActionBar bar = getActionBar();
bar.setDisplayHomeAsUpEnabled(true);
}
/**
* Custom Page adapter
*/
private class MyPageAdapter extends FragmentPagerAdapter
{
public MyPageAdapter(FragmentManager fm)
{
super(fm);
}
@Override
public int getCount()
{
return 5;
}
@Override
public Fragment getItem(int position)
{
switch(position)
{
case 0: return new MyFragment();
case 1: return SecondFragment.newInstance("asdasd");
default : return RamFragment.newInstance("s");
}
}
}
}

这是 fragment

的代码
public class MyFragment extends Fragment
{
@Override
public View onCreateView(LayoutInflater paramLayoutInflater, ViewGroup paramViewGroup, Bundle paramBundle)
{
return paramLayoutInflater.inflate(R.layout.processorlayout, paramViewGroup, false);
}
}

我使用了 5 个这样的 fragment ,它们都有不同的布局,但 viewpager 最多只显示 2 个。

编辑:SecondFragment的代码

public class SecondFragment extends Fragment
{
public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";

public static final SecondFragment newInstance(String paramString)
{
SecondFragment f = new SecondFragment();
Bundle localBundle = new Bundle(1);
localBundle.putString("EXTRA_MESSAGE", paramString);
f.setArguments(localBundle);
return f;
}

@Override
public View onCreateView(LayoutInflater paramLayoutInflater, ViewGroup paramViewGroup, Bundle paramBundle)
{
return paramLayoutInflater.inflate(R.layout.motherboardlayout, paramViewGroup, false);
}
}

最佳答案

由于这是一个非常常见的问题,我想花时间和精力详细解释具有多个 fragment 和布局的 ViewPager。给你。

具有多个 fragment 和布局文件的 ViewPager - 操作方法

The following is a complete example of how to implement a ViewPager with different fragment Types and different layout files.

在这种情况下,我有 3 个 Fragment 类,每个类都有一个不同的布局文件。为了简单起见,fragment-layouts 仅在背景颜色上有所不同 。当然,任何布局文件都可以用于 Fragments。

FirstFragment.java 具有 orange 背景布局,SecondFragment.java 具有 green 背景布局,ThirdFragment.java 具有 red 背景布局.此外,每个 Fragment 显示不同的文本,具体取决于它来自哪个类以及它是哪个实例。

Also be aware that I am using the support-library's Fragment: android.support.v4.app.Fragment

MainActivity.java(初始化 Viewpager 并将其适配器作为内部类)。再次查看 imports。我正在使用 android.support.v4 包。

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

public class MainActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

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

private class MyPagerAdapter extends FragmentPagerAdapter {

public MyPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int pos) {
switch(pos) {

case 0: return FirstFragment.newInstance("FirstFragment, Instance 1");
case 1: return SecondFragment.newInstance("SecondFragment, Instance 1");
case 2: return ThirdFragment.newInstance("ThirdFragment, Instance 1");
case 3: return ThirdFragment.newInstance("ThirdFragment, Instance 2");
case 4: return ThirdFragment.newInstance("ThirdFragment, Instance 3");
default: return ThirdFragment.newInstance("ThirdFragment, Default");
}
}

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

activity_main.xml(MainActivitys .xml 文件)- 一个简单的布局文件,仅包含填满整个屏幕的 ViewPager。

<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/viewPager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>

fragment 类,FirstFragment.java 导入android.support.v4.app.Fragment;

public class FirstFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.first_frag, container, false);

TextView tv = (TextView) v.findViewById(R.id.tvFragFirst);
tv.setText(getArguments().getString("msg"));

return v;
}

public static FirstFragment newInstance(String text) {

FirstFragment f = new FirstFragment();
Bundle b = new Bundle();
b.putString("msg", text);

f.setArguments(b);

return f;
}
}

first_frag.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"
android:background="@android:color/holo_orange_dark" >

<TextView
android:id="@+id/tvFragFirst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="26dp"
android:text="TextView" />
</RelativeLayout>

SecondFragment.java

public class SecondFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.second_frag, container, false);

TextView tv = (TextView) v.findViewById(R.id.tvFragSecond);
tv.setText(getArguments().getString("msg"));

return v;
}

public static SecondFragment newInstance(String text) {

SecondFragment f = new SecondFragment();
Bundle b = new Bundle();
b.putString("msg", text);

f.setArguments(b);

return f;
}
}

second_frag.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"
android:background="@android:color/holo_green_dark" >

<TextView
android:id="@+id/tvFragSecond"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="26dp"
android:text="TextView" />

</RelativeLayout>

ThirdFragment.java

public class ThirdFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.third_frag, container, false);

TextView tv = (TextView) v.findViewById(R.id.tvFragThird);
tv.setText(getArguments().getString("msg"));

return v;
}

public static ThirdFragment newInstance(String text) {

ThirdFragment f = new ThirdFragment();
Bundle b = new Bundle();
b.putString("msg", text);

f.setArguments(b);

return f;
}
}

third_frag.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"
android:background="@android:color/holo_red_light" >

<TextView
android:id="@+id/tvFragThird"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="26dp"
android:text="TextView" />

</RelativeLayout>

最终结果如下:

Viewpager有5个Fragment,Fragment 1是FirstFragment类型,显示first_frag.xml布局,Fragment 2是SecondFragment类型,显示second_frag.xml,Fragment 3-5是ThirdFragment类型,都显示third_frag.xml。

enter image description here

您可以在上方看到 5 个 fragment ,可以通过向左或向右滑动来在它们之间切换。当然只能同时显示一个 Fragment。

最后但同样重要的是:

I would recommend that you use an empty constructor in each of your Fragment classes.

不要通过构造函数传递潜在的参数,而是使用 newInstance(...) 方法和 Bundle 来传递参数。

这样,如果分离并重新附加,对象状态可以通过参数存储。很像 Bundles 附加到 Intents

关于android - 如何使用不同的 fragment/布局实现 ViewPager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18413309/

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