我的片段在替换时重叠

时间:2014-10-23 21:55:44

标签: java android xml android-fragments

发生什么事

所以我有一个带有两个标签和滑动导航的应用。

在我的第一个标签(片段)中,我添加了一个按钮,当onClick时,使用FragmentTransaction.replace()用新片段替换当前片段。然而,当我点击按钮时,片段会相互重叠。我几乎尝试了所有内容,我认为这可能与我的.xml文件有关。

这就是发生的事情:

Delivery fragment

Hitting "New Delivery" button.显示新片段的一半。

这是我的代码:

MainActivity.java

package com.example.deliverytracker;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

public class MainActivity extends FragmentActivity implements TabListener {

    ViewPager   viewPager;
    ActionBar   actionBar;
    FragAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Changes fragments by user swiping
        adapter = new FragAdapter(getSupportFragmentManager());

        viewPager = (ViewPager) findViewById(R.id.pager);
        viewPager.setAdapter(adapter);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {  
                actionBar.setSelectedNavigationItem(arg0);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {}

            @Override
            public void onPageScrollStateChanged(int arg0) {}
        });

        actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        ActionBar.Tab tab_deliv = actionBar.newTab().setText(R.string.tab_deliv);
        ActionBar.Tab tab_cust  = actionBar.newTab().setText(R.string.tab_cust);

        tab_deliv.setTabListener(this);
        tab_cust.setTabListener(this);

        actionBar.addTab(tab_deliv);
        actionBar.addTab(tab_cust);

    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {}

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {}

}

activity_main.xml中

<android.support.v4.view.ViewPager 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity" />

FragAdapter.java

package com.example.deliverytracker;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class FragAdapter extends FragmentPagerAdapter {

    final int TOTAL_PAGES = 2;

    public FragAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }

    @Override
    public Fragment getItem(int i) {

        Fragment f = null;

        // If position is 0, return Fragment A
        if(i == 0){
            f = new FragmentDeliv();
        }
        if(i == 1){
            f = new FragmentCustomers();
        }

        return f;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return TOTAL_PAGES;
    }

}

FragmentDeliv.java这就是我在尝试更换按钮上的碎片

package com.example.deliverytracker;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.*;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class FragmentDeliv extends Fragment {

    private View view;
    private Button newDeliv;
    private ListView delivList;

    public FragmentDeliv() {
    // Required empty public constructor
    }

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

        view = inflater.inflate(R.layout.fragment_deliv, container, false);

        setUpVars();

        newDeliv.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {

                FragmentAddDelivery fragment = new FragmentAddDelivery();
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                ft.replace(R.id.frag_delivery, fragment, "tag");
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                //ft.hide(getFragmentManager().findFragmentByTag("tag"));   
                ft.addToBackStack("tag");
                ft.commit();
            }

        });

        // Inflate the layout for this fragment
        return view;

    }

    private void setUpVars() {
        newDeliv = (Button) view.findViewById(R.id.btnAddDeliv);        
        delivList = (ListView) view.findViewById(R.id.listDeliv);
        ArrayList<String> list = new ArrayList<String>();
        ArrayAdapter<String> listAdapter = 
                new ArrayAdapter<String>(this.getActivity(),
                                         android.R.layout.simple_list_item_1,
                                         list);
        delivList.setAdapter(listAdapter);

        list.add("fds");
        list.add("fds");
        list.add("fds");    
    }


}

fragment_deliv.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/frag_delivery"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context=".FragmentDeliv" >

    <ListView
        android:id="@+id/listDeliv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" >
    </ListView>

    <Button
        android:id="@+id/btnAddDeliv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="4"
        android:text="@string/add_delivery" />

</LinearLayout>

FragmentAddDelivery.java

package com.example.deliverytracker;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;

public class FragmentAddDelivery extends Fragment {

    private EditText addr, city, apt, name, num, notes;

    public FragmentAddDelivery() {
        // Required empty public constructor
    }

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

        View view = inflater.inflate(R.layout.fragment_add_delivery, container, false);

        addr  = (EditText) view.findViewById(R.id.et_addr);
        city  = (EditText) view.findViewById(R.id.et_city);
        apt   = (EditText) view.findViewById(R.id.et_apt);
        name  = (EditText) view.findViewById(R.id.et_name);
        num   = (EditText) view.findViewById(R.id.et_num);
        notes = (EditText) view.findViewById(R.id.et_notes);

        return view;
    }

}

fragment_add_delivery.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frag_add_deliv"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/et_addr"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@drawable/rounded_edittext"
        android:ems="10"
        android:hint="Address (Required)"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/et_city"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:ems="10"
        android:hint="City (Required)"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/et_apt"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:ems="10"
        android:hint="Apt (Optional)"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/et_name"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:ems="10"
        android:hint="Name (Optional)"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/et_num"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:ems="10"
        android:hint="Number (Optional)"
        android:inputType="phone" />

    <EditText
        android:id="@+id/et_notes"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:ems="10"
        android:hint="Notes (Optional)"
        android:inputType="textPersonName" />

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

  

通过定义片段将片段添加到活动布局中   在布局XML文件中,您无法在运行时删除该片段。如果   你计划在用户交互过程中交换你的片段,   您必须在活动开始时将片段添加到活动中   开始,如下一课所示。

Creating a fragment

考虑到您在xml中定义了<fragment>标记。但是从您的代码中,您似乎正在尝试用片段替换父LinearLayout?它没有意义。示例@ Google将为您提供帮助。

答案 1 :(得分:0)

所以我想通了。

我所做的是,在片段适配器中,我没有为每个相应的选项卡返回我想要显示的片段,而是为每个选项卡创建了一个空白的根片段,每个选项卡都拥有自己的FrameLayout容器而没有其他内容。

在根片段类中,我有一个FragmentTransaction,用我最初想要的片段替换根帧。

因此,对于我想要在该选项卡中显示的任何片段,我只需使用新片段更新root_frame