我正在尝试在片段(R.layout.fragment_2_ble)中显示活动处理的结果,而不是主活动布局(R.layout.activity_main)。
因此,当我试图找到片段_2_ble中的(R.id.devicelist)而不是activity_main时,我遇到了问题。
MainActivity.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//setButtonText();
ble_device_list_adapter = new ListAdapter();
ListView listView = (ListView) this.findViewById(R.id.deviceList);
listView.setAdapter(ble_device_list_adapter);
ble_scanner = new BLEScanner();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (ble_scanning) {
ble_scanner.stopScanning();
}
BluetoothDevice device = ble_device_list_adapter.getDevice(position);
if (toast != null) {
toast.cancel();
}
Intent intent = new Intent(MainActivity.this, PeripheralControlActivity.class);
intent.putExtra(PeripheralControlActivity.EXTRA_NAME, device.getName());
intent.putExtra(PeripheralControlActivity.EXTRA_ID, device.getAddress());
startActivity(intent);
}
});
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
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();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
displaySelectedScreen(R.id.nav_data);
//ButterKnife.bind(this);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean logged = prefs.getBoolean("logged", false);
if (!logged) {
//Launch login activity
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
}
}
@Override
protected void onStart() {
super.onStart();
}
private void displaySelectedScreen(int itemId) {
//creating fragment object
Fragment fragment = null;
//initializing the fragment object which is selected
switch (itemId) {
case R.id.nav_data:
fragment = new DataFragment();
break;
case R.id.nav_BLE:
fragment = new BLEFragment();
break;
case R.id.nav_settings:
fragment = new SettingsFragment();
break;
}
//replacing the fragment
if (fragment != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
displaySelectedScreen(item.getItemId());
return true;
}
BLEFragment.java:
public class BLEFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//returning our layout file
return inflater.inflate(R.layout.fragment_2_ble, container, false);
}
有关如何解决这个问题的想法吗?
答案 0 :(得分:2)
我认为你缺少有关片段的概念。 如果listView位于片段中,则应创建findViewById并在片段中设置OnClickListener。就像这样:
public class BLEFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//returning our layout file
View view = inflater.inflate(R.layout.fragment_2_ble, container, false);
ListView list = (ListView) view.findViewById(R.id.your_id);
list.setOnClickListenre(...);
return view;
}
}
答案 1 :(得分:0)
在您的片段onCreateView()
中,您将拥有以下内容:
YOURLISTVIEW = (ListView) view.findViewById(R.id.deviceList);
在片段内部,你可以创建一个这样的方法:
public ListView getListView(){
return YOURLISTVIEW;
}
然后在main中你将有一个片段的实例。
您可以从片段中访问ListView
。
myFragment.getListView();
我将补充一点,如果此活动是片段的父级,则findViewById(R.id.fragmentITEM)
应该有效。
答案 2 :(得分:0)
默认情况下,您无法实现此类结构。您无法在创建活动中找到片段UI元素。
在活动中创建onCreate视图时未创建Becoz片段UI元素。
您可以在此链接中找到简短的答案:Is it possible to access fragment's UI elements from activitiy's onCreate