从TabLayout标签的Andr​​oid的Eclipse启动片段 [英] Android Eclipse Start Fragment from TabLayout Tab

查看:270
本文介绍了从TabLayout标签的Andr​​oid的Eclipse启动片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了一个TabLayout其中有一些标签和放大器;一个选项卡中有一张桌子和放大器;当用户点击上表中的行我需要摆脱的标签布局,除了工具栏和放大器;开辟新的片段。

我已经尝试了几种不同的方法,但似乎没有任何工作,如果任何人有任何想法,这将将大大AP preciated!

感谢您!
干杯!

这是activity_main.xml中

 < RelativeLayout的
机器人:ID =@ + ID / main_layout
的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
工具:上下文=MainActivity。>< android.support.v7.widget.Toolbar
    机器人:ID =@ + ID /工具栏
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentTop =真
    机器人:ATTR / colorPrimary背景=
    机器人:海拔=6DP
    安卓了minHeight =?ATTR / actionBarSize
    机器人:主题=@风格/ ThemeOverlay.AppCompat.Dark.ActionBar
    应用:popupTheme =@风格/ ThemeOverlay.AppCompat.Light/>< android.support.design.widget.TabLayout
    机器人:ID =@ + ID / tab_layout
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_below =@ + ID /工具栏
    机器人:ATTR / colorPrimary背景=
    机器人:海拔=6DP
    安卓了minHeight =?ATTR / actionBarSize
    机器人:主题=@风格/ ThemeOverlay.AppCompat.Dark.ActionBar/>< android.support.v4.view.ViewPager
    机器人:ID =@ + ID /寻呼机
    机器人:layout_width =match_parent
    机器人:layout_height =FILL_PARENT
    机器人:layout_below =@ ID / tab_layout/>< / RelativeLayout的>

&安培;这是MainActivity.java

 公共类MainActivity扩展AppCompatActivity {@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    工具条工具栏=(栏)findViewById(R.id.toolbar);
    setSupportActionBar(工具栏);    TabLayout tabLayout =(TabLayout)findViewById(R.id.tab_layout);
    tabLayout.addTab(tabLayout.newTab()的setText(TAB1)。);
    tabLayout.addTab(tabLayout.newTab()的setText(TAB2)。);
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);    最后ViewPager viewPager =(ViewPager)findViewById(R.id.pager);
    最后PagerAdapter适配器=新PagerAdapter
            (getSupportFragmentManager(),tabLayout.getTabCount());
    viewPager.setAdapter(适配器);
    viewPager.addOnPageChangeListener(新TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(新TabLayout.OnTabSelectedListener(){
        @覆盖
        公共无效onTabSelected(TabLayout.Tab标签){
            viewPager.setCurrentItem(tab.getPosition());
        }        @覆盖
        公共无效onTabUnselected(TabLayout.Tab标签){        }        @覆盖
        公共无效onTabReselected(TabLayout.Tab标签){        }
    });}@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    //getMenuInflater().inflate(R.menu.menu_main,菜单);
    返回true;
}@覆盖
公共布尔onOptionsItemSelected(菜单项项){
    INT ID = item.getItemId();
    如果(ID == R.id.action_settings){
        返回true;
    }    返回super.onOptionsItemSelected(项目);
}}

&安培;这是tab1.xml

 <的FrameLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:paddingBottom会=@扪/ activity_vertical_margin
机器人:paddingLeft =@扪/ activity_horizo​​ntal_margin
机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
机器人:paddingTop =@扪/ activity_vertical_margin
机器人:背景=#F0F0F0><滚动型
    机器人:ID =@ + ID /滚动视图
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_centerHorizo​​ntal =真
    机器人:layout_centerVertical =真
    机器人:比重=中心>
< TableLayout
    机器人:ID =@ + ID /表
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:比重=中心>
< / TableLayout>

 < RelativeLayout的
    机器人:ID =@ + ID / loadingPanel
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:比重=中心><进度
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:不确定=真/>
< / RelativeLayout的>< /&的FrameLayout GT;

&安培;这是Tab1.java

 公共类TopStories扩展片段{很多code的放在这里............    公共无效CREATETABLE(){    TableLayout表=(TableLayout)getActivity()findViewById(R.id.table)。
    table.setBackgroundColor(Color.rgb(240,240,240));
    table.setStretchAllColumns(真);
    table.setShrinkAllColumns(真);    table.removeAllViews();连续的TableRow =新的TableRow(getActivity());
        排.setGravity(Gravity.CENTER);
        行.setPadding(0,10,0,10);
        排.setBackgroundColor(Color.WHITE);
        排.setClickable(真);
        TableRow.LayoutParams PARAMS =新TableRow.LayoutParams();        排.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(视图v){                这是我需要启动新的片段(NewFragment.Java)。            }
        });    }}@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
    返回inflater.inflate(R.layout.fragment_topstories,集装箱,FALSE);
}}

NewFragment.java

 公共类NewFragment扩展片段{公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
    返回inflater.inflate(R.layout.newfragment,集装箱,FALSE);
}}

newfragment.xml

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直>< RelativeLayout的
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_weight =1.04>    < ImageView的
        机器人:ID =@ + ID /图像
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_alignParentTop =真
        机器人:layout_marginLeft =17dp
        机器人:layout_marginTop =18dp/>    <的TextView
        机器人:ID =@ + ID /标题
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_below =@ + ID / imageView1
        机器人:layout_centerHorizo​​ntal =真
        机器人:layout_marginTop =37dp
        机器人:文字=大文本
        机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceLarge?    <的TextView
        机器人:ID =@ + ID /文
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_below =@ + ID / imageView1
        机器人:layout_marginTop =87dp
        机器人:文字=中文字
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>< / RelativeLayout的>< / LinearLayout中>


解决方案

好吧,所以我认为我想通了一个非常便宜的&安培;面露办法做到这一点和放大器;可能不是非常的应用健康,但它的作品完美!

非常感谢你的帮助布赖恩!鸭preciate吧!也许你可以为你的应用程序的backstack问题,做到这一点为好,不知道它是否会奏效,或者你需要正确的事情做虽然。

干杯!

增加了的FrameLayout我main_activity.xml

 < RelativeLayout的
机器人:ID =@ + ID / main_layout
的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
工具:上下文=MainActivity。>< android.support.v7.widget.Toolbar
    机器人:ID =@ + ID /工具栏
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentTop =真
    机器人:ATTR / colorPrimary背景=
    机器人:海拔=6DP
    安卓了minHeight =?ATTR / actionBarSize
    机器人:主题=@风格/ ThemeOverlay.AppCompat.Dark.ActionBar
    应用:popupTheme =@风格/ ThemeOverlay.AppCompat.Light/>< android.support.design.widget.TabLayout
    机器人:ID =@ + ID / tab_layout
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_below =@ + ID /工具栏
    机器人:ATTR / colorPrimary背景=
    机器人:海拔=6DP
    安卓了minHeight =?ATTR / actionBarSize
    机器人:主题=@风格/ ThemeOverlay.AppCompat.Dark.ActionBar/>< android.support.v4.view.ViewPager
    机器人:ID =@ + ID /寻呼机
    机器人:layout_width =match_parent
    机器人:layout_height =FILL_PARENT
    机器人:layout_below =@ ID / tab_layout/><的FrameLayout
    机器人:ID =@ + ID / flContent
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>
< /&的FrameLayout GT;< / RelativeLayout的>

改变了我的newfragment.xml这种

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:ID =@ + ID / fullStoryLinearLayout
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直>< RelativeLayout的
    机器人:ID =@ + ID / fullStoryRelativeLayout
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_weight =1.04>    < ImageView的
        机器人:ID =@ + ID /图像
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_alignParentTop =真
        机器人:layout_marginLeft =17dp
        机器人:layout_marginTop =80dp/>    <的TextView
        机器人:ID =@ + ID / fullStorytitle
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_below =@ + ID / imageView1
        机器人:layout_centerHorizo​​ntal =真
        机器人:layout_marginTop =80dp
        机器人:文字=大文本
        机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceLarge?    <的TextView
        机器人:ID =@ + ID / fullStory
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_below =@ + ID /图像
        机器人:layout_marginTop =50dp
        机器人:文字=中文字
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>< / RelativeLayout的>< / LinearLayout中>

&安培;已将此添加到表行的onclick事件

  row.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(视图v){                      getActivity()findViewById(R.id.tab_layout).setVisibility(View.GONE)。
                   getActivity()findViewById(R.id.pager).setVisibility(View.GONE)。
                   ((AppCompatActivity)getActivity())getSupportActionBar()setDisplayHomeAsUpEnabled(真)。
                  ((AppCompatActivity)getActivity())getSupportActionBar()的setTitle(新片段)。;                片段片段= NULL;                类fragmentClass = NewFragment.class;
                尝试{
                    片段=(片段)fragmentClass.newInstance();
                }赶上(例外五){
                    e.printStackTrace();
                }                FragmentManager fragmentManager = getActivity()getSupportFragmentManager()。
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.flContent,片段);
                fragmentTransaction.commit();            }
        });

&安培;回到从NewFragment主要TabLayout,我说这在MainActivity.java

  @覆盖
公共布尔onOptionsItemSelected(菜单项项){
    INT ID = item.getItemId();    开关(ID){
    案例android.R.id.home:
        onBack pressed();
       返回true;
   默认:
    返回super.onOptionsItemSelected(项目);
    }
}
@覆盖
公共布尔的onkeydown(INT键code,KeyEvent的事件){
    开关(键code){
    案例KeyEvent.KEY code_BACK:
        如果(getSupportActionBar()的getTitle()!=主要活动){
            onBack pressed();
        }
        其他{
            System.exit(0);
        }
        返回true;
    }
    返回super.onKeyDown(键code,事件);
}公共无效onBack pressed(){
    getSupportActionBar()setDisplayHomeAsUpEnabled(假)。
    getSupportActionBar()的setTitle(主要活动);
    findViewById(R.id.fullStoryLinearLayout).setVisibility(View.GONE);
    findViewById(R.id.tab_layout).setVisibility(View.VISIBLE);
    findViewById(R.id.pager).setVisibility(View.VISIBLE);

}

I have implemented a TabLayout which has a few tabs & one of the tabs has a table & when a user clicks on a row in the table I need it to get rid of the tab layout, except for the toolbar & open up a new Fragment.

I've tried a few different ways but nothing seems to work, if anyone has any ideas, it'll will be much appreciated!

Thank you! Cheers!

This is the activity_main.xml

<RelativeLayout
android:id="@+id/main_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="?attr/colorPrimary"
    android:elevation="6dp"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/toolbar"
    android:background="?attr/colorPrimary"
    android:elevation="6dp"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/tab_layout"/>

</RelativeLayout>

& this is the MainActivity.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.addTab(tabLayout.newTab().setText("Tab1"));
    tabLayout.addTab(tabLayout.newTab().setText("Tab2"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    final PagerAdapter adapter = new PagerAdapter
            (getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    //getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

& this is tab1.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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" 
android:background="#F0F0F0" >

<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:gravity="center" >
<TableLayout
    android:id="@+id/table"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center" >
</TableLayout>

<RelativeLayout
    android:id="@+id/loadingPanel"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center" >

<ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:indeterminate="true" />
</RelativeLayout>

</FrameLayout>

& this is Tab1.java

public class TopStories extends Fragment {

A lot of code goes here............

    public void createTable() {

    TableLayout table = (TableLayout)          getActivity().findViewById(R.id.table);
    table.setBackgroundColor(Color.rgb(240, 240, 240));
    table.setStretchAllColumns(true);
    table.setShrinkAllColumns(true);

    table.removeAllViews();

TableRow row = new TableRow(getActivity());
        row .setGravity(Gravity.CENTER);
        row .setPadding(0, 10, 0, 10);
        row .setBackgroundColor(Color.WHITE);
        row .setClickable(true);
        TableRow.LayoutParams params = new TableRow.LayoutParams();

        row .setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                This is where I need to start up the new fragment (NewFragment.Java).

            }


        });

    }

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,        Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_topstories, container, false);        
}

}

NewFragment.java

public class NewFragment extends Fragment {

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.newfragment, container, false);        
}

}

newfragment.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="vertical" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1.04" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="17dp"
        android:layout_marginTop="18dp"/>

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="37dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView1"
        android:layout_marginTop="87dp"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

</LinearLayout>

解决方案

Alright so I think that I figured out a really cheap & a cheeky way to do this & probably not very healthy for the app but it works perfect!

Thank you very much for the help Brian! Appreciate it! Maybe you can do this for the backstack problem of your app as well, don't know if it will work or the right thing that you need to do though.

Cheers!

Added a FrameLayout to my main_activity.xml

<RelativeLayout
android:id="@+id/main_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="?attr/colorPrimary"
    android:elevation="6dp"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/toolbar"
    android:background="?attr/colorPrimary"
    android:elevation="6dp"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/tab_layout"/>

<FrameLayout
    android:id="@+id/flContent"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</FrameLayout>

</RelativeLayout>

Changed my newfragment.xml to this

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fullStoryLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<RelativeLayout
    android:id="@+id/fullStoryRelativeLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1.04" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="17dp"
        android:layout_marginTop="80dp"/>

    <TextView
        android:id="@+id/fullStorytitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="80dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/fullStory"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/image"
        android:layout_marginTop="50dp"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

</LinearLayout>

& added this to the onClick event of the table row

row.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                      getActivity().findViewById(R.id.tab_layout).setVisibility(View.GONE);
                   getActivity().findViewById(R.id.pager).setVisibility(View.GONE);


                   ((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                  ((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("New Fragment");

                Fragment fragment = null;

                Class fragmentClass = NewFragment.class;
                try {
                    fragment = (Fragment) fragmentClass.newInstance();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                FragmentManager fragmentManager =   getActivity().getSupportFragmentManager();
                FragmentTransaction fragmentTransaction =   fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.flContent, fragment);
                fragmentTransaction.commit();

            }


        });

& to go back to the main TabLayout from the NewFragment, I added this at MainActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    switch (id) {
    case android.R.id.home:
        onBackPressed();
       return true;
   default:
    return super.onOptionsItemSelected(item);
    }
}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch(keyCode){
    case KeyEvent.KEYCODE_BACK:
        if (getSupportActionBar().getTitle() != "Main Activity"){
            onBackPressed();
        }
        else {
            System.exit(0);
        }
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

public void onBackPressed(){
    getSupportActionBar().setDisplayHomeAsUpEnabled(false);
    getSupportActionBar().setTitle("Main Activity");
    findViewById(R.id.fullStoryLinearLayout).setVisibility(View.GONE);
    findViewById(R.id.tab_layout).setVisibility(View.VISIBLE);
    findViewById(R.id.pager).setVisibility(View.VISIBLE);

}

这篇关于从TabLayout标签的Andr​​oid的Eclipse启动片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆