tabhost_layout.xml

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:orientation="horizontal" >


    <TabHost

        android:id="@+id/tabhost1"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:layout_weight="1" >


        <LinearLayout

            android:layout_width="match_parent"

            android:layout_height="match_parent"

            android:orientation="vertical" >


            <TabWidget

                android:id="@android:id/tabs"

                android:layout_width="match_parent"

                android:layout_height="wrap_content" >

            </TabWidget>


            <FrameLayout

                android:id="@android:id/tabcontent"

                android:layout_width="match_parent"

                android:layout_height="match_parent" >

            </FrameLayout>

        </LinearLayout>

    </TabHost>


</LinearLayout>

=======================================

tab_content_layout.xml

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="horizontal" >


    <ListView

        android:id="@+id/item_list"

        android:layout_width="match_parent"

        android:layout_height="match_parent" />


</LinearLayout>

=======================================

TestTabHost.java

private Context mContext;
private View mView=null;
private TabHost mTab;
private LayoutInflater mInflater = null;
private int mDefaultItemId;
private int mSelectedItemId;

private OnItemClickListener mItemClickListener = new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ArrayList<MyTabItem> tabItems = getTabItems();
int tabIndex = mTab.getCurrentTab();
int itemId = tabItems.get(tabIndex).getItem(position).getId();
mSelectedItemId = itemId;
}
};

private void initLayout(){

TabHost.TabSpec spec = null;

mView = mInflater.inflate(R.layout.tabhost_layout, null);

mTab = (TabHost)mView.findViewById(R.id.tabhost1);

mTab.setup();

ArrayList<MyTabItem> tabItems = getTabItems();

for(final MyTabItem tabItem : tabItems){

spec = mTab.newTabSpec(tabItem.getTag());

spec.setIndicator(tabItem.getTitle());

spec.setContent(new TabHost.TabContentFactory() {

@Override

public View createTabContent(String arg0) {

View v = mInflater.inflate(R.layout.tab_content_layout, null);

ListView lv = (ListView)v.findViewById(R.id.item_list);

String[] itemArray = new String[tabItem.AllItems().length];

int seletedIndex = -1;

for(int i=0; i<tabItem.AllItems().length; i++){

itemArray[i] = tabItem.getItem(i).getName();

if(mDefaultItemId == tabItem.getItem(i).getId()){

seletedIndex = i;

mSelectedItemId = mDefaultItemId;

}

}

lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

lv.setAdapter(new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_single_choice, itemArray));

lv.setOnItemClickListener(mItemClickListener);

if(seletedIndex >= 0){

lv.setItemChecked(seletedIndex, true);

}

return v;

}

});

mTab.addTab(spec);

}

mTab.setCurrentTab(0);

mTab.setOnTabChangedListener(new TabHost.OnTabChangeListener(){

@Override

public void onTabChanged(String tag) {

ListView lv = (ListView)mTab.getCurrentView().findViewById(R.id.item_list);

if(lv != null){

int tabIndex = mTab.getCurrentTab();

ArrayList<MyTabItem> tabItems = getTabItems();

for(int i=0;i<lv.getChildCount(); i++){

int itemId = tabItems.get(tabIndex).getItem(i).getId();

if(mSelectedItemId != itemId){

lv.setItemChecked(i, false);

}

}

}

}

});

}



저작자 표시 비영리 변경 금지
신고
크리에이티브 커먼즈 라이선스
Creative Commons License

'programmer > android' 카테고리의 다른 글

media scanner  (0) 2013.07.16
menu icon invalidate(enable/disable)  (0) 2013.07.11
Using TabHost  (0) 2013.07.11
AlertDialog Message with List(Custom View)  (0) 2013.07.08
Using context menu on Linearlayout  (0) 2013.07.08
touch block  (0) 2013.07.08
Posted by ocsoosoo

댓글을 달아 주세요


티스토리 툴바