【问题标题】:android layout messed up (using Fragments/Viewpager)android 布局混乱(使用 Fragments/Viewpager)
【发布时间】:2014-03-25 19:24:53
【问题描述】:

我有一个带有 2 个选项卡的应用程序,其中包含几个控件 Editext/Textviews/ListView 和 我最近从 Tabhost 更改为 Fragments/ViewPager。 当我启动应用程序时,两个选项卡的内容会同时绘制到屏幕上。 如果我选择另一个选项卡,属于旧选项卡的控件不会被删除。, 这会导致屏幕混乱。 如果我输入文本,EditText 的提示也不会消失。 我还尝试了一个简化的布局(更少的控件)并取得了成功。

在我使用片段之前,这种事情并没有发生。 知道我错过了什么吗?

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

【问题讨论】:

  • 我找到了解决方案。从 XML 中删除片段标签就可以了。

标签: android android-layout android-fragments android-viewpager


【解决方案1】:

你应该在主xml中只使用一个viewPager,然后在其中加载片段。并且一定要使用action bar tab。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多