android中的适配器,它使用相同的overriden方法

时间:2017-05-10 14:12:52

标签: java android

我正在使用switch case,其中我设置的适配器具有相同的重写方法和重复代码,所以我想最小化它。

private void settingAdapter(int position) {
    switch (position) {
        case 0:
            adapter = new SongAdapter(getContext(), false, false)
            {
                @Override
                protected void onOverScrolled() {
                    super.onOverScrolled();
                    if(nextPageCount==null||nextPageCount.isEmpty())
                    {
                        return;
                    }
                    else {
                        int count = Integer.parseInt(nextPageCount);
                        getList(count);
                    }
                }
            };
            customList.setAdapter(adapter);
            break;
        case 1:
            adapter = new ArtistAdapter(getContext(), false, false, false){
                @Override
                protected void onOverScrolled() {
                    super.onOverScrolled();
                    if(nextPageCount==null||nextPageCount.isEmpty())
                    {
                        return;
                    }
                    else {
                        int count = Integer.parseInt(nextPageCount);
                        getList(count);
                    }
                }
            };
            customList.setAdapter(adapter);
            break;
        case 2:
            adapter = new AlbumAdapter(getContext(), false, false){
                @Override
                protected void onOverScrolled() {
                    super.onOverScrolled();
                    if(nextPageCount==null||nextPageCount.isEmpty())
                    {
                        return;
                    }
                    else {
                        int count = Integer.parseInt(nextPageCount);
                        getList(count);
                    }
                }
            };
            customList.setAdapter(adapter);
            break;
        case 3:
            adapter = new PlaylistAdapter(getContext(), false, false){
                @Override
                protected void onOverScrolled() {
                    super.onOverScrolled();
                    if(nextPageCount==null||nextPageCount.isEmpty())
                    {
                        return;
                    }
                    else {
                        int count = Integer.parseInt(nextPageCount);
                        getList(count);
                    }
                }
            };
            customList.setAdapter(adapter);
            break;
    }


}

2 个答案:

答案 0 :(得分:1)

你不能让所有Adapters扩展到同一个班级吗?

public class BaseAdapter extends SomeAdapterOfYours {
    //... everything needed

    @Override
    protected void onOverScrolled() {
        super.onOverScrolled();
        if(nextPageCount==null||nextPageCount.isEmpty()){
            return;
        }
        else {
            int count = Integer.parseInt(nextPageCount);
            getList(count);
        }
    } 
}

然后是您的自定义适配器:

public class ArtistAdapter extends BaseAdapter {
  //custom adapter stuff
}

public class SongAdapter extends BaseAdapter {
  //custom adapter stuff
}

依旧......

答案 1 :(得分:0)

如果您使用的是同一个适配器,最好的策略是在新文件的类中创建一个CustomAdapter适配器,将其导入您的Activity并在每次需要时创建一个实例。

如果您不想这样做,可以在未设置适配器的情况下控制案例,并在default:设置它。你只需要一个适配器。