如何在片段中修复fab监听器?

问题描述 投票:0回答:2

我有一个片段与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>
android android-fragments floating-action-button
2个回答
0
投票

删除此行

View rootView = inflater.inflate(R.layout.fragment_blank_fragment4, container,
            false);

您正在添加fab onClickListener以从第一次充气中查看。在片段的onCreateView中,您应该只膨胀一次,并返回函数末尾的视图。


0
投票

你能尝试一下,看看它是否有效:用on return视图替换onCreateView方法末尾的返回rootView;相反?

© www.soinside.com 2019 - 2024. All rights reserved.