gpt4 book ai didi

android - 从 fragment 中的 onItemClick 访问 BottomNavigationView 项

转载 作者:行者123 更新时间:2023-12-04 10:12:47 26 4
gpt4 key购买 nike

我有一个 MainActivityBottomNavigationView与 3 items在里面。当我第一次打开应用程序时,只有一个启用,第一个。

我有一个 RecyclerView在第一个 fragment 上(菜单中的第一个 item)。当我点击一个 itemRecyclerView ,我想切换到第二个Fragment并启用第二个和第三个 item .

我可以切换到第二个 Fragment ,但我无法再次启用这些项目。

第一FragmentRecyclerView是:

public class Homefragment extends Fragment implements OnItemClickListener {

private ArrayList<fileItem> mfileList = new ArrayList<>();
private RecyclerView mRecyclerView;
private fileAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;

private BottomNavigationView bottomNav;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment_home, container, false);

Context context = getActivity();
bottomNav = view.findViewById(R.id.bottom_navigation);

// Listing all text files
String path = Environment.getExternalStorageDirectory().toString()+"/FlexiCounts";
File directory = new File(path);
File[] filesList = directory.listFiles(new FilenameFilter() {
public boolean accept(File directory, String name) {
return name.toLowerCase().endsWith(".txt");
}
});

setRecyclerView(view, filesList);

return view;
}

private void setRecyclerView(View view, File[] files){

if (files.length > 0) {

for (int i = 0; i < files.length; i++) {
mfileList.add(new fileItem(R.drawable.ic_account_balance_black_24dp, files[i].getName().replace(".txt", ""), "25-03-2020", "07-04-2020"));
}

mRecyclerView = view.findViewById(R.id.fileRecycler);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getActivity());
mAdapter = new fileAdapter(mfileList, this);

mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);

}
}


// the magic should happen here !
@Override
public void onItemClick(int position) {
mfileList.get(position);
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container_primary, new Dashboardfragment()).commit();

bottomNav.getMenu().getItem(1).setEnabled(true);
//bottomNav.getMenu().getItem(2).setEnabled(true);

}
}

以下几行不起作用:
bottomNav.getMenu().getItem(1).setEnabled(true);

它似乎指向一个空引用。我认为它没有找到 bottomNavR.id.bottom_navigation 定义在 onCreateView方法。事实上, bottom_navigation包含在 MainActivitylayout ,但这应该不是问题。我收到此错误:
    --------- beginning of crash
2020-04-15 20:09:20.202 8673-8673/com.flexicounts.flexicounts E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.flexicounts.flexicounts, PID: 8673
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Menu android.support.design.widget.BottomNavigationView.getMenu()' on a null object reference
at com.flexicounts.flexicounts.Homefragment.onItemClick(Homefragment.java:145)
at com.flexicounts.flexicounts.fileAdapter$fileViewHolder.onClick(fileAdapter.java:50)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

最佳答案

创建 BottomNavigationView getBottomNav()主要 Activity 中的方法然后从 onItemClick 获取它

MainActivity :

public BottomNavigationView getBottomNav(){ return findViewById(R.id.bottom_navigation); }

Homefragment :
// the magic should happen here !
@Override
public void onItemClick(int position) {
mfileList.get(position);
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container_primary, new Dashboardfragment()).commit();

((MainActivity) getActivity()).getBottomNav().getMenu().getItem(1).setEnabled(true);
//bottomNav.getMenu().getItem(2).setEnabled(true);

}

关于android - 从 fragment 中的 onItemClick 访问 BottomNavigationView 项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61240695/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com