将一个片段替换为可滑动TabHost中的另一个片段

我有一个活动扩展片段活动和swipble选项卡视图。 在选项卡视图中,我使用了片段和Tabs寻呼机适配器根据选项卡位置调用片段。 现在我想要替换这些片段使用单击项目与另一个片段和后面按我想要的原始片段回来......但是当我点击items..it什么也不做......我应该做什么..pls帮助我...在此先感谢这里是我的代码 - 这是我的Homeactivity其中有选项卡@Override保护无效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();
         }
}
     }

我的XMl for HomeActivity:

     <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>

我的TabsPagerAdapter是:public class TabsPagerAdapter extends FragmentPagerAdapter {

    public TabsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int index) {

        switch (index) {
        case 0:
            return new Menu();
        case 1:

            return new Jobs();
        case 2:

            return new Applicant();
        case 3:

            return new Admin();
        }

        return null;
    }

    @Override
    public int getCount() {
        // get item count - equal to number of tabs
        return 4;
    }

}

我的Job片段是: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作业片段是:

    <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>
链接地址: http://www.djcxy.com/p/78915.html

上一篇: Replace one fragment to another fragment in the swipable TabHost

下一篇: ActionBar Tab that contains fragments crashes when reshown