java如何在单击ListView条目时切换到另一个片段?
我正在开发一款Android应用程序,并使用带有两个片段的TabLayout,Map和Discovery。在发现片段中,我有一个ListView,用于显示应用程序连接到的设备。我正在尝试添加一个功能,一旦单击ListView中的条目,就会加载映射片段。尝试通过添加代码来实现此功能
Fragment fragment = new MapFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_map,fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
到ListView的OnClickListener,其中R.id.content_map是与MapFragment类一起使用的布局文件的id。我找到了上面的代码here。下面是DiscoveryFragment和MapFragment类的定义
public class DiscoveryFragment extends Fragment {
private DhcpInfo mDhcpInfo = null;
private ArrayList<String> mArrayList = null;
private final int TASK_COMPLETE = 1;
private Handler mHandler = null;
private final String IP_KEY = "IPs";
private DiscoveryArrayAdapter mArrayAdapter = null;
private final String ROBOT_ID = "create2";
private AppDatabase db = null;
private int mTaskCounterVar = 0;
public DiscoveryFragment() {
// This is an anonymous class which extends the Handler class
// Handler object is attached to the UI thread
mHandler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(@NonNull Message message) {
// Get the message string from the Message
super.handleMessage(message);
Bundle bundle = message.getData();
ArrayList<String> msgs = bundle.getStringArrayList(IP_KEY);
if(msgs != null) {
mArrayList.addAll(msgs);
}
mArrayAdapter.notifyDataSetChanged();
}
};
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WifiManager wifiManager = (WifiManager) getContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
mDhcpInfo = wifiManager.getDhcpInfo();
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
ListView listView = null;
final View discoveryView = inflater.inflate(R.layout.fragment_discovery,container,false);
mArrayList = new ArrayList<String>();
new Thread(new FindRobotTask()).start();
db = Room.databaseBuilder(discoveryView.getContext(), AppDatabase.class, "AppDatabase").build();
// Setup a new thread to perform network operations
// Use the ArrayAdapter to register new content to the ListView
mArrayAdapter = new DiscoveryArrayAdapter(discoveryView.getContext(), mArrayList);
FloatingActionButton fab = (FloatingActionButton) discoveryView.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new FindRobotTask()).start();
mArrayAdapter.notifyDataSetChanged();
}
});
listView = (ListView)discoveryView.findViewById(R.id.listview);
listView.setAdapter(mArrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Fragment fragment = new MapFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_map,fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
return discoveryView;
}
class FindRobotTask implements Runnable {
// Implement the run method
public void run() {
Bundle bundle = new Bundle();
Message message = mHandler.obtainMessage();
ArrayList<String> ipArrayList = new ArrayList<String>();
try {
String msg = "Hello";
String ip = "";
InetAddress group = InetAddress.getByName("224.3.29.71");
MulticastSocket sock = new MulticastSocket();
sock.joinGroup(group);
byte[] bytes = msg.getBytes();
DatagramPacket hi = new DatagramPacket(bytes, bytes.length, group, 10000);
sock.send(hi);
byte[] buf = new byte[1000];
String recvMsg = "";
InetAddress address = null;
for(int i = 0; i < 10 && !"iRobot Create 2".equals(recvMsg); i++) {
DatagramPacket recv = new DatagramPacket(buf, buf.length);
sock.receive(recv);
recvMsg = new String(recv.getData(), 0, recv.getLength());
address = recv.getAddress();
}
ip = address.getHostAddress();
List<Robot> robotsWithIp = db.robotDao().loadByIp(ip);
// If a corresponding table entry is not found,
// add the robot to the database.
if(mTaskCounterVar == 0) {
if(robotsWithIp.size() == 0) {
ipArrayList.add(recvMsg + ": " + ip);
Robot robot = new Robot(recvMsg, ip);
db.robotDao().insert(robot);
mTaskCounterVar++;
// Else load the robot from the database
} else if (robotsWithIp.size() > 0) {
for(Robot robot : robotsWithIp) {
if(robot != null) {
ipArrayList.add(robot.name + ": " + robot.ip);
}
}
mTaskCounterVar++;
}
}
sock.leaveGroup(group);
sock.close();
} catch(UnknownHostException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
bundle.putStringArrayList(IP_KEY,ipArrayList);
message.setData(bundle);
mHandler.sendMessage(message);
}
}
}
public class MapFragment extends Fragment {
public MapFragment() {
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_map, container, false);
}
}
但是,当我运行应用程序时,在列表视图中单击条目时,发现片段不会被MapFragment替换。有人能告诉我哪里出了问题吗?我相信它可能是传递给replace方法的id,但我真的不确定
共 (0) 个答案