java - android 中的适配器使用相同的重写方法

标签 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;
    }


}

最佳答案

不能让所有适配器扩展同一个类吗?

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
}

等等...

关于java - android 中的适配器使用相同的重写方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43895033/

相关文章:

Android WebChromeClient WebView onGeolocationPermissionsShowPrompt 从未触发

java - PublishResult 中的 ConcurrentModificationException - ArrayAdapter

java - 跨多个客户端和服务器生成真正全局唯一的 ID

java - 使用 Jsoup 从 HTML 中选择 <p>

java - ORMLite 外部字段类没有 id 字段

android - 什么是 intent-filter 而不是在共享 url 时仅在共享菜单中显示应用程序?

java - 为什么我们需要在同一个对象上同步 notify() 才能工作

java - 如何修复简单 GET 回调中的 "No Retrofit annotation found."?

android - Gson会忽略序列化时枚举的自定义@SerializedName值

android - 解析 xml 时出错 : Unbound prefix with the Facebook SDK