【问题标题】:Disable MainActivity SwipeRefreshLayout from Fragment从 Fragment 禁用 MainActivity SwipeRefreshLayout
【发布时间】:2018-03-01 05:10:52
【问题描述】:

我有一个主要活动,其中包括SwipeRefreshLayout。该活动包含fragments

我想在特定片段中禁用SwipeRefreshLayout,因为SwipeRefreshLayout 的滑动手势会干扰片段上ExpandableListView 的滚动手势。

为此,我尝试了以下方法,但没有成功:

关于我拥有的片段:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.listfragment, container, false);

    //......
    expListView  = (ExpandableListView) v.findViewById(R.id.expandableList);
    final MainActivity mainAct = (MainActivity) getActivity();
    //.......
    expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
        @Override
        public void onGroupExpand(int groupPosition) {
            Log.d("test","Disabled");
            //SwipeRefreshLayout is called swipeRefreshLayout on MainActivity
            mainAct.swipeRefreshLayout.setEnabled(false);

        }
    });

运行后,我可以看到日志消息,所以 swipeRefreshLayout 应该设置为 false,但是 SwipeRefreshLayout 的下拉手势正在工作

【问题讨论】:

  • 同时发布您的活动代码

标签: android android-studio android-fragments expandablelistview swiperefreshlayout


【解决方案1】:

试一试:

来自您的片段(铸造):

((MainActivity) getActivity()).disableSwipe();

现在在你的活动中:

private void disableSwipe(){
    swipeRefreshLayout.setEnabled(false);
}

查看this answer了解更多详情

【讨论】:

  • 这应该可以,但我更喜欢使用delegation
  • @LucaNicoletti,我假设您的委托是指使用接口?为什么接口会比这个解决方案更好?这个看起来很简单,代码行数很少。
  • @Luca Nicoletti 是的,我想说有很多可用的解决方案,但这是简单易用的解决方案
  • 这将解决你的问题@codeKiller,如果你被卡住了,你可以告诉我
  • 是的,接口允许您将函数委托给另一个类,该类具有执行操作所需的一切。这对可用性更好。因为您可能想在其他情况下重新使用片段,而不是附加到MainActivity。建议的解决方案将使您的应用程序崩溃。相反,使用接口,片段可以检查interface 是否不为空,从而调用方法。甚至检查接口的类型是否正确。
猜你喜欢
  • 1970-01-01
  • 2019-05-22
  • 1970-01-01
  • 2018-06-23
  • 1970-01-01
  • 1970-01-01
  • 2021-02-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多