CQM*_*CQM 3 tabs android tabactivity android-fragments
你好,我很难改变片段活动的视觉观点.我创建了所有选项卡但是onClick我不知道如何将活动传递到该空白区域.
http://i40.tinypic.com/22bwxx.png
所以这个结构是一个名为的简单活动Main,它只是将内容视图设置为一个maintabholder.xml文档,该文档包含片段视图,该视图连接到一个名为MainNav其内容视图包含底部所有选项卡的类.
maintabholder.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include layout="@layout/header" android:layout_alignParentTop="true" android:id="@+id/headerArea"></include>
<fragment
class="my.package.name.MainNav"
android:id="@+id/fragment_tabs"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
MainNav.java code snippet:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
mRoot = inflater.inflate(R.layout.fragment_tabs, null);
mTabHost = (TabHost) mRoot.findViewById(android.R.id.tabhost);
setupTabs();
return mRoot;
}
Run Code Online (Sandbox Code Playgroud)
和R.layout.fragment_tabs:
<?xml version="1.0" encoding="utf-8"?>
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#EFEFEF">
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:background="@drawable/tab_background"
android:gravity="center_horizontal"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<FrameLayout
android:id="@+id/tab_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@+id/tab_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@+id/tab_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@+id/tab_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@+id/tab_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
最后我要加载到片段中的Activity的内容(按下tab1按钮时)
public class HomeFragment extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
}
}
Run Code Online (Sandbox Code Playgroud)
这是为了帮助您获得事物的要点,但问题出现在MainNav.java中:
@Override
public void onTabChanged(String tabId) {
Log.d(TAG, "onTabChanged(): tabId=" + tabId);
if (TAB_HOME.equals(tabId)) {
updateTab(tabId, R.id.tab_1);
mCurrentTab = 0;
return;
}
}
Run Code Online (Sandbox Code Playgroud)
单击选项卡时,它将在onTabChanged活动中注册,并updateTab调用该函数:
private void updateTab(String tabId, int placeholder) {
FragmentManager fm = getFragmentManager();
if (fm.findFragmentByTag(tabId) == null) {
//fm.beginTransaction()
// .replace(placeholder, /*need fragment object here*/ , tabId)
// .commit();
//
}
}
Run Code Online (Sandbox Code Playgroud)
注释掉的replace方法正在寻找一个 (int, fragment, string) ,我猜它应该为我改变空白区域.
但我不明白从哪里得到片段对象!这个幻像对象需要以某种方式包含我HomeFragment的xml布局,并允许我与我为HomeFragment活动编码的其他所有内容进行交互.
我的第一个预感是我的HomeFragment活动只需要extend FragmentActivity活动.但这就是我撞墙的地方.
关于兼容包方法的文档很少,但TabActivity的文档说不再使用TabActivity了,所以我在这里.开发适用于Android 2.2+
洞察力赞赏.
你不能将活动放入碎片,这很简单.您必须将提供制表符内容的活动转换为片段.鉴于您不在这些活动中使用片段,这应该不是问题.
所以这里有两个规则要遵循:
我建议您在布局周围保留底部片段,以便处理tab-widget中的tab-switch.如果发生制表符切换,则将其报告给父Activity,它将相应的片段加载到制表符片段上方.为此,您必须修改布局,使其在选项卡片段上方和标题下方具有ViewGroup.您将使用tab-contents将片段动态加载到该ViewGroup中.
这是布局(maintabholder.xml):
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include layout="@layout/header" android:layout_alignParentTop="true" android:id="@+id/headerArea"></include>
<FrameLayout android:id="@+id/TabContent"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"/>
<fragment
class="my.package.name.MainNav"
android:id="@+id/fragment_tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
当底部片段中发生制表符切换时,您将其告知父活动:
...
(YourActivity)getActivity().switchToTab(tabId);
...
Run Code Online (Sandbox Code Playgroud)
在YourActivity中:
...
public void switchToTab(int tabId){
switch(tabId){
...
case 2:
getSupportFragmentManager().beginTransaction().replace(R.id.TabContent, TabContentFragment2.newInstance(), "someTag").commit().
break;
...
}
}
Run Code Online (Sandbox Code Playgroud)
这样的事情.
| 归档时间: |
|
| 查看次数: |
10554 次 |
| 最近记录: |