我正在使用 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/