activity 的 "this" 在创建新的 PagerAdapter 时未被识别为回调

activity's "this" not recognized as Callback while creates a new PagerAdapter

我的应用程序有 CustomPagerAdapter,我在其中创建了片段,其中有一个带有 editText 的对话框。主要思想是将这段文字转移到 MainActivity 中。我尝试做 eventBus:dialog->fragment->pagerAdapter->activity。我可以跳过 pagerAdapter,但我想保持所有 "stations" 的顺序。

我在对话框中创建了一个接口,并通过回调将其与片段连接起来。片段和适配器之间也是如此。但是现在我需要在创建适配器时添加回调参数,我收到这个错误

还有我的部分代码:

package com.slavafleer.tipcalculator02;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.ArrayList;

/**
 * PagerAdapter for ManualModeActivity ViewPager
 */
public class DinersPagerAdapter extends FragmentPagerAdapter implements
    DinerFragment.Callbacks {

    private Callbacks mCallbacks;
    private int mDinersAmount;

    public DinersPagerAdapter(FragmentManager fm, int dinersAmount, Callbacks callbacks) {
        super(fm);

        mCallbacks = callbacks;
        mDinersAmount = dinersAmount;
    }

    @Override
    public Fragment getItem(int position) {

            // Insert diners amount to fragment and DinerId
            Bundle bundle = new Bundle();
            bundle.putInt(Constants.KEY_DINNERS_AMOUNT, mDinersAmount);
            bundle.putInt(Constants.KEY_DINER_ID, position);
            DinerFragment dinerFragment = new DinerFragment();
            dinerFragment.setArguments(bundle);

            return dinerFragment;
    }

    // Diners amount + All
    @Override
    public int getCount() {
        return mDinersAmount + 1;
    }

    // DinerFragment.OrderDialog.Callbacks
    @Override
    public void onDialogAddClick(ArrayList<Boolean> selectedIdsList, double orderPrice) {

        mCallbacks.onDialogAddClick(selectedIdsList, orderPrice);
    }

    public interface Callbacks {

        void onDialogAddClick(ArrayList<Boolean> selectedIdsList, double orderPrice);
    }
}

package com.slavafleer.tipcalculator02;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.slavafleer.tipcalculator02.recycler.PageHeaderAdapter;

import java.util.ArrayList;

public class ManualModeActivity extends AppCompatActivity implements
        PageHeaderAdapter.Callbacks, DinerFragment.Callbacks {

    private int mDinersAmount;
    private ViewPager mViewPagerDiners;
    private PageHeaderAdapter mHeaderAdapter;

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

        // Get diners amount from previous activity
        Intent intent = getIntent();
        mDinersAmount = intent.getIntExtra(Constants.KEY_DINNERS_AMOUNT, 1);

        // Initialise PageHeader Recycler
        mHeaderAdapter = new PageHeaderAdapter(this, mDinersAmount, this);
        final RecyclerView recyclerPageHeader = (RecyclerView) findViewById(R.id.recyclerViewPagerHeader);
        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        recyclerPageHeader.setLayoutManager(linearLayoutManager);
        recyclerPageHeader.setAdapter(mHeaderAdapter);

        // Initialise ViewPager
        mViewPagerDiners = (ViewPager) findViewById(R.id.viewPagerDiners);
        FragmentManager fragmentManager = getSupportFragmentManager();
        DinersPagerAdapter dinersPagerAdapter = new DinersPagerAdapter(fragmentManager, mDinersAmount, this);
        mViewPagerDiners.setAdapter(dinersPagerAdapter);

        // ViewPager Listener - synchronise with headers recycler
        mViewPagerDiners.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            // Gets position for selected page
            @Override
            public void onPageSelected(int position) {

                mHeaderAdapter.selectItem(position);
                linearLayoutManager.smoothScrollToPosition(recyclerPageHeader, null,position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    // HeaderPage Adapter Callbacks
    // Scroll ViewPager by clicked Header
    @Override
    public void onItemClick(int position) {
        mViewPagerDiners.setCurrentItem(position, true);
    }

    // DinerFragment.OrderDialog.Callbacks
    // Receive new order from Dialog.
    // Send back to all fragments (via PagerAdapter), where would be added to Recycler
    @Override
    public void onDialogAddClick(ArrayList<Boolean> selectedIdsList, double orderPrice) {

    }
}

感谢您的帮助。

您的 PagerAdapter 正在等待 DinersPagerAdapter.Callbacks 作为第三个参数。 但是您的 activity 没有实现它,因此无法识别 this

将您的 activity 更改为

public class ManualModeActivity extends AppCompatActivity implements
    PageHeaderAdapter.Callbacks, DinerFragment.Callbacks, DinersPagerAdapter.Callbacks {
//Everything else...

//Don't forget to override
@Override
void onDialogAddClick(ArrayList<Boolean> selectedIdsList, double orderPrice) {
//Whatever has to be done here
}