Fragment.replace不删除一个片段,与多个其他片段一起使用

时间:2016-07-19 11:55:23

标签: android listview android-fragments

编辑:以下问题出现在我的三星Galaxy S3上,但是,当我在索尼Xperia Z3 +上运行相同的应用时,它根本不显示WIFI列表。 : - /

我的应用程序有一种奇怪的情况。我目前有五个不同的片段。除了一个FragmentTransaction之外,所有这些都按预期工作 最初,当我启动我的应用时,我使用了其中一个Android Studio模板,但这似乎严重过度,因为它使用Fragments代替ListView来列出我的WIFI项目。我用Android开发了一段时间已经有一段时间了,所以我正在追赶。

我将代码留在原地并继续开发界面。当我最终决定删除使用Fragment“items”填充主容器的代码并将其替换为包含Fragment的{​​{1}}时,出现了麻烦。

当我从菜单中选择一个新项目时,我的所有ListView按预期工作并按预期更换,但新的Fragments除外,一旦我选择它,它将保留在后台。

在我选择之后,如果我选择其他ListView Fragment,他们会按照自己的意愿进行更改,但第一个会保留原位。

我发现最接近我的情况的问题是this one,但它对我没有帮助。

选择问题Fragments和另一个Fragment之后,这就是我的屏幕的样子:

Layered Fragments

我的Fragment MainActivity方法:

onCreate()

我的 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); setupWifiScan(); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Scanning for devices...", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); scanForNetworks(view); } }); DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.setDrawerListener(toggle); toggle.syncState(); if(findViewById(R.id.fragment_container) != null){ if(savedInstanceState != null){ return; } NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); WifiFragment wifiFragment = new WifiFragment(); wifiFragment.setArguments(getIntent().getExtras()); FragmentManager manager = getSupportFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); transaction.replace(R.id.fragment_container, wifiFragment); transaction.commit(); }else{ Log.i("MAIN_ACTIVITY", "fragment_container is NULL"); } } MainActivity方法:

@覆盖 public boolean onNavigationItemSelected(MenuItem item){     //处理导航视图项目单击此处。     int id = item.getItemId();

onNavigationItemSelected()

问题if (id == R.id.nav_settings) { GeneralSettings generalSettings = new GeneralSettings(); Bundle args = new Bundle(); args.putInt(GeneralSettings.ARG_POSITON, 0); generalSettings.setArguments(args); FragmentManager manager = getSupportFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); transaction.replace(R.id.fragment_container, generalSettings); transaction.addToBackStack(null); int stackId = transaction.commit(); Log.i(this.getClass().getSimpleName(), "Stack ID: " + stackId); }else if(id == R.id.nav_wlan_setting){ WifiSettings wifiSettings = new WifiSettings(); Bundle args = new Bundle(); args.putInt(GeneralSettings.ARG_POSITON, 0); wifiSettings.setArguments(args); FragmentManager manager = getSupportFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); transaction.replace(R.id.fragment_container, wifiSettings); transaction.addToBackStack(null); int stackId = transaction.commit(); Log.i(this.getClass().getSimpleName(), "Stack ID: " + stackId); }else if(id == R.id.nav_led_settings){ UvLedSettings uvLedSettings = new UvLedSettings(); Bundle args = new Bundle(); args.putInt(GeneralSettings.ARG_POSITON,0); uvLedSettings.setArguments(args); FragmentManager manager = getSupportFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); transaction.replace(R.id.fragment_container, uvLedSettings); transaction.addToBackStack(null); int stackId = transaction.commit(); Log.i(this.getClass().getSimpleName(), "Stack ID: " + stackId); }else if(id == R.id.nav_server_settings){ ServerSettings serverSettings = new ServerSettings(); Bundle args = new Bundle(); // TODO: Fix Args Settings for all Fragments args.putInt(GeneralSettings.ARG_POSITON, 0); serverSettings.setArguments(args); FragmentManager manager = getSupportFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); transaction.replace(R.id.fragment_container, serverSettings); transaction.addToBackStack(null); int stackId = transaction.commit(); Log.i(this.getClass().getSimpleName(), "Stack ID: " + stackId); } else if (id == R.id.nav_current_device) { } else if (id == R.id.nav_available_devices){ WifiFragment wifi = new WifiFragment(); Bundle args = new Bundle(); args.putInt(GeneralSettings.ARG_POSITON, 0); wifi.setArguments(args); FragmentManager manager = getSupportFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); transaction.replace(R.id.fragment_container, wifi); transaction.addToBackStack(null); int stackId = transaction.commit(); Log.i(this.getClass().getSimpleName(), "Stack ID: " + stackId); } DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true; }

Fragment

问题package com.myapp.serviceapplication.fragments; import android.content.Context; import android.net.wifi.ScanResult; 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.ListView; import android.widget.RelativeLayout; import com.myapp.serviceapplication.R; import com.myapp.serviceapplication.adapters.WifiItemListAdapter; import java.util.ArrayList; /** * A fragment representing a list of Items. * <p> * Activities containing this fragment MUST implement the {@link OnListFragmentInteractionListener} * interface. */ public class WifiFragment extends Fragment { // TODO: Customize parameter argument names private static final String ARG_COLUMN_COUNT = "column-count"; // TODO: Customize parameters private int mColumnCount = 1; private OnListFragmentInteractionListener mListener; /** * Mandatory empty constructor for the fragment manager to instantiate the * fragment (e.g. upon screen orientation changes). */ public WifiFragment() { // Required empty public constructor } // TODO: Customize parameter initialization @SuppressWarnings("unused") public static WifiFragment newInstance(int columnCount) { WifiFragment fragment = new WifiFragment(); Bundle args = new Bundle(); args.putInt(ARG_COLUMN_COUNT, columnCount); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_wifi_list, container, false); if(view instanceof RelativeLayout){ Context context = view.getContext(); RelativeLayout relativeLayout = (RelativeLayout) view; ListView listView = (ListView) relativeLayout.findViewById(R.id.lst_wifi_items); listView.setAdapter(new WifiItemListAdapter(this.getContext(), new ArrayList<ScanResult>())); } return view; } @Override public void onAttach(Context context) { super.onAttach(context); // TODO: This needs to be modified to the correct listener type if (context instanceof OnListFragmentInteractionListener) { mListener = (OnListFragmentInteractionListener) context; } else { throw new RuntimeException(context.toString() + " must implement OnListFragmentInteractionListener"); } } @Override public void onDetach() { super.onDetach(); mListener = null; } /** * This interface must be implemented by activities that contain this * fragment to allow an interaction in this fragment to be communicated * to the activity and potentially other fragments contained in that * activity. * <p> * See the Android Training lesson <a href= * "http://developer.android.com/training/basics/fragments/communicating.html" * >Communicating with Other Fragments</a> for more information. */ public interface OnListFragmentInteractionListener { // TODO: Update argument type and name void onListFragmentInteraction(View item); } } 布局文件:

Fragment

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" tools:context=".fragments.WifiFragment" > <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/lst_wifi_items" android:background="#ffffff"/> </RelativeLayout>

content_main.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context=".activities.MainActivity" tools:showIn="@layout/app_bar_main"> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>

activity_main.xml

我花了太多时间试图解决这个问题,但我看不出这个问题。

2 个答案:

答案 0 :(得分:4)

这是因为你的片段容器在listview之上,默认情况下如果你不在ViewGroup中使用背景(LinearLayout,RelativeLayout,...)那么那将是透明的,所以,你需要做的只是放入一个背景你的片段容器:

fragment_wifi_list.xml

<RelativeLayout 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"
    tools:context=".fragments.WifiFragment"

    android:background="#ffffff"

    >
    ...

提示:

android:clickable="true"

这可以避免并发点击问题

答案 1 :(得分:2)

将以下行添加到有问题的片段

的顶部相对布局中
android:background="#ffffff"
android:clickable="true"

希望它有效。