【问题标题】:Can't implement fragment listner in the mainactivity using interface in fragment无法使用片段中的接口在主要活动中实现片段侦听器
【发布时间】:2017-07-25 14:30:59
【问题描述】:

我在片段中创建了一个接口,我想将值从该接口传输到 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);
    }
}

主要活动:

    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();
            }


        }
    }

【问题讨论】:

    标签: android android-studio android-fragments interface


    【解决方案1】:

    而不是这个:

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

    使用这种方法,因为多个接口需要唯一的逗号作为分隔符,而不是多个实现:

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

    【讨论】:

    • 感谢您的帮助!我之前从未实现过两个监听器
    猜你喜欢
    • 1970-01-01
    • 2018-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-19
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多