如何修复 fragment 中的 fab 监听器?
how to fix fab listener in fragment?
我有一个 lsitview 片段。
当我点击按钮时,什么都没有发生,期待动画。
我尝试 fab.bringToFront();
但它也不起作用。
我认为我的 OnclickListener 有问题,但我不知道如何解决。
这是我的 BlankFragment4.java
package com.example.vbg.myapplication;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.Set;
public class BlankFragment4 extends ListFragment {
private static final String TAG = "Bluetooth Settings";
private ArrayAdapter<String> mNewDevicesArrayAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_blank_fragment4, container, false);
FloatingActionButton fab = view.findViewById(R.id.fab);
fab.bringToFront();
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i(TAG, "FAB was Pressed");
Toast.makeText(getActivity(), "FAB Pressed", Toast.LENGTH_LONG).show();
}
});
mNewDevicesArrayAdapter = new ArrayAdapter<>(getActivity(),
android.R.layout.simple_list_item_1);
// Register for broadcasts when a device is discovered.
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
getActivity().registerReceiver(receiver, filter);
BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = bluetooth.getBondedDevices();
if (!bluetooth.isEnabled()) {
Toast.makeText(getActivity(), "bluetooth is off", Toast.LENGTH_LONG).show();
}
View rootView = inflater.inflate(R.layout.fragment_blank_fragment4, container,
false);
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress(); // MAC address
mNewDevicesArrayAdapter.add(deviceName + "\n" + deviceHardwareAddress);
}
}
setListAdapter(mNewDevicesArrayAdapter);
return rootView;
}
private final BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress();
mNewDevicesArrayAdapter.add(device.getName()+"\n"+ device.getAddress());
mNewDevicesArrayAdapter.notifyDataSetChanged();
Toast.makeText(getActivity(), "one new device find", Toast.LENGTH_SHORT).show();// MAC address
}
}
};
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Toast.makeText(getActivity(), "Connecting... ", Toast.LENGTH_SHORT).show();
}
@Override
public void onDestroy() {
super.onDestroy();
getActivity().unregisterReceiver(receiver);
}
}
这是我的 xml 文件。
fragment_blank_fragment4
<?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"
tools:context=".BlankFragment4">
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="16dp"
android:layout_marginRight="16dp"
android:clickable="true"
android:src="@drawable/ic_bluetooth_searching_white_24dp"
app:fabSize="normal"
app:elevation="4dp"/>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:cacheColorHint="@android:color/transparent"></ListView>
</RelativeLayout>
你能试试这个,看看它是否有效:
将 onCreateView 方法末尾的 return rootView 替换为 return view; instead ?
删除这一行
View rootView = inflater.inflate(R.layout.fragment_blank_fragment4, container,
false);
您正在添加 fab onClickListener 以从第一个 inflate 开始查看。在onCreateView
片段的片段你应该只膨胀一次,return 函数末尾的视图。
我有一个 lsitview 片段。
当我点击按钮时,什么都没有发生,期待动画。
我尝试 fab.bringToFront();
但它也不起作用。
我认为我的 OnclickListener 有问题,但我不知道如何解决。
这是我的 BlankFragment4.java
package com.example.vbg.myapplication;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.Set;
public class BlankFragment4 extends ListFragment {
private static final String TAG = "Bluetooth Settings";
private ArrayAdapter<String> mNewDevicesArrayAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_blank_fragment4, container, false);
FloatingActionButton fab = view.findViewById(R.id.fab);
fab.bringToFront();
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i(TAG, "FAB was Pressed");
Toast.makeText(getActivity(), "FAB Pressed", Toast.LENGTH_LONG).show();
}
});
mNewDevicesArrayAdapter = new ArrayAdapter<>(getActivity(),
android.R.layout.simple_list_item_1);
// Register for broadcasts when a device is discovered.
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
getActivity().registerReceiver(receiver, filter);
BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = bluetooth.getBondedDevices();
if (!bluetooth.isEnabled()) {
Toast.makeText(getActivity(), "bluetooth is off", Toast.LENGTH_LONG).show();
}
View rootView = inflater.inflate(R.layout.fragment_blank_fragment4, container,
false);
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress(); // MAC address
mNewDevicesArrayAdapter.add(deviceName + "\n" + deviceHardwareAddress);
}
}
setListAdapter(mNewDevicesArrayAdapter);
return rootView;
}
private final BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress();
mNewDevicesArrayAdapter.add(device.getName()+"\n"+ device.getAddress());
mNewDevicesArrayAdapter.notifyDataSetChanged();
Toast.makeText(getActivity(), "one new device find", Toast.LENGTH_SHORT).show();// MAC address
}
}
};
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Toast.makeText(getActivity(), "Connecting... ", Toast.LENGTH_SHORT).show();
}
@Override
public void onDestroy() {
super.onDestroy();
getActivity().unregisterReceiver(receiver);
}
}
这是我的 xml 文件。 fragment_blank_fragment4
<?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"
tools:context=".BlankFragment4">
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="16dp"
android:layout_marginRight="16dp"
android:clickable="true"
android:src="@drawable/ic_bluetooth_searching_white_24dp"
app:fabSize="normal"
app:elevation="4dp"/>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:cacheColorHint="@android:color/transparent"></ListView>
</RelativeLayout>
你能试试这个,看看它是否有效: 将 onCreateView 方法末尾的 return rootView 替换为 return view; instead ?
删除这一行
View rootView = inflater.inflate(R.layout.fragment_blank_fragment4, container,
false);
您正在添加 fab onClickListener 以从第一个 inflate 开始查看。在onCreateView 片段的片段你应该只膨胀一次,return 函数末尾的视图。