我有一个带Tablayout的主活动,其中包含3个片段。主要活动中的FAB位于所有片段的顶部,作为我的滑动。我需要创建一个界面,以便在单击FAB时,我的ViewPager会收到警报并执行某个操作。有谁知道如何创建这个监听器?
监听器应该只返回一个布尔值,表示点击了Fab。
我在主要活动中添加了一个界面,但我不确定如何利用它并将其连接到我的ViewPagerAdapter类?
这是我的主要活动:
public class MainActivity extends AppCompatActivity {
private ViewPager viewPager;
private ArrayList<Fragment> fragments = new ArrayList<>();
private ArrayList<String> titles = new ArrayList<>();
//MY LISTENER....
private static SwitchFragmentsListener switchFragsListener;
public static interface SwitchFragmentsListener{
public void doISwitch(boolean change);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout)findViewById(R.id.collapsing_toolbar);
setSupportActionBar(toolbar);
fragments.addAll(Arrays.asList(new Tasks(),new Calendar(),new Contacts()));
titles.addAll(Arrays.asList(getResources().getString(R.string.fragment_task_title),
getResources().getString(R.string.fragment_cal_title),getResources().getString(R.string.fragment_contacts_title)));
TabLayout tabLayout = (TabLayout)findViewById(R.id.tabs);
final FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab);
fab.hide();
viewPager = (ViewPager)findViewById(R.id.container);
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(),fragments,titles);
viewPager.setAdapter(viewPagerAdapter);
viewPager.setCurrentItem(1);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (position == 1) {
fab.hide();
}else {
fab.show();
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
tabLayout.setupWithViewPager(viewPager);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fabAction();
}
});
if (savedInstanceState==null){
if (getSupportActionBar()!=null){
getSupportActionBar().setDisplayShowTitleEnabled(true);
}
}
}
public interface FragChangeListener{
public void doIChange(boolean change);
}
public void fabAction(){
int current = viewPager.getCurrentItem();
switch (current){
case 0:{
//NEED TO IMPLEMENT LISTENER HERE
break;
}
case 2:{
Toast.makeText(this, "IN TAB 3", Toast.LENGTH_LONG).show();
break;
}
}
}}
和我的ViewPagerAdapter
public class ViewPagerAdapter extends FragmentPagerAdapter {
ArrayList<Fragment> fragments;
ArrayList<String> titles;
private Fragment mFragmentAtPos0;
private FragmentManager fragManager;
//NEED TO IMPLEMENT THE LISTENER IN THIS CLASS TELLING ME THAT THE BUTTON WAS CLICKED IN MAIN ACTIVITY
public ViewPagerAdapter(FragmentManager fm, ArrayList<Fragment> fragments, ArrayList<String>titles) {
super(fm);
this.fragments = fragments;
this.titles = titles;
fragManager = fm;
}
@Override
public Fragment getItem(int position) {
if (position == 0)
{
mFragmentAtPos0 = new AddContact();
fragManager.beginTransaction().replace(R.id.container,mFragmentAtPos0).commit();
notifyDataSetChanged();
return mFragmentAtPos0;
}else{
return fragments.get(position);
}
}
@Override
public int getCount() {
return fragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
return titles.get(position);
}
@Override
public int getItemPosition(Object object)
{
if (object instanceof Tasks && mFragmentAtPos0 instanceof AddContact)
return POSITION_NONE;
return POSITION_UNCHANGED;
}
}
答案 0 :(得分:0)
单击FAB时,我的ViewPager会收到警报并执行 某些行动
我认为在这种情况下你不需要创建监听器。您可以检测单击FAB并将其直接传递给适配器。在你的适配器中,只需创建一个方法来获取FAB的动作。
在ViewPagerAdapter
中,创建方法
public void clickFAB() {
// your code here
}
并在SwitchFragmentsListener
中,只需拨打电话:
ViewPagerAdapter viewPagerAdapter;
public void fabAction(){
int current = viewPager.getCurrentItem();
switch (current){
case 0:{
//NEED TO IMPLEMENT LISTENER HERE
viewPagerAdapter.clickFAB();
break;
}
case 2:{
Toast.makeText(this, "IN TAB 3", Toast.LENGTH_LONG).show();
break;
}
}
}}