包含tabhost和viewpager的片段不会与ListFragment重叠

时间:2015-02-24 12:58:32

标签: android android-fragments android-tabhost android-listfragment

MyListFragment.java this extends ListFragment and onListItemClick() I need to migrate to another two Fragments which should be displayed over this ListFragment.

我不知道如何解决这个错误我被困了3天帮助我。我没有得到任何可以记录和显示的错误,这已经变得不可能了。

public void onListItemClick(ListView l, View v, int position, long id) {

        Log.d("ListCLicked","Yes Clicked");

        //String clickedDetail = (String)l.getItemAtPosition(position);

            /*
             * The second fragment not yet loaded. 
             * Load MyDetailFragment by FragmentTransaction, and pass 
             * data from current fragment to second fragment via bundle.
             */
            MyDetailFragment myDetailFragment = new MyDetailFragment();
            Bundle bundle = new Bundle();
            //bundle.putString("KEY_DETAIL", clickedDetail);
            myDetailFragment.setArguments(bundle);
            Log.d("ListCLicked1","Yes Clicked");
            FragmentTransaction fragmentTransaction =
                    getChildFragmentManager().beginTransaction();
            Log.d("ListCLicked2","Yes Clicked");
            fragmentTransaction.add(R.id.tablistfragment, new MyDetailFragment());
            Log.d("ListCLicked3","Yes Clicked");
            fragmentTransaction.commit();


    }

MyDetailFragment.java is Fragment that an inflate to detailfragment.xml containg two fragments to be overlapped over the MyListFragment. MyListFragment is containg loaders and which log my phonebook contacts and form a list out of them on click of any List Item I need to get the fragment containing the details about the contact.Help Help Help Help help Help!!!!!!!!!

package com.example.crush_crm;

import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;





public class MyDetailFragment extends Fragment {


    ContactDetails con;

    public String number,callnumber,phn,emailtext;
    View view;

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

        Log.d("MyDetailFragment1","Here it is");
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        view=inflater.inflate(R.layout.detailfragment, null);

        Log.d("MyDetailFragment","Here it is");




        return view ;

    }   

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);


    }


}

MyDetailActivity extends FragmentActivity initialising the Tabhost and Viewpager.This activity doesn't get called when I execute even gives no error.I execute ContactHistory which inflates tablayout.xml , where I specify tools:context=".MyDetailACtivity" 



import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;







public class MyDetailActivity extends FragmentActivity implements OnTabChangeListener,OnPageChangeListener{

    public ContactHistory his;

    ContactDetails con;

    public String number,callnumber,phn,emailtext;


    public void OnCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);


        Log.d("hiiiiiiii i m in ","main");
        his=(ContactHistory)getSupportFragmentManager().findFragmentById(R.id.oppofragment);
        his.mTabHost1.setup();


        MyDetailActivity.AddTab(this, his.mTabHost1, his.mTabHost1.newTabSpec("History").setIndicator("History"));
        MyDetailActivity.AddTab(this, his.mTabHost1, his.mTabHost1.newTabSpec("Opportunity").setIndicator("Opportunity"));

        his.mAdapter = new TabsPagerAdapter(this.getSupportFragmentManager());
        // Fragments and ViewPager Initialization
        Log.d("Tabs Added1 ","Done");

       his.mViewPager1.setAdapter(his.mAdapter);
       his.mViewPager1.setOnPageChangeListener(MyDetailActivity.this);
       Log.d("Tabs Added2 ","Done");
       his.mTabHost1.setOnTabChangedListener(this);

    }


    private static void AddTab(MyDetailActivity activity, TabHost tabHost, TabHost.TabSpec tabSpec) {
        tabSpec.setContent(new MyTabFactory(activity));
        tabHost.addTab(tabSpec);
    }
    public void onTabChanged(String tag) {
        int pos = his.mTabHost1.getCurrentTab();
        his.mViewPager1.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 = his.mViewPager1.getCurrentItem();
        his.mTabHost1.setCurrentTab(pos);
    }

    @Override
        public void onPageSelected(int arg0) {
    }
    public void CallAct(View v)
    {
        Log.d("Hiiiiiiii","i m in main");
        con=(ContactDetails)getSupportFragmentManager().findFragmentById(R.id.contactfragment);

        number=con.phone.getText().toString();
        Log.d("Phone Number",number);
         callnumber="tel:"+number;
                Log.d("Phone Number",callnumber);

                Intent callButton=new Intent(Intent.ACTION_CALL,Uri.parse(callnumber));
                Log.d("Not Working","Intent Issue");
                startActivity(callButton);





        }

    public void MailAct(View v)
    {
        con=(ContactDetails)getSupportFragmentManager().findFragmentById(R.id.contactfragment);
        emailtext=con.textemail.getText().toString();

                Intent emailIntent=new Intent(Intent.ACTION_SEND);
                emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {emailtext});
                emailIntent.setType("message/rfc822");
                startActivity(emailIntent);



        }


}

ContactHistory.java ContactHistory just gives an inflate call to the tablayout and finds the ids for the tabhost and viewpagers.



import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;

public class ContactHistory extends Fragment {

    View view;
    public TabsPagerAdapter mAdapter;
    public ViewPager mViewPager;
    public TabHost mTabHost;

    public View onCreateView(LayoutInflater inflator,ViewGroup container,Bundle saveInstanceState)
    {
        Log.d("ContactHistory","History Oppurutnity");
        view=inflator.inflate(R.layout.tablayout,container,false);
        Log.d("onCreateView","Here I m OKKKKKKKKKKKKKKKKKKKKKKKKKK");
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

        mViewPager = (ViewPager)view.findViewById(R.id.viewpager);
        mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
        Log.d("I m not ohk","I go from here");
        return view;
    }

}

tablayout.xml this layout I used in another project works very fine without any problem even it gets displayed but here I need to overlap it over the Listfragment.
<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=".MainActivity" >

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

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

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0"
                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="top" />
        </LinearLayout>
    </TabHost>

</RelativeLayout>

detailfragment.xml contains the two fragments the ContactDetails fragment works very fine but the fragment ContactHistory with tabhost and ViewPager doesn't get displayed and I also don't get any error
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <fragment
        android:id="@+id/contactfragment"
        android:name="com.example.crush_crm.ContactDetails"
        android:layout_width="match_parent"
        android:layout_height="230dp"
         />

    <fragment
        android:id="@+id/oppofragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/contactfragment"
        class="com.example.crush_crm.ContactHistory" />

</RelativeLayout>

我没有收到任何错误,但是当我尝试显示它显示的联系人片段时,我的片段反对片段没有显示出来。

1 个答案:

答案 0 :(得分:0)

据我所知,可能是因为嵌套片段。

嵌套片段并不是解决问题的好方法。 您最好使用自定义视图:扩展Linear / RelativeLayout,并将您的内容放入其中。

更改

public class MyDetailFragment extends Fragment {

public class MyDetailView extends LinearLayout {

如果需要还有其他一些调整

然后您不需要使用片段管理器,您可以通过xml或编程方式添加MyDetailView。

希望有所帮助