Android fragments 将可切换TabHost中的一个片段替换为另一个片段

Android fragments 将可切换TabHost中的一个片段替换为另一个片段,android-fragments,android-tabhost,Android Fragments,Android Tabhost,我有一个扩展片段活动和可滑动选项卡视图的活动。在tab视图中,我使用fragment和Tabs寻呼机适配器根据选项卡位置调用片段。 现在,我想用另一个片段替换这些片段,然后按back键,我想还原原始片段。。。 但是当我点击这些项目时,它什么也不做。。。 我该怎么办请帮我。。。 提前谢谢 这是我的密码- 这是我的家庭活动,有标签 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(sav

我有一个扩展片段活动和可滑动选项卡视图的活动。在tab视图中,我使用fragment和Tabs寻呼机适配器根据选项卡位置调用片段。 现在,我想用另一个片段替换这些片段,然后按back键,我想还原原始片段。。。 但是当我点击这些项目时,它什么也不做。。。 我该怎么办请帮我。。。 提前谢谢 这是我的密码- 这是我的家庭活动,有标签

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

    setContentView(R.layout.home_screen);
    ressources = getResources();
    mViewPager = (ViewPager) findViewById(R.id.viewpager);
    // Tab Initialization
    initialiseTabHost();
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
    // Fragments and ViewPager Initialization

    mViewPager.setAdapter(mAdapter);
    mViewPager.setOnPageChangeListener(HomeScreen.this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
}
// Method to add a TabHost
private static void AddTab(HomeScreen activity, TabHost tabHost,
        TabHost.TabSpec tabSpec) {
    tabSpec.setContent(new MyTabFactory(activity));
    tabHost.addTab(tabSpec);
}
// Manages the Tab changes, synchronizing it with Pages
public void onTabChanged(String tag) {
    int pos = this.mTabHost.getCurrentTab();
    this.mViewPager.setCurrentItem(pos);
}

@Override
public void onPageScrollStateChanged(int arg0) {
}

// Manages the Page changes, synchronizing it with Tabs
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
    int pos = this.mViewPager.getCurrentItem();
    this.mTabHost.setCurrentTab(pos);
}

@Override
public void onPageSelected(int arg0) {
}
// Tabs Creation
private void initialiseTabHost() {
    mTabHost = (TabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup();

    // TODO Put here your Tabs
    HomeScreen.AddTab(
            this,
            this.mTabHost,
            this.mTabHost.newTabSpec(Button_tab).setIndicator("",
                    ressources.getDrawable(R.drawable.menu)));
    HomeScreen.AddTab(this, this.mTabHost,
            this.mTabHost.newTabSpec(Image_tab).setIndicator("",ressources.getDrawable(R.drawable.jobs)));
    HomeScreen.AddTab(this, this.mTabHost,
            this.mTabHost.newTabSpec(Text_tab).setIndicator("",ressources.getDrawable(R.drawable.people)));
    HomeScreen.AddTab(this, this.mTabHost,
            this.mTabHost.newTabSpec("line_tab").setIndicator("",ressources.getDrawable(R.drawable.calenders)));
    mTabHost.setOnTabChangedListener(this);
}
@Override
public void onBackPressed() {
    boolean isPopFragment = false;
    String currentTabTag = mTabHost.getCurrentTabTag();
    if (currentTabTag.equals(Button_tab)) {
        isPopFragment = ((BaseContainerFragment)getSupportFragmentManager().findFragmentByTag(Button_tab)).popFragment();
    } else if (currentTabTag.equals(Image_tab)) {
        isPopFragment = ((BaseContainerFragment)getSupportFragmentManager().findFragmentByTag(Image_tab)).popFragment();
    } else if (currentTabTag.equals(Text_tab)) {
        isPopFragment = ((BaseContainerFragment)getSupportFragmentManager().findFragmentByTag(Text_tab)).popFragment();
    } else if (currentTabTag.equals(line_tab)) {
        isPopFragment = ((BaseContainerFragment)getSupportFragmentManager().findFragmentByTag(line_tab)).popFragment();
    }
    if (!isPopFragment) {
        finish();
    }
}
My XMl for HomeActivity:

<RelativeLayout 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=".HomeScreen" >

    <RelativeLayout
        android:id="@+id/header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="10dp"
        android:orientation="horizontal" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:src="@drawable/krma" />
        <Button
            android:layout_width="30dp"
            android:layout_marginTop="3dp"
            android:layout_height="30dp"
            android:layout_marginRight="10dp"
           android:layout_alignParentRight="true"
            android:background="@drawable/listmenu" />
    </RelativeLayout>

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="vertical" >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:layout_weight="0"
                android:background="#FF7519"
                android:orientation="horizontal" />

            <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="wrap_content"
                android:layout_gravity="bottom" />
        </LinearLayout>
    </TabHost>
</RelativeLayout>
我的工作是:

public class Jobs extends  BaseContainerFragment implements  OnItemClickListener {
    ListView listView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_job, container,
                false);
        listView = (ListView) rootView.findViewById(R.id.list);
        SharedPreferences sharedpreferences = getActivity()
                .getSharedPreferences(LoginScreen.MyPREFERENCES,
                        Context.MODE_PRIVATE);
        String loggedInEmail = sharedpreferences.getString("nameKey", "");
        // String loggedInEmail="aditya.pratap@krmaa.com";
        RestClient client = new RestClient(
                "http://122.180.4.83:8080/krmaa/krmaa/joblist/" + loggedInEmail
                        + "/1234");
        client.AddHeader("GData-Version", "2");
        try {
            client.Execute(RequestMethod.GET);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String response = client.getResponse();
        Gson gson = new Gson();
        String convertedStr = response.replace("\\\"", "\"");

        String finalJson = convertedStr.substring(1, convertedStr.length() - 2);
        JobDTO[] jobDTOList = gson.fromJson(finalJson, JobDTO[].class);

        ItemAdapter adapter = new ItemAdapter(getActivity()
                .getApplicationContext(), jobDTOList);
        // Attach the adapter to a ListView
        ListView listView = (ListView) rootView.findViewById(R.id.list);
        listView.setAdapter(adapter);

        // return list of jobDTO and iterate in view page.

        return rootView;
    }

    public class JobDTOList {
        List<JobDTO> jobDTOList;

        public List<JobDTO> getJobDTOList() {
            return jobDTOList;
        }

        public void setJobDTOList(List<JobDTO> jobDTOList) {
            this.jobDTOList = jobDTOList;
        }
    }
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        // Here I want to switch from 1st Tab(Fragment1) to 2nd Tab(Fragment2)
        // How can I switch from 1st tab to 2nd tab here???
        // Below code is not worked for me.

        fragment2 fr = new fragment2();
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.job,fr);
        transaction.commit();
    }
}
<?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:id="@+id/job"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentTop="true"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="6"
            android:background="#A3A385"
            android:paddingBottom="10dp"
            android:paddingLeft="10dp"
            android:paddingTop="10dp"
            android:text="Jobs"
            android:textColor="#000000"
            android:textSize="10pt" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:background="#A3A385"
            android:text="Filter"`enter code here`
            android:onClick="frag"
            android:textSize="10pt" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >`enter code here`

        <ListView`enter code here`
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
             >
        </ListView>
    </LinearLayout>
</LinearLayout>
公共类作业扩展了BaseContainerFragment实现{
列表视图列表视图;
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
视图根视图=充气机。充气(R.layout.fragment\u作业,容器,
假);
listView=(listView)rootView.findViewById(R.id.list);
SharedReferences SharedReferences=getActivity()
.GetSharedReferences(LoginScreen.MyPREFERENCES,
上下文。模式(私人);
String loggedInEmail=SharedReferences.getString(“nameKey”,”);
//字符串loggedInEmail=“aditya。pratap@krmaa.com";
RestClient=newrestclient(
"http://122.180.4.83:8080/krmaa/krmaa/joblist/“+loggedInEmail
+ "/1234");
客户端.AddHeader(“GData版本”,“2”);
试一试{
client.Execute(RequestMethod.GET);
}捕获(例外e){
e、 printStackTrace();
}
字符串响应=client.getResponse();
Gson Gson=新的Gson();
字符串convertedStr=response.replace(“\\\”,“\”);
字符串finalJson=convertedStr.substring(1,convertedStr.length()-2);
JobDTO[]jobDTOList=gson.fromJson(finalJson,JobDTO[].class);
ItemAdapter=新的ItemAdapter(getActivity()
.getApplicationContext(),jobDTOList);
//将适配器连接到ListView
ListView ListView=(ListView)rootView.findViewById(R.id.list);
setAdapter(适配器);
//返回jobDTO列表并在视图页面中迭代。
返回rootView;
}
公共职业政客{
列出Jobdolist;
公共列表getJobDTOList(){
返回Jobdolist;
}
public void setJobDTOList(列出jobDTOList){
this.jobDTOList=jobDTOList;
}
}
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
//这里我想从第一个选项卡(片段1)切换到第二个选项卡(片段2)
//我如何在这里从第一个选项卡切换到第二个选项卡???
//下面的代码不适合我。
fragment2 fr=新的fragment2();
FragmentTransaction=getFragmentManager().beginTransaction();
交易。替换(R.id.job,fr);
commit();
}
}
我的作业片段xml是:

public class Jobs extends  BaseContainerFragment implements  OnItemClickListener {
    ListView listView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_job, container,
                false);
        listView = (ListView) rootView.findViewById(R.id.list);
        SharedPreferences sharedpreferences = getActivity()
                .getSharedPreferences(LoginScreen.MyPREFERENCES,
                        Context.MODE_PRIVATE);
        String loggedInEmail = sharedpreferences.getString("nameKey", "");
        // String loggedInEmail="aditya.pratap@krmaa.com";
        RestClient client = new RestClient(
                "http://122.180.4.83:8080/krmaa/krmaa/joblist/" + loggedInEmail
                        + "/1234");
        client.AddHeader("GData-Version", "2");
        try {
            client.Execute(RequestMethod.GET);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String response = client.getResponse();
        Gson gson = new Gson();
        String convertedStr = response.replace("\\\"", "\"");

        String finalJson = convertedStr.substring(1, convertedStr.length() - 2);
        JobDTO[] jobDTOList = gson.fromJson(finalJson, JobDTO[].class);

        ItemAdapter adapter = new ItemAdapter(getActivity()
                .getApplicationContext(), jobDTOList);
        // Attach the adapter to a ListView
        ListView listView = (ListView) rootView.findViewById(R.id.list);
        listView.setAdapter(adapter);

        // return list of jobDTO and iterate in view page.

        return rootView;
    }

    public class JobDTOList {
        List<JobDTO> jobDTOList;

        public List<JobDTO> getJobDTOList() {
            return jobDTOList;
        }

        public void setJobDTOList(List<JobDTO> jobDTOList) {
            this.jobDTOList = jobDTOList;
        }
    }
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        // Here I want to switch from 1st Tab(Fragment1) to 2nd Tab(Fragment2)
        // How can I switch from 1st tab to 2nd tab here???
        // Below code is not worked for me.

        fragment2 fr = new fragment2();
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.job,fr);
        transaction.commit();
    }
}
<?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:id="@+id/job"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentTop="true"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="6"
            android:background="#A3A385"
            android:paddingBottom="10dp"
            android:paddingLeft="10dp"
            android:paddingTop="10dp"
            android:text="Jobs"
            android:textColor="#000000"
            android:textSize="10pt" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:background="#A3A385"
            android:text="Filter"`enter code here`
            android:onClick="frag"
            android:textSize="10pt" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >`enter code here`

        <ListView`enter code here`
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
             >
        </ListView>
    </LinearLayout>
</LinearLayout>

`在这里输入代码`