我正在使用带有(碎片)的androidx
导航抽屉,HomeFragment
由RecyclerView
组成以显示图像。
我正在尝试调用getMenu()
方法,该方法会更新MainActivity
中的图像,但不起作用
我尝试使用此MainActivity
,但不起作用
HomeFragment fragment=new HomeFragment();
fragment.getMenu();
=========================== HomeFragment ================== =============
public class HomeFragment extends Fragment {
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
recycler_menu = (RecyclerView)view.findViewById(R.id.recycler_menu);
recycler_menu.setLayoutManager(new GridLayoutManager(context,2));
recycler_menu.setHasFixedSize(true);
mService = Common.getAPI();
getMenu();
return view;
}
public void getMenu() {
compositeDisposable.add(mService.getMenu()
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Consumer<List<Category>>() {
@Override
public void accept(List<Category> categories) throws Exception {
displayMenuList(categories);
}
}));
}
private void displayMenuList(List<Category> categories) {
MenuAdapter adapter = new MenuAdapter(context,categories);
recycler_menu.setAdapter(adapter);
}
}
============================ MainActivity =================== =============
public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu_action_bar ID as a set of Ids because each
// menu_action_bar should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_send)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
@Override
public void onResume() {
super.onResume();
HomeFragment fragment=new HomeFragment();
fragment.getMenu();
}
}
答案 0 :(得分:0)
不能,因为该方法无效,并且结果通过另一种方法返回,并且位于您的片段中。
您应该创建一个接口以将数据从片段传递到活动。
HomeFragment:
public class HomeFragment extends Fragment {
private OnAcceptListener listener;
//...
public void getMenu() {
compositeDisposable.add(mService.getMenu()
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Consumer<List<Category>>() {
@Override
public void accept(List<Category> categories) throws Exception {
listener.onAccept(categories);
}
}));
}
public void setOnAcceptListener(OnAcceptListener listener) {
this.listener = listener;
}
public interface OnAcceptListener{
void onAccept(List<Category> categories);
}
}
活动:
public class MainActivity extends AppCompatActivity implements HomeFragment.OnAcceptListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//...
HomeFragment homeFragment = new HomeFragment();
homeFragment.setOnAcceptListener(this);
}
@Override
public void onAccept(List<Category> categories) {
//update here
}
}