java - 我的 fragment 在替换时重叠

标签 java android xml android-fragments

大家怎么了

所以我有一个带有两个选项卡和滑动导航的应用程序。

在我的第一个选项卡( fragment )中,我添加了一个按钮,当 onClick 时,使用 FragmentTransaction.replace() 将当前 fragment 替换为新 fragment 。然而,当我单击按钮时, fragment 会相互重叠。我几乎尝试了所有方法,我认为这可能与我的 .xml 文件有关。

这就是正在发生的事情:

Delivery fragment

Hitting "New Delivery" button.显示了新 fragment 的一半。

这是我的代码:

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 这是我尝试替换按钮单击 fragment 的位置

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>

最佳答案

When you add a fragment to an activity layout by defining the fragment in the layout XML file, you cannot remove the fragment at runtime. If you plan to swap your fragments in and out during user interaction, you must add the fragment to the activity when the activity first starts, as shown in the next lesson.

Creating a fragment

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

关于java - 我的 fragment 在替换时重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26538147/

相关文章:

java - java中的文件共享

java - 以编程方式更改应用程序资源语言后,SlidingTabLayout 的选项卡语言不会更改

xml - Qt如何读取XML?

java - "Exception javax.xml.ws.WebServiceException: Unsupported endpoint address"尝试使用 JAX-WS 2.1 调用 Web 服务

java - 有没有办法在 play 框架中的拦截器和 Action 之间共享数据?

java - 在 Activity 之间共享数据的最佳方式是什么?

java - Android 中的默认录音

android - 如何访问 FirebaseRecycler 的 getItemViewType() 中的项目?

javascript - 请,需要帮助循环 Javascript 事件

java - 如何在TabLayout中包裹单个选项卡宽度?