Replace one fragment to another fragment in the swipable TabHost

I have an activity extending fragment activity and swipable tab view. In tab view i have used fragment and Tabs pager adapter to call fragments according to the tab position. Now i want to replace these fragment using on click items with another fragment and on back press i want the original fragment back... But when i am clicking on the items..it do nothing... What should i do..pls help me ... Thanks in advance here is my code- This is my Homeactivity which has tab @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
        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>

My TabsPagerAdapter is : 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;
    }

}

my Job fragment is : 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();
    }


}

My xml for Job fragment is :

    <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/78916.html

上一篇: getActionBar方法返回null

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