嵌套片段中的 findViewById
findViewById in nested Fragment
我试图在片段 (R.layout.fragment_2_ble) 中显示 Activity 处理的结果,而不是 activity 主布局 (R.layout .activity_main).
因此,当我试图找到 fragment_2_ble 而不是 activity_main 中的 (R.id.devicelist) 时遇到问题。
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);
}
知道如何解决这个问题吗?
在你的片段中onCreateView()
你会有这样的东西:
YOURLISTVIEW = (ListView) view.findViewById(R.id.deviceList);
在您的片段中,您可以创建这样的方法:
public ListView getListView(){
return YOURLISTVIEW;
}
然后在 main 中你会有一个片段的实例。
您可以从片段中访问 ListView
。
myFragment.getListView();
我要补充一点,如果这个 activity 是片段的父级,那么 findViewById(R.id.fragmentITEM)
应该可以。
默认情况下您无法实现这种结构。您无法在创建时在 activity 中找到片段 UI 元素。
在 activity 中创建 onCreate 视图时未创建 Becoz 片段 UI 元素。
您可以在 link 中找到简短的答案:Is it possible to access fragment's UI elements from activitiy's onCreate
我认为您缺少有关片段的概念。
如果 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;
}
}
我试图在片段 (R.layout.fragment_2_ble) 中显示 Activity 处理的结果,而不是 activity 主布局 (R.layout .activity_main).
因此,当我试图找到 fragment_2_ble 而不是 activity_main 中的 (R.id.devicelist) 时遇到问题。
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);
}
知道如何解决这个问题吗?
在你的片段中onCreateView()
你会有这样的东西:
YOURLISTVIEW = (ListView) view.findViewById(R.id.deviceList);
在您的片段中,您可以创建这样的方法:
public ListView getListView(){
return YOURLISTVIEW;
}
然后在 main 中你会有一个片段的实例。
您可以从片段中访问 ListView
。
myFragment.getListView();
我要补充一点,如果这个 activity 是片段的父级,那么 findViewById(R.id.fragmentITEM)
应该可以。
默认情况下您无法实现这种结构。您无法在创建时在 activity 中找到片段 UI 元素。
在 activity 中创建 onCreate 视图时未创建 Becoz 片段 UI 元素。
您可以在 link 中找到简短的答案:Is it possible to access fragment's UI elements from activitiy's onCreate
我认为您缺少有关片段的概念。 如果 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;
}
}