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,然后在其中加载片段。并确保使用操作栏选项卡。