我有一个 Main Activity ,它在第一次启动时显示一个 fragment ,该 fragment 显示已配对蓝牙设备的 ListView 。我不想使用 ListFragment,因为我想在顶部显示 TextView。 我创建了一个 onItemClickListener,它使用所选设备的 MAC 地址和名称设置字符串。这在日志中显示正常 我想将这些字符串发送回主要 Activity ,然后打开一个使用这些字符串连接到蓝牙设备的不同 fragment 。
我已经使用所有 Activity 完成了这项工作,但我想更改为 Fragments。
我的主要 Activity 是。
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
// MAC-address of Bluetooth module (you must edit this line)
private static String address = "No Device Selected";
private static String itemName = "No Device Selected";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
class="com.ming.pondcontroller.AddressFragment"
android:id="@+id/main_frag"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
我的地址 fragment
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Set;
public class AddressFragment extends Fragment {
private static final String TAG = "get address Activity";
private BluetoothAdapter BA = null;
private Set<BluetoothDevice> pairedDevices;
LayoutInflater inflater2;
ListView lv;
String address;
String itemName;
String itemString;
public AddressFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
inflater2 = inflater;
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_address, container, false);
}
@Override
public void onStart(){
super.onStart();
lv = (ListView)getView().findViewById(R.id.listView);
BA = BluetoothAdapter.getDefaultAdapter();
checkBTState();
// get paired devices
pairedDevices = BA.getBondedDevices();
ArrayList list = new ArrayList();
// get a list of paired devices
for(BluetoothDevice bt:pairedDevices){
list.add(bt.getName()+"\n"+bt.getAddress());
}
// display the list in the list view
final ArrayAdapter adapter = new ArrayAdapter(inflater2.getContext(),android.R.layout.simple_list_item_1,list);
lv.setAdapter(adapter);
// onItemClick Listener
lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> adapterView,View view, int position, long l){
// get the text of the item clicked from position
itemString = (String)(lv.getItemAtPosition(position));
// get last 17 characters for MAC address
address = itemString.substring(itemString.length()-17);
itemName = itemString.substring(0,itemString.length()-17);
Log.d(TAG,address);
}
}); //end of onClickListener
}
private void checkBTState() {
// Check for Bluetooth support and then check to make sure it is turned on
// Emulator doesn't support Bluetooth and will return null
if(BA==null) {
Log.d("Fatal Error", "Bluetooth not support");
} else {
if (BA.isEnabled()) {
Log.d(TAG, "...Bluetooth ON...");
} else {
//Prompt user to turn on Bluetooth
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
}
}
}
}
及其布局。
<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"
android:background="@color/colorMainBack"
tools:context="com.ming.pondcontroller.AddressFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="@+id/textView"
android:background="@color/colorPrimary"
android:textColor="@color/colorWhite"
android:textSize="20dp"
android:text="Select Device"/>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/textView"
android:background="@color/colorTextBack">
</ListView> />
</RelativeLayout>
我查看了大量回调、监听器和接口(interface)的示例,但似乎无法在不使用 listFragment 的情况下找到正确的东西。
最佳答案
创建一个您的 Activity 将实现的接口(interface),然后在 onAttach()
中将其设置为您的 Fragment 中的成员变量:
接口(interface)类:
interface BluetoothItemSelected {
void onBluetoothItemSelected(String itemName, String itemAddress);
}
MainActivity 实现:
public class MainActivity extends AppCompatActivity implements BluetoothItemSelected {
// MAC-address of Bluetooth module (you must edit this line)
private static String address = "No Device Selected";
private static String itemName = "No Device Selected";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onBluetoothItemSelected(String itemName, String itemAddress) {
// TODO do your work here
}
// your other Activity code...
}
fragment 实现:
public class AddressFragment extends Fragment {
private static final String TAG = "get address Activity";
private BluetoothAdapter BA = null;
private Set<BluetoothDevice> pairedDevices;
LayoutInflater inflater2;
ListView lv;
String address;
String itemName;
String itemString;
BluetoothItemListener btItemListener;
@Override
public void onAttach(Context activity) {
if (activity instanceof BluetoothItemListener) {
btItemListener = (BluetoothItemListener) activity;
}
}
@Override
public void onDetach() {
// remove the reference to your Activity
btItemListener = null;
}
@Override
public void onStart(){
super.onStart();
lv = (ListView)getView().findViewById(R.id.listView);
BA = BluetoothAdapter.getDefaultAdapter();
checkBTState();
// get paired devices
pairedDevices = BA.getBondedDevices();
ArrayList list = new ArrayList();
// get a list of paired devices
for(BluetoothDevice bt:pairedDevices){
list.add(bt.getName()+"\n"+bt.getAddress());
}
// display the list in the list view
final ArrayAdapter adapter = new ArrayAdapter(inflater2.getContext(),android.R.layout.simple_list_item_1,list);
lv.setAdapter(adapter);
// onItemClick Listener
lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> adapterView,View view, int position, long l){
// get the text of the item clicked from position
itemString = (String)(lv.getItemAtPosition(position));
// get last 17 characters for MAC address
address = itemString.substring(itemString.length()-17);
itemName = itemString.substring(0,itemString.length()-17);
Log.d(TAG,address);
// call your interface here
if (btItemListener != null) {
btItemListener.onBluetoothItemSelected(itemName, address);
}
}
}); //end of onClickListener
}
// your other Fragment code ...
}
关于java - 我如何获取 fragment 以将 onClickListener 结果发送回主要 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39904416/