我已经在我的ListView中实现了原生广告,它运行正常但每次重点都会重新加载。有什么方法可以保存实例或视图,所以它不会每次都重新加载。或者我可以阻止视图的重新创建吗?
class CustomAdapter extends BaseAdapter {
String content, currentFile, pv;
@Override
public int getCount() {
return fileList.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (i == 0) {
if (isNetworkAvailable()) {
// ad loads here
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.native_ad_item, null);
final CardView cardView = (CardView) view.findViewById(R.id.card_view);
NativeExpressAdView adView = (NativeExpressAdView) view.findViewById(R.id.nativeAd);
AdRequest request = new AdRequest.Builder().addTestDevice("264647BDFDDB6FBB0F34C797D5D53A4D").build();
adView.loadAd(request);
}
} else {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.purchased_list_item, null);
// rest of the code
}
return view;
}
答案 0 :(得分:0)
ListView重用在getView()中创建的子视图,并将它们作为该方法的第二个参数(实际上称为 convertView )发送。您可以检查该视图是否为空并使用它而不是每次都创建一个新视图。 您可以在此问题的答案中查看更多详细信息: What is the purpose of `convertView` in ListView adapter?