在 Android 应用程序中,我有两个 fragment :
带有 ListView 项的 fragment
带有ImageView的 fragment
通过回调 onListItemSelected,当用户单击 ListView 项时,MainActivity 将 ImageView 压入堆栈,并且带有图像的 fragment 出现在屏幕上。此时,我希望由于 ListView fragment 不再可见,因此不再触发与该 fragment 关联的任何事件。不是这种情况。如果我触摸 ImageView,ListView 项目的监听器仍然会触发。
两个问题:
有没有办法根据 Fragment 可见性自动启用/禁用监听器?
如果没有,我想可行的方法是禁用 ListView fragment View ,然后在按下后退按钮时重新启用它。如何捕获 MainActivity 中的 backButton 事件以重新启用之前禁用的 View ?
public class MainActivity extends FragmentActivity implements ListViewFragment.Callbacks {
[...]
public void onListItemSelected(String str) { FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.addToBackStack(null); fragmentTransaction.replace(R.id.listView, f); fragmentTransaction.commit(); // disable listView //View lw = getSupportFragmentManager().findFragmentById(R.id.listView).getView().findViewById(R.id.my_listView); //lw.setEnabled(false); }
最佳答案
你可以尝试两件事
设置 ImageView 以消耗触摸事件。
ImageView.setClickable(true);
推送新 fragment 时禁用 ListView 上的触摸事件。
ListView.setClickable(false);
如果您想知道如何知道何时删除带有 ImageView 的 fragment ,请尝试 setTargetFragment。看看这里:https://stackoverflow.com/a/13733914/935421
关于android - 根据 fragment 可见性自动禁用/启用监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18603275/