从 Fragment 禁用 MainActivity SwipeRefreshLayout

Disable MainActivity SwipeRefreshLayout from Fragment

我有一个主要 activity,其中包括一个 SwipeRefreshLayout。 activity 包含 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 正在运行

试试看:

来自您的片段(铸造):

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

现在在您的 activity 中:

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

查看this answer了解更多详情