2012-11-21 2 views
0

내 안드로이드 프로젝트에 SWIPE 탭이있는 응용 프로그램을 구현하려고합니다. 두 개의 탭 (tab1 및 tab2)이 있다고 가정 해 보겠습니다. 그리고이 탭에 다른 내용을 표시하려고합니다. (사실 그 모든 것이 내가 더 이상 아무것도하고 싶지 않습니다.)스 와이프 탭 - listview + 어댑터가있는 두 개의 탭

Tab1에는 목록이 포함되어 있으며 Tab2에는 "another"목록과 일부 텍스트가 포함되어 있습니다. 두 개의 탭에 두 개의 "활동"을 사용할 수 있습니까? (그게 내가 지금하고있는 일이야)

목록 내용을 설정하는 데 사용자 지정 어댑터를 사용합니다.

final ListView lv = (ListView)findViewById(R.id.tab1_list); 
lv.setAdapter(new CustomListAdapter(this, tab1Items)); 

하지만 탭 콘텐츠를 올바르게 표시 할 수 없습니다. 그들은 전혀 표시되지 않거나 동일한 내용을 표시합니다.

나에게 좋은 아이디어가 있거나 작은 코드 예제가 있습니까? 탭이있는 appbar와 어댑터 목록을 사용하는 방법 (어댑터 사용)은 누구입니까?

어떤 도움을 주셔서 감사합니다.

답변

2

그것을 경우 단지 정의

<?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"> 
     <TabHost 
      android:id="@android:id/tabhost" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      > 
      <LinearLayout 
       android:orientation="vertical" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       > 
       <TabWidget 
        android:id="@android:id/tabs" 
        android:orientation="horizontal" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_weight="0" 
        /> 

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

       <android.support.v4.view.ViewPager 
        android:id="@+id/viewpager" 
        android:layout_width="fill_parent" 
        android:layout_height="0dp" 
        android:layout_weight="1" 
        /> 
      </LinearLayout> 
     </TabHost> 
    </LinearLayout> 

나를 위해 PagerAdapter 투표 .... 당신이 "A 슬쩍 탭"이 당신을 도울 것입니다

희망을 원하는 예입니다

package com.andy.fragments.viewpager; 

import java.util.List; 

import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 


public class PagerAdapter extends FragmentPagerAdapter { 

    private List<Fragment> fragments; 

    public PagerAdapter(FragmentManager fm, List<Fragment> fragments) { 
     super(fm); 
     this.fragments = fragments; 
    } 

    @Override 
    public Fragment getItem(int position) { 
     return this.fragments.get(position); 
    } 


    @Override 
    public int getCount() { 
     return this.fragments.size(); 
    } 
} 

탭 FragmentActivity를 정의

package com.andy.fragments.tabs; 

import java.util.HashMap; 
import java.util.List; 
import java.util.Vector; 

import android.content.Context; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.view.ViewPager; 
import android.view.View; 
import android.widget.TabHost; 
import android.widget.TabHost.TabContentFactory; 

import com.andy.R; 
import com.andy.fragments.viewpager.PagerAdapter; 


public class TabsViewPagerFragmentActivity extends FragmentActivity implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener { 

    private TabHost mTabHost; 
    private ViewPager mViewPager; 
    private HashMap<String, TabInfo> mapTabInfo = new HashMap<String, TabsViewPagerFragmentActivity.TabInfo>(); 
    private PagerAdapter mPagerAdapter; 

    private class TabInfo { 
     private String tag; 
     private Class<?> clss; 
     private Bundle args; 
     private Fragment fragment; 
     TabInfo(String tag, Class<?> clazz, Bundle args) { 
      this.tag = tag; 
      this.clss = clazz; 
      this.args = args; 
     } 

    } 

    class TabFactory implements TabContentFactory { 

     private final Context mContext; 

     public TabFactory(Context context) { 
      mContext = context; 
     } 


     public View createTabContent(String tag) { 
      View v = new View(mContext); 
      v.setMinimumWidth(0); 
      v.setMinimumHeight(0); 
      return v; 
     } 

    } 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.tabs_viewpager_layout); 

     this.initialiseTabHost(savedInstanceState); 
     if (savedInstanceState != null) { 
      mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); 
     } 

     this.intialiseViewPager(); 
    } 


    protected void onSaveInstanceState(Bundle outState) { 
     outState.putString("tab", mTabHost.getCurrentTabTag()); 
     super.onSaveInstanceState(outState); 
    } 


    private void intialiseViewPager() { 

     List<Fragment> fragments = new Vector<Fragment>(); 
     fragments.add(Fragment.instantiate(this, Tab1Fragment.class.getName())); 
     fragments.add(Fragment.instantiate(this, Tab2Fragment.class.getName())); 
     fragments.add(Fragment.instantiate(this, Tab3Fragment.class.getName())); 
     this.mPagerAdapter = new PagerAdapter(super.getSupportFragmentManager(), fragments); 

     this.mViewPager = (ViewPager)super.findViewById(R.id.viewpager); 
     this.mViewPager.setAdapter(this.mPagerAdapter); 
     this.mViewPager.setOnPageChangeListener(this); 
    } 


    private void initialiseTabHost(Bundle args) { 
     mTabHost = (TabHost)findViewById(android.R.id.tabhost); 
     mTabHost.setup(); 
     TabInfo tabInfo = null; 
     TabsViewPagerFragmentActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab1").setIndicator("Tab 1"), (tabInfo = new TabInfo("Tab1", Tab1Fragment.class, args))); 
     this.mapTabInfo.put(tabInfo.tag, tabInfo); 
     TabsViewPagerFragmentActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab2").setIndicator("Tab 2"), (tabInfo = new TabInfo("Tab2", Tab2Fragment.class, args))); 
     this.mapTabInfo.put(tabInfo.tag, tabInfo); 
     TabsViewPagerFragmentActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab3").setIndicator("Tab 3"), (tabInfo = new TabInfo("Tab3", Tab3Fragment.class, args))); 
     this.mapTabInfo.put(tabInfo.tag, tabInfo); 

     mTabHost.setOnTabChangedListener(this); 
    } 


    private static void AddTab(TabsViewPagerFragmentActivity activity, TabHost tabHost, TabHost.TabSpec tabSpec, TabInfo tabInfo) { 

     tabSpec.setContent(activity.new TabFactory(activity)); 
     tabHost.addTab(tabSpec); 
    } 


    public void onTabChanged(String tag) { 

     int pos = this.mTabHost.getCurrentTab(); 
     this.mViewPager.setCurrentItem(pos); 
    } 


    @Override 
    public void onPageScrolled(int position, float positionOffset, 
      int positionOffsetPixels) { 


    } 


    @Override 
    public void onPageSelected(int position) { 

     this.mTabHost.setCurrentTab(position); 
    } 


    @Override 
    public void onPageScrollStateChanged(int state) { 


    } 
} 
1

왜 레이아웃 접근 방식을 사용하지 않습니까? 이 같은 탭 호스트를 넣어하는 레이아웃 만들기 :

<?xml version="1.0" encoding="utf-8"?> 

<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:android1="http://schemas.android.com/apk/res/android" 
android1:id="@android:id/tabhost" 
android1:layout_width="match_parent" 
android1:layout_height="match_parent" > 

<TabWidget 
    android1:id="@android:id/tabs" 
    android1:layout_width="match_parent" 
    android1:layout_height="wrap_content" > 
</TabWidget> 

<FrameLayout 
    android1:id="@android:id/tabcontent" 
    android1:layout_width="match_parent" 
    android1:layout_height="match_parent" > 

    <LinearLayout 
     android1:id="@+id/tab1" 
     android1:layout_width="match_parent" 
     android1:layout_height="match_parent" > 
    </LinearLayout> 

    <LinearLayout 
     android1:id="@+id/tab2" 
     android1:layout_width="match_parent" 
     android1:layout_height="match_parent" > 
    </LinearLayout> 

    <LinearLayout 
     android1:id="@+id/tab3" 
     android1:layout_width="match_parent" 
     android1:layout_height="match_parent" > 
    </LinearLayout> 
</FrameLayout> 

그래서있는 LinearLayout에 ID TAB1 당신은 (등등과) 첫 번째 목록을 추가 할 수 있습니다.

: 여기
관련 문제