2

所以我有一個應用程序與3 actionbar.Tabs之間切換3片段。在其中兩個片段中,我需要一個可在6個ListView之間切換的ViewPager。問題是,我試圖讓這個工作的片段顯示頁面,只顯示黑色......總是...我已經證實我可以顯示其他東西(我試圖讓我的SherlockFragment成爲一個SherlockListFragment,我可以顯示數據)。所以這個問題完全侷限於我的適配器和我的ViewPager以及將它們放在適當位置的代碼。哦,我還應該提到我正在使用ActionBarSherlock,並(嘗試)ViewPagerIndicator。我已正確導入(最終)。一個片段內的ViewPager不工作

因此,這裏是我的片段代碼:

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; 
    } 
} 

和這裏的,片斷的佈局我的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> 

我想我應該簡單地提一下這個附表,它包含日期對象,它們包含課程列表,它們是t o顯示。所有這些代碼絕對有效,我已經使用了好幾個月。

回答

5

嘿我想通了我的自我我使用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去除以避免開銷。! :-)

相關問題