0

我有一個應用程序與2個選項卡與幾個控件Editext/Textviews/ListView和 我最近從Tabhost更改爲Fragments/ViewPager。 當我啓動應用程序時,兩個選項卡的內容將同時繪製到屏幕上。 如果我選擇另一個選項卡,則屬於舊選項卡的控件不會被刪除, 會導致混亂的屏幕。 如果我輸入文本,EditText的提示也不會消失。 我也嘗試了一個簡化的佈局(少控制)我成功。Android佈局搞砸了(使用Fragments/Viewpager)

在我使用片段之前,這種東西沒有發生。 任何想法我失蹤?

---------------- activity_main.xml中----------------

android:layout_width="match_parent" 
android:layout_height="match_parent"> 
<android.support.v4.view.ViewPager 

    android:id="@+id/pager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity" /> 
<android.support.v4.app.fragment 
    android:id="@+id/DetailFragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    class="com.app2.tabbedviews.FragmentDetailTab" /> 

<android.support.v4.app.fragment 
    android:id="@+id/ListFragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    class="com.app2.tabbedviews.FragmentListTab" />  

---- -------- listfragment.xml -------------------------

<LinearLayout 
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:orientation="vertical" 
tools:context="FragmentListTab" 
> 
      <ListView 
...... 
      </ListView> 

------- ----- detailfragment.xml -------------------------

<LinearLayout 
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:orientation="vertical" 
tools:context="FragmentDetailTab" 
> 
..... (some Editexts/textviews) 

---------- - java -------------------------

import android.os.Bundle; 
import android.provider.Settings.Secure; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.support.v7.app.ActionBar; 
import android.support.v7.app.ActionBar.Tab; 
import android.text.Editable; 
import android.text.InputFilter; 
import android.text.InputType; 
import android.text.TextWatcher; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.EditText; 
import android.widget.ListView; 
import android.widget.TableLayout; 
import android.widget.TextView; 

public class MainActivity extends android.support.v7.app.ActionBarActivity implements ActionBar.TabListener { 
............. 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     lw = new LogWrapper(this); 
     setContentView(R.layout.activity_main); 
     final ActionBar actionBar = getSupportActionBar();   
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
     mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 
     // Set up the ViewPager with the sections adapter. 
     mViewPager = (ViewPager) findViewById(R.id.pager); 
     mViewPager.setAdapter(mSectionsPagerAdapter); 
     mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { 
      @Override 
      public void onPageSelected(int position) { 
       Log.d(TAG, "setOnPageChangeListener, onPageSelected: ".concat(Integer.toString(position))); 
       actionBar.setSelectedNavigationItem(position); 
      } 
     }); 
     for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) { 
      actionBar.addTab(
        actionBar.newTab() 
          .setText(mSectionsPagerAdapter.getPageTitle(i)) 
          .setTabListener(this)); 
     } 

      public class SectionsPagerAdapter extends FragmentPagerAdapter { 
     // END_INCLUDE (fragment_pager_adapter) 
     final String TAG = "MainActivity"; 
     final String[] asTags = {(String) getResources().getText(R.string.strFEFragmentTagDetail), 
       (String) getResources().getText(R.string.strFEFragmentTagList)}; 

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

     public Fragment fetchFragment(int position) { 

      lw.dlog(TAG, "SectionsPagerAdapter, fetchFragment: ".concat(Integer.toString(position))); 
      String name = asTags[position]; 
      Fragment oFragment = getSupportFragmentManager().findFragmentByTag(name); 
      return (oFragment); 
     } 

     @Override 
     public Fragment getItem(int position) { 
      // getItem is called to instantiate the fragment for the given page. 
      // Return a DummySectionFragment (defined as a static inner class 
      // below) with the page number as its lone argument. 
      Fragment oFragment = null; 
      lw.dlog(TAG, "SectionsPagerAdapter, getItem: ".concat(Integer.toString(position))); 



       switch (position) { 
       case 0: 
        oFragment = new FragmentDetailTab();     
        break; 
       case 1: 
        oFragment = new FragmentListTab(); 
        break; 
       }   
      return oFragment; 
     } 

    } 
......................... 
    @Override 
    public void onTabReselected(Tab oTab, 
      android.support.v4.app.FragmentTransaction arg1) { 
    } 

    @Override 
    public void onTabSelected(Tab oTab, 
      android.support.v4.app.FragmentTransaction oFt) { 
     int nPos = oTab.getPosition(); 
     Fragment oSPA = mSectionsPagerAdapter.getItem(nPos); 
     mViewPager.setCurrentItem(nPos); 
     oFt.attach(oSPA); 
    } 

    @Override 
    public void onTabUnselected(Tab oTab, 
      android.support.v4.app.FragmentTransaction oFt) { 
     // TODO Auto-generated method stub 
     Fragment oFragment = mSectionsPagerAdapter.fetchFragment(oTab.getPosition()); 
     int nPos = oTab.getPosition(); 
     lw.dlog(TAG, "onTabUnselected: ".concat(Integer.toString(nPos))); 
     oFt.detach(oFragment); 

    } 




} 

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


public class FragmentFormulaTab extends Fragment { 
    public FragmentFormulaTab() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     super.onCreateView(inflater, container,savedInstanceState); 
     Log.d(TAG, "onCreateView: "); 
     View rootView = inflater.inflate(R.layout.fe_formulafragment, container, false); 
     return rootView; 
    } 

} 


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


public class FragmentListTab extends Fragment { 
    final String TAG = getTag(); 

    public FragmentListTab() { 
    } 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     super.onCreateView(inflater, container,savedInstanceState); 
     View rootView = inflater.inflate(R.layout.fe_listfragment, container, false); 
     return rootView; 
    } 
} 
+0

我找到了解決方案。從XML中刪除片段標籤確實有竅門。 – user3356078

回答

0

您應該在main xml中只使用一個viewPager,然後在其中加載片段。並確保使用操作欄選項卡。