使用fragment中的接口无法在mainactivity中实现片段列表器

时间:2017-03-05 20:16:55

标签: android android-studio android-fragments interface

我在片段中创建了一个接口,我希望将值传递给mainactivity,但问题是我无法实现它的监听器,我不知道为什么?

帮我解决这个问题。提前谢谢你

片段类:

public class Business extends Fragment {

    public List<StringList> businessNews = new ArrayList<>();
    TransferValue SendData;
    private RecyclerView recyclerView;

    public Business() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_business, container, false);
        recyclerView = (RecyclerView) view.findViewById(R.id.business_recycler_view);

        FetchLists f = new FetchLists();
        f.execute(10, 0);
        return view;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        try {
            SendData = (TransferValue) context;
        } catch (ClassCastException e) {
            Log.d("ashu", "implement the methods");
            throw new ClassCastException("implemented the methods");
        }
    }

    public interface TransferValue {

        public void sendValue(StringList value, int positionValue);
    }
}

Mainactivity:

    public class MainActivity extends AppCompatActivity
            implements NavigationView.OnNavigationItemSelectedListener implements Business.TransferValue //error
{

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


            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);

            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                    this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
            drawer.setDrawerListener(toggle);
            toggle.syncState();

            NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
            navigationView.setNavigationItemSelectedListener(this);
        }

        @Override
        public void onBackPressed() {
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            if (drawer.isDrawerOpen(GravityCompat.START)) {
                drawer.closeDrawer(GravityCompat.START);
            } else {
                super.onBackPressed();
            }
        }



        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();

            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }

            return super.onOptionsItemSelected(item);
        }


            FragmentManager frag = getSupportFragmentManager();

            if (id == R.id.nav_sport) {
                frag.beginTransaction().replace(R.id.content_frame, new Sport()).commit();

            } else if (id == R.id.nav_entertainment) {
                frag.beginTransaction().replace(R.id.content_frame, new Entertainment()).commit();
            } else if (id == R.id.nav_general) {
                frag.beginTransaction().replace(R.id.content_frame, new General()).commit();
            } else if (id == R.id.nav_business) {
                frag.beginTransaction().replace(R.id.content_frame, new Business()).commit();
            } else if (id == R.id.nav_techno) {
                frag.beginTransaction().replace(R.id.content_frame, new Technology()).commit();
            }


        }
    }

1 个答案:

答案 0 :(得分:0)

而不是:

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener implements Business.TransferValue //error
{

使用此方法,因为多个接口只需要逗号作为分隔符,而不需要多个实现:

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener, Business.TransferValue
{