来自MainActivity的Fragment中的调用方法

时间:2018-12-21 18:53:14

标签: android android-studio android-fragments android-recyclerview android-tablayout

我的应用程序由包含MainActivity的{​​{1}}组成。第二个选项卡具有TabLayout,可通过单击选择项目。初始状态如左图所示。但是,当选择一个或多个项目时,将显示一个叠加的RecyclerView,并带有(从左到右)复制,编辑,删除或取消选择这些项目的选项。中间图显示了这种情况。

例如,当选择取消选择选项时,Toolbar应该隐藏,并且应该取消选择项目。问题是我不知道如何让Toolbar中的ListAdapter知道应该取消选择那些项目。  选择取消选择选项的当前结果如右图所示,因此RecyclerView隐藏了,但是没有取消选择项目。

enter image description here

困难之处在于Toolbar在第二个片段中,称为RecyclerView,而带有选项的Fragment2Toolbar布局的一部分。 MainActivity包含方法Fragment2。此方法可以正常工作,但无法从deselect调用它。

为解决这个问题,我尝试使用界面,但没有成功。第二种方法在下面提供的代码中实现。我试图在MainActivity方法中调用deselect,但是它告诉我onOptionsItemSelected。我也曾尝试按照Dimitar Darazhanski的建议进行操作,但无法弄清楚在何处给fragment2 == null标签以及在我的情况下使用哪个容器ID。

如果您知道如何通过修改此方法或使用完全不同的方法来解决此问题,请告诉我!

MainActivity

fragment2

Fragment2

import android.animation.Animator;
import android.content.Intent;
import android.graphics.PorterDuff;
import android.os.Build;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    ViewPager mViewPager;
    TabLayout tabLayout;
    Integer currentTab, fragmentID;
    boolean itemsSelected;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        mViewPager = findViewById(R.id.viewpager_main_activity);
        tabLayout = findViewById(R.id.tablayout_main_activity);
        tabLayout.setupWithViewPager(mViewPager);
        itemsSelected = false;

        displayDefaultAppbar();
        handleFragments(mViewPager);
        currentTab = handleStartupFragment(mViewPager, savedInstanceState);
    }

    private void displayDefaultAppbar() {
        final Toolbar toolbar = findViewById(R.id.toolbar_main_activity);
        TabLayout tabLayout = findViewById(R.id.tablayout_main_activity);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setTitle(getResources().getString(R.string.app_name));
        }
        toolbar.setVisibility(View.VISIBLE);
        tabLayout.setVisibility(View.VISIBLE);
    }

    private void handleFragments(ViewPager mViewPager) {
        SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mSectionsPagerAdapter);
        tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager) {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                super.onTabSelected(tab);
                int tabIconColor = ContextCompat.getColor(MainActivity.this, android.R.color.holo_red_dark);
                if(tab.getIcon() != null) {
                    tab.getIcon().setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN);
                }
                currentTab = tab.getPosition();
                handleToolbarOptions(currentTab);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                super.onTabUnselected(tab);
                int tabIconColor = ContextCompat.getColor(MainActivity.this, android.R.color.black);
                if(tab.getIcon() != null) {
                    tab.getIcon().setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN);
                }
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                super.onTabReselected(tab);
            }
        });
    }

    private Integer handleStartupFragment(ViewPager mViewPager, Bundle savedInstanceState) {
        Intent intent = getIntent();
        Integer startupFragment;
        tabLayout.getTabAt(0).setText("1");
        tabLayout.getTabAt(1).setText("2");
        tabLayout.getTabAt(2).setText("3");
        if(intent.hasExtra("initialFragment")){
            Bundle bd = getIntent().getExtras();
            startupFragment = bd.getInt("initialFragment");
        } else {
            startupFragment = 1;
        }
        if (savedInstanceState == null) {
            mViewPager.setCurrentItem(startupFragment);
        }
        return startupFragment;
    }

    @Override
    public void onAttachFragment(Fragment fragment) {
        super.onAttachFragment(fragment);
        fragmentID = fragment.getId();
    }

    private void handleToolbarOptions(Integer currentTab) {
        Toolbar toolbar = findViewById(R.id.toolbar_main_activity);
        if (currentTab == 1) {
            toolbar.getMenu().clear();
            toolbar.inflateMenu(R.menu.fragment2);
        } else {
            toolbar.getMenu().clear();
            toolbar.inflateMenu(R.menu.other);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        Toolbar toolbar = findViewById(R.id.toolbar_main_activity_selected);
        if (toolbar.isShown()) {
            getMenuInflater().inflate(R.menu.selected, menu);
        } else if (currentTab == 1) {
            getMenuInflater().inflate(R.menu.fragment2, menu);
        } else {
            getMenuInflater().inflate(R.menu.other, menu);
        }
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.selected_close) {
            if (findViewById(R.id.toolbar_main_activity_selected) != null) {
                hideSelectedToolbar();
                Fragment2 fragment2 = (Fragment2) getSupportFragmentManager().findFragmentById(R.id.tablayout_main_activity);
                if(fragment2 == null) {
                    Toast.makeText(this,"null",Toast.LENGTH_SHORT).show();
                } else if (!fragment2.isAdded()) {
                    Toast.makeText(this,"Not added",Toast.LENGTH_SHORT).show();
                } else {
                    fragment2.deselect();
                }
            }
            return true;
        } else {
            return super.onOptionsItemSelected(item);
        }
    }

    public void displaySelectedToolbar() {
        final Toolbar toolbar = findViewById(R.id.toolbar_main_activity_selected);
        toolbar.setVisibility(View.VISIBLE);
        if (Build.VERSION.SDK_INT >= 21) {
            toolbar.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
                @Override
                public void onLayoutChange(View view, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                    Animator animator = ViewAnimationUtils.createCircularReveal(toolbar, toolbar.getWidth(), 0, 0,toolbar.getWidth());
                    animator.setDuration(ANIMATION_DURATION);
                    toolbar.removeOnLayoutChangeListener(this);
                    animator.start();
                }
            });
        }
        toolbar.setTitleTextColor(ContextCompat.getColor(MainActivity.this, android.R.color.black));
        setSupportActionBar(toolbar);
    }

    public void hideSelectedToolbar() {
        final Toolbar toolbar = findViewById(R.id.toolbar_main_activity_selected);
        if (Build.VERSION.SDK_INT >= 21) {
            Animator animator = ViewAnimationUtils.createCircularReveal(toolbar, toolbar.getWidth(), 0, toolbar.getWidth(), 0);
            animator.setDuration(ANIMATION_DURATION);
            animator.start();
            animator.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animator) {

                }

                @Override
                public void onAnimationEnd(Animator animator) {
                    toolbar.setVisibility(View.GONE);
                    displayDefaultAppbar();
                }

                @Override
                public void onAnimationCancel(Animator animator) {

                }

                @Override
                public void onAnimationRepeat(Animator animator) {

                }
            });
        } else {
            toolbar.setVisibility(View.GONE);
            displayDefaultAppbar();
        }
    }

    private class SectionsPagerAdapter extends FragmentPagerAdapter {

        private SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return new Fragment1();
                case 1:
                    return new Fragment2();
                case 2:
                    return new Fragment3();
                default:
                    return null;
            }
        }

        @Override
        public int getCount() {
            return 3;
        }
    }
}

activity_main.xml

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import java.util.ArrayList;

public class Fragment2 extends Fragment {

    ListAdapter adapter;
    RecyclerView rv;
    ArrayList<TestItem> list;
    Integer selected, selectedOld;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment2, container, false);

        selected = 0;
        selectedOld = 0;

        list = new ArrayList<>();
        list.add(new TestItem("Item 1",false));
        list.add(new TestItem("Item 2",false));
        list.add(new TestItem("Item 3",false));
        list.add(new TestItem("Item 4",false));
        list.add(new TestItem("Item 5",false));
        list.add(new TestItem("Item 6",false));

        rv = rootView.findViewById(R.id.rv_fragment2);
        adapter = new ListAdapter(getActivity());
        rv.setAdapter(adapter);
        rv.setLayoutManager(new LinearLayoutManager(getActivity()));

        return rootView;
    }

    private void handleToolbar() {
        if (selected == 1 && selectedOld == 0) {
            if (getActivity() != null) {
                ((MainActivity) getActivity()).displaySelectedToolbar();
            }
        } else if (selected == 0 && selectedOld == 1) {
            if (getActivity() != null) {
                ((MainActivity) getActivity()).hideSelectedToolbar();
            }
        }
    }

    public void deselect() {
        for (int i=0; i<list.size(); i++) {
            list.get(i).setSelected(false);
        }
        adapter.notifyDataSetChanged();
        rv.setAdapter(adapter);
    }
}

fragment2.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbarlayout_main_activity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar_main_activity"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:layout_scrollFlags="scroll|enterAlways|snap"
            android:background="?attr/colorPrimary"
            android:visibility="visible"/>

        <android.support.design.widget.TabLayout
            android:id="@+id/tablayout_main_activity"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager_main_activity"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_main_activity_selected"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorAccent"
        android:translationZ="4dp"
        android:visibility="invisible"/>

</android.support.design.widget.CoordinatorLayout>

2 个答案:

答案 0 :(得分:1)

您可以使用的一种解决方案是LocalBroadcastManager,就像您在this question.中看到的那样

Fragment2

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // you will be notified here, you can do your stuff(deselect) here
            String message = intent.getStringExtra("my_message");
            Log.d("receiver_message", "my_message = " + message);
        }
    };

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment2, container, false);

        LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mMessageReceiver,
                new IntentFilter("my_event_tag"));

        return rootView;

    }

    @Override
    public void onDestroy() {
        LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mMessageReceiver);
        super.onDestroy();
    }

MainActivity

private void callDeselectFromMainActivity() {
    Intent intent = new Intent("my_event_tag");
    intent.putExtra("my_message", "This is text should go to fragment...");
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}

当您从 MainActivity调用 callDeselectFromMainActivity 方法时。应在 Fragment2中调用onReceive方法。

答案 1 :(得分:0)

您可以这样添加课程:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.ViewGroup;

public class MainTabsPagerAdapter extends FragmentStatePagerAdapter {

    int mNoOfTabs;

    private OneFragment oneFragment;
    private TwoFragment twoFragment;
    private ThreeFragment threeFragment;

    public MainTabsPagerAdapter(FragmentManager fm, int numberOfTabs) {
        super(fm);
        mNoOfTabs = numberOfTabs;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new oneFragment();
            case 1:
                return new twoFragment();
            case 2:
                return new threeFragment();
        }
        return null;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Fragment createdFragment = (Fragment) super.instantiateItem(container, position);
        switch(position){
            case 0:
                oneFragment = (OneFragment) createdFragment;
                break;
            case(1):
                twoFragment = (TwoFragment) createdFragment;
                break;
            case(2):
                threeFragment = (ThreeFragment) createdFragment;
                break;
        }
        return createdFragment;
    }

    public OneFragment getOneFragment() {
        return oneFragment;
    }

    public TwoFragment getTwoFragment() {
        return twoFragment;
    }

    public ThreeFragment getThreeFragment() {
        return threeFragment;
    }

    @Override
    public int getCount() {
        return mNoOfTabs;
    }
}

之后,添加您的MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    final String ONE_FRAGMENT = getString(R.string.one_fragment);
    final String TWO_FRAGMENT = getString(R.string.two_fragment);
    final String THREE_FRAGMENT = getString(R.string.three_fragment);

    mainTabLayout.addTab(mainTabLayout.newTab().setText(ONE_FRAGMENT));
    mainTabLayout.addTab(mainTabLayout.newTab().setText(TWO_FRAGMENT));
    mainTabLayout.addTab(mainTabLayout.newTab().setText(THREE_FRAGMENT));

    tabsPagerAdapter = new MainTabsPagerAdapter(getSupportFragmentManager(), mainTabLayout.getTabCount());
    mainViewPager.setAdapter(tabsPagerAdapter);
    mainViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mainTabLayout));
    mainTabLayout.addOnTabSelectedListener(this);
}

@Override
public void onTabSelected(TabLayout.Tab tab) {
    mainViewPager.setCurrentItem(tab.getPosition());
    mainToolbar.setTitle(firstCharToUpperCase(String.valueOf(tab.getText())));
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {

}

@Override
public void onTabReselected(TabLayout.Tab tab) {

}

如果要获取例如OneFragment,可以致电:

OneFragment oneFragment = tabsPagerAdapter.getOneFragment();
oneFragment.doSomething();