2015-10-06 102 views
1

內刷卡意見我已經創建了有一些關於它的只讀數據字段,並在底部有一個horizontal一套tabs的屏幕。FragmentPageAdaptor - 選項卡內容

每個標籤都有一個fragment表示tab content並且由數據對象的例如凸片之一的陣列備份是以前的地址,其中有可以爲0到許多。

我想tab內容fragmentswipeable這樣它將initally顯示最近的以前的地址和swipeleft將在未來拉離an array最初,繼續刷卡將在任一方向如果通過地址導航適當。

我一直在尋找FragmentPageAdapter,但我不想在title bar任何東西,我也想用同樣的fragment每個swipe作爲唯一改變的是顯示的數據,該layout將是相同的每次。

我仍然希望swipe行動,使得它看起來像頁刷卡從一個網頁到另一個,雖然。

這是此前進的最佳方式,並有更好的選擇?目前有5 tabs和那些tabs 3將有swipe-able內容,這將顯示更多的數據,但本身不會改變的觀點。

我以爲我會編輯這個並添加一些代碼,所以它可能是更容易看到我想要實現:

我有repsents被點擊和片段內部的垂直製表片段是一組水平標籤:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:layout_marginTop="5dp"> 

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:stretchColumns="1" 
     android:id="@+id/name_table"> 
    </TableLayout> 
</LinearLayout> 

<android.support.v4.app.FragmentTabHost 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/white" 
    android:layout_marginTop="5dp"> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:weightSum="20"> 
     <FrameLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="3" 
      android:orientation="horizontal"> 
      <TabWidget android:id="@android:id/tabs" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:visibility="gone"/> 
      <LinearLayout 
       android:id="@+id/tab_btn_container" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal"> 
       <Button 
        android:layout_height="wrap_content" 
        android:layout_width="match_parent" 
        android:layout_weight="1.0" 
        android:background="@color/lightest_grey" 
        android:id="@+id/temp_addr_tab_btn" 
        android:text="Temporary Addresses" 
        android:layout_marginRight="1dp" 
        android:textSize="7sp" 
        android:gravity="bottom|center" 
        android:drawableTop="@mipmap/ic_launcher" 
        android:paddingTop="4dp" 
        /> 
       <Button 
        android:layout_height="wrap_content" 
        android:layout_width="match_parent" 
        android:layout_weight="1.0" 
        android:background="@color/lightest_grey" 
        android:id="@+id/postal_addr_tab_btn" 
        android:text="Postal Addresses" 
        android:layout_marginRight="1dp" 
        android:textSize="7sp" 
        android:gravity="bottom|center" 
        android:drawableTop="@mipmap/ic_launcher" 
        android:paddingTop="4dp" 
        /> 
       <Button 
        android:layout_height="wrap_content" 
        android:layout_width="match_parent" 
        android:layout_weight="1.0" 
        android:background="@color/lightest_grey" 
        android:id="@+id/home_addr_tab_btn" 
        android:text="Home Addresses" 
        android:layout_marginRight="1dp" 
        android:textSize="7sp" 
        android:gravity="bottom|center" 
        android:drawableTop="@mipmap/ic_launcher" 
        android:paddingTop="4dp" 
        /> 
       <Button 
        android:layout_height="wrap_content" 
        android:layout_width="match_parent" 
        android:layout_weight="1.0" 
        android:background="@color/lightest_grey" 
        android:id="@+id/tel_fax_tab_btn" 
        android:text="Telephone/Fax" 
        android:layout_marginRight="1dp" 
        android:textSize="7sp" 
        android:gravity="bottom|center" 
        android:drawableTop="@mipmap/ic_launcher" 
        android:paddingTop="4dp" 
        /> 
       <Button 
        android:layout_height="wrap_content" 
        android:layout_width="match_parent" 
        android:layout_weight="1.0" 
        android:background="@color/lightest_grey" 
        android:id="@+id/email_other_tab_btn" 
        android:text="Email/Other" 
        android:textSize="7sp" 
        android:gravity="bottom|center" 
        android:drawableTop="@mipmap/ic_launcher" 
        android:paddingTop="4dp" 
        /> 
      </LinearLayout> 
     </FrameLayout> 
     <FrameLayout android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="17" 
      android:background="@color/white"/> 
    </LinearLayout> 
</android.support.v4.app.FragmentTabHost> 

所以,當用戶點擊第一個選項卡或頁面剛剛呈現,它會顯示在Android另一個片段:ID =「@機器人:ID/tabcontent」。

這個工作,它表明我的臨時地址片段:

<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.android.mockattempt1.TemporaryAddressesFragment"> 

<TextView android:layout_width="match_parent" android:layout_height="match_parent" 
    android:text="Temp Addresses Fragment that will show address information soon" /> 

目前的臨時地址片段只是一個硬編碼的文本的看法,但我想這是一個滑動式組臨時地址片段是用戶可以通過滑動查看地址。

我想將本教程http://www.101apps.co.za/articles/swipe-view-tutorial.html放到我的應用程序中,但我不確定ViewPager xml應該放在哪裏,它幾乎看起來應該是我父母片段中的tabcontent xml?

我不認爲這是通過,因爲在父權片段標籤內容設置是這樣的:

mTabHost = (FragmentTabHost)rootView.findViewById(android.R.id.tabhost); 
    mTabHost.setup(getActivity(), getChildFragmentManager(), android.R.id.tabcontent); 
    mTabHost.addTab(mTabHost.newTabSpec("temp_addresses").setIndicator("Temporary Addresses"), TemporaryAddressesFragment.class, null); 
    mTabHost.setCurrentTab(0); 

我幾乎需要更換與頁適配器的功能或以某種方式讓他們一起工作,但我不知道如何做到這一點。

回答

0

我已經成功地做到這一點,但不能完全肯定它的最好的方式,但我會詳細介紹我的嘗試,因爲它的工作原理:

片段有水平的標籤顯示第一個選項卡的內容,以前的地址(fragment_record.xml) :

<android.support.v4.app.FragmentTabHost 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/white" 
    android:layout_marginTop="5dp"> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:weightSum="20"> 
     <FrameLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="3" 
      android:orientation="horizontal"> 
      <TabWidget android:id="@android:id/tabs" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:visibility="gone"/> 
      <LinearLayout 
       android:id="@+id/tab_btn_container" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal"> 
       <Button 
        android:layout_height="wrap_content" 
        android:layout_width="match_parent" 
        android:layout_weight="1.0" 
        android:background="@color/lightest_grey" 
        android:id="@+id/temp_addr_tab_btn" 
        android:text="Temporary Addresses" 
        android:layout_marginRight="1dp" 
        android:textSize="7sp" 
        android:gravity="bottom|center" 
        android:drawableTop="@mipmap/ic_launcher" 
        android:paddingTop="4dp" 
        /> 
       <Button 
        android:layout_height="wrap_content" 
        android:layout_width="match_parent" 
        android:layout_weight="1.0" 
        android:background="@color/lightest_grey" 
        android:id="@+id/postal_addr_tab_btn" 
        android:text="Postal Addresses" 
        android:layout_marginRight="1dp" 
        android:textSize="7sp" 
        android:gravity="bottom|center" 
        android:drawableTop="@mipmap/ic_launcher" 
        android:paddingTop="4dp" 
        /> 
       <Button 
        android:layout_height="wrap_content" 
        android:layout_width="match_parent" 
        android:layout_weight="1.0" 
        android:background="@color/lightest_grey" 
        android:id="@+id/home_addr_tab_btn" 
        android:text="Home Addresses" 
        android:layout_marginRight="1dp" 
        android:textSize="7sp" 
        android:gravity="bottom|center" 
        android:drawableTop="@mipmap/ic_launcher" 
        android:paddingTop="4dp" 
        /> 
       <Button 
        android:layout_height="wrap_content" 
        android:layout_width="match_parent" 
        android:layout_weight="1.0" 
        android:background="@color/lightest_grey" 
        android:id="@+id/tel_fax_tab_btn" 
        android:text="Telephone/Fax" 
        android:layout_marginRight="1dp" 
        android:textSize="7sp" 
        android:gravity="bottom|center" 
        android:drawableTop="@mipmap/ic_launcher" 
        android:paddingTop="4dp" 
        /> 
       <Button 
        android:layout_height="wrap_content" 
        android:layout_width="match_parent" 
        android:layout_weight="1.0" 
        android:background="@color/lightest_grey" 
        android:id="@+id/email_other_tab_btn" 
        android:text="Email/Other" 
        android:textSize="7sp" 
        android:gravity="bottom|center" 
        android:drawableTop="@mipmap/ic_launcher" 
        android:paddingTop="4dp" 
        /> 
      </LinearLayout> 
     </FrameLayout> 
     <FrameLayout android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="17" 
      android:background="@color/white"/> 
    </LinearLayout> 
</android.support.v4.app.FragmentTabHost> 

設置的選項卡和下面詳述爲臨時地址片段的意圖(RecordFragment.java):

mTabHost = (FragmentTabHost)rootView.findViewById(android.R.id.tabhost); 
    mTabHost.setup(getActivity(), getChildFragmentManager(), android.R.id.tabcontent); 
    mTabHost.addTab(mTabHost.newTabSpec("temp_addresses").setIndicator("Temporary Addresses"), TemporaryAddressesFragment.class, null); 
    mTabHost.setCurrentTab(0); 

佔位符用於ViewPager(fragment_temporary_addresses.xml)片段:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 
    <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/temp_addr_pager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="1"> 

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

類,它表示上述片段,並且還包含內部類(TemporaryAddressesFragment.java):

private ViewPager temporaryAddressViewPager; 
private TemporaryAddressDetailFragmentStatePageAdapter temporaryAddressStateAdapter; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View rootView = (View)inflater.inflate(R.layout.fragment_temporary_addresses, container, false); 
    temporaryAddressStateAdapter = new TemporaryAddressDetailFragmentStatePageAdapter(getFragmentManager()); 
    temporaryAddressViewPager = (ViewPager)rootView.findViewById(R.id.temp_addr_pager); 
    temporaryAddressViewPager.setAdapter(temporaryAddressStateAdapter); 
    return rootView; 
} 

內類包含在此片段類是適配器,目前它只提供3個硬編碼的臨時地址細節片段:

public class TemporaryAddressDetailFragmentStatePageAdapter extends FragmentStatePagerAdapter { 

    public TemporaryAddressDetailFragmentStatePageAdapter(FragmentManager fragmentManager) { 
     super(fragmentManager); 
    } 

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

    @Override 
    public Fragment getItem(int position) { 
     Fragment fragment = null; 

     fragment = new TemporaryAddressDetailFragment(); 

     return fragment; 
    } 
} 

}

其代表實際內容\細節,我想有滑動式(fragment_temporary_address_detail.xml)另一個片段:

<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.android.oxygenmockattempt1.TemporaryAddressDetailFragment"> 

<!-- TODO: Update blank fragment layout --> 
<TextView android:layout_width="match_parent" android:layout_height="match_parent" 
    android:text="This is the temporary address detail fragment screen" /> 

還存在表示上述片段的類(TemporaryAddressDetailFragment.java)當前沒有做任何事情,因爲片段xml顯示硬編碼的字符串。

我也實現了我的實際活動類以下inerfaces(MainActivity.java):

TemporaryAddressesFragment.OnFragmentInteractionListener, TemporaryAddressDetailFragment.OnFragmentInteractionListener 

時會顯示在我的片段中的第一個選項卡我有一個是滑動式3個的地址窗格,但這樣做所以現在不管怎樣都不要滑動標籤。