gpt4 book ai didi

android - fragment 中的 ViewPager 不起作用

转载 作者:搜寻专家 更新时间:2023-11-01 07:35:21 26 4
gpt4 key购买 nike

所以我有一个带有 3 个 actionbar.Tab 的应用程序,它们在 3 个 fragment 之间切换。在其中两个 fragment 中,我需要有一个在大约 6 个 ListView 之间切换的 ViewPager。问题是我试图让它工作的 fragment 显示页面只显示黑色......总是......我已经确认我可以显示其他东西(我试着让我的 SherlockFragment 成为 SherlockListFragment 我可以在其中显示数据)。所以问题只限于我的适配器和我的 ViewPager,以及将它们放置到位的代码。哦,我还应该提到我正在使用 ActionBarSherlock 和(尝试)ViewPagerIndicator。我已正确导入(最终)。

所以这是我的 fragment 的代码:

import vt.finder.schedule.Schedule;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.actionbarsherlock.app.SherlockFragment;
import com.viewpagerindicator.TitlePageIndicator;


public class FreeTimeFragment extends SherlockFragment {

//~Data Fields--------------------------------------------
/**
* Adapter used for swiping between days.
*/
private ViewPager dayPage;
/**
* The DayAdapter that is used to switch between course Lists for each day for the listView.
*/
private DayAdapter dayAdapt;


//~Constructors--------------------------------------------
@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
View view = onCreateView(getLayoutInflater(savedInstanceState), null, savedInstanceState);

LinearLayout layout = (LinearLayout) view.findViewById(R.id.linear_layout);
dayPage = (ViewPager) layout.findViewById(R.id.free_time_day_pager);

Schedule freeTime = getArguments().getParcelable("freeTime");

dayAdapt = new DayAdapter(freeTime);
//Bind the title indicator to the adapter
TitlePageIndicator titleIndicator = (TitlePageIndicator) layout.findViewById(R.id.titles);
dayPage.setAdapter(dayAdapt);
titleIndicator.setViewPager(dayPage);
}

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

View view = inflater.inflate(R.layout.free_time_fragment_layout, container, false);
return view;
}
}

这是我的适配器的代码:

import vt.finder.schedule.Course;
import vt.finder.schedule.Schedule;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;


/**
* PageAdapter class, provides functionality for switching between days of
* the week.
*
*
*/
public final class DayAdapter extends PagerAdapter {

//~Data Fields----------------------------------------//
/**
* The list adapter that the current list is being handed to.
*/
private Schedule schedule;

//~Constructors---------------------------------------//
public DayAdapter(Schedule theSchedule) {

schedule = theSchedule;
}

//~Methods--------------------------------------------//
@Override
public Object instantiateItem(ViewGroup container, int index) {

ListView view = new ListView(container.getContext());

view.setAdapter(new ArrayAdapter<Course>(container.getContext(),
R.layout.list_view_child, schedule.getDay(curIndex).getList()));

((ViewPager) container).addView(view, 0);

return container;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {

ViewPager pager = (ViewPager) container;
View view = (View) object;
pager.removeView(view);
}

/**
* Gets the pageTitle, which is the name of the day that is at position.
*
* @param position the index of the day selected.
* @return the name of the day the index refers to.
*/
@Override
public CharSequence getPageTitle(int position) {

return schedule.getDay(position).getThisDay();
}

@Override
public int getCount() {

return 6;
}

@Override
public boolean isViewFromObject(View view, Object object) {

return view == (View) object;
}
}

这是我的 fragment 布局的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1"
android:id="@+id/linear_layout">

<com.viewpagerindicator.TitlePageIndicator
android:id="@+id/titles"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.09" />

<android.support.v4.view.ViewPager
android:id="@+id/free_time_day_pager"
android:layout_width="fill_parent"
android:layout_height="284dp" />

<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="52dp"
android:layout_weight="0.07" >

<Button
android:id="@+id/getScheduleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:onClick="getScheduleClicked"
android:text="@string/get_schedule" />

<Button
android:id="@+id/compareWithFriends"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/getScheduleButton"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/getScheduleButton"
android:onClick="compareWithFriendsClicked"
android:text="@string/compare_with_a_friend" />

</RelativeLayout>

</LinearLayout>

而且我想我应该简要地提一下,Schedule 包含 Day 对象,而 Day 对象包含要显示的类(class)列表。所有这些代码绝对有效,我已经使用了几个月。

最佳答案

嘿,我自己想出来了,我正在使用 ViewPager 和 FragmentPagerAdapter 一起制作画廊......!

我添加了

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub

FragmentManager manager = ((Fragment) object).getFragmentManager();
FragmentTransaction trans = manager.beginTransaction();
trans.remove((Fragment) object);
trans.commit();

super.destroyItem(container, position, object);
}

我的 FragmentPagerAdapter 删除 View 以避免开销。! :-)

关于android - fragment 中的 ViewPager 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12129299/

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