我知道这听起来有点疯狂,但我的平板电脑的recyclerview适配器对于不同的Android版本表现不同。在我的棉花糖平板电脑中它的工作正常,即它只加载屏幕上可见的视图,并仅在滚动时加载剩余的视图。
但是当使用其他Android版本(棒棒糖及以下版本)进行测试时,它似乎在不滚动recyclerview的情况下加载我的数组中的每个视图。并且整个阵列加载4到5次而没有做任何事情。
这会导致我的应用在这些设备中滚动时冻结。
我不认为这是代码的问题,因为它在棉花糖片中非常有效。
所以有人可以告诉我问题可能是什么?
我希望我的问题足够清楚。
以下是我的适配器代码,以防万一。
public class MagazineTOC_Tab_Adapter extends
RecyclerView.Adapter<MagazineTOC_Tab_Adapter.TOCHolder> {
private ArrayList<Category> magazineCategoryList;
private ArrayList<ArticlesItem> magazineArticleList;
private Context mContext;
private AccessPreference mAccessPreference;
private OnArticleclickListener mOnTOCclickListener;
private final int CATEGORY = 301,TYPE_THREE_AD = 601,TYPE_ONE_AD = 401,TYPE_TWO_AD = 501;
public MagazineTOC_Tab_Adapter(ArrayList<ArticlesItem> magazineArticleList, Context mContext) {
this.magazineCategoryList = magazineCategoryList;
this.magazineArticleList = magazineArticleList;
this.mContext = mContext;
this.mOnTOCclickListener = (OnArticleclickListener) mContext;
this.mAccessPreference = new AccessPreference(mContext);
}
@Override
public TOCHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch(viewType)
{
case CATEGORY:
View headerView = LayoutInflater.from(parent.getContext()).inflate(R.layout.rootview_magazine_toc_tab, parent, false);
TOCHolder myHeaderViewHolder = new TOCHolder(headerView, viewType);
Log.d("CreateviewHolder", ""+CATEGORY);
return myHeaderViewHolder;
case TYPE_ONE_AD:
View adView = LayoutInflater.from(parent.getContext()).inflate(R.layout.rootview_toc_ad_one, parent, false);
TOCHolder adViewHolder = new TOCHolder(adView, viewType);
Log.d("CreateviewHolder", ""+TYPE_ONE_AD);
return adViewHolder;
case TYPE_TWO_AD:
View adView2 = LayoutInflater.from(parent.getContext()).inflate(R.layout.rootview_toc_ad_two, parent, false);
TOCHolder adViewHolder2 = new TOCHolder(adView2, viewType);
Log.d("CreateviewHolder", ""+TYPE_TWO_AD);
return adViewHolder2;
case TYPE_THREE_AD:
View adView3 = LayoutInflater.from(parent.getContext()).inflate(R.layout.rootview_toc_ad_three, parent, false);
TOCHolder adViewHolder3 = new TOCHolder(adView3, viewType);
Log.d("CreateviewHolder", ""+TYPE_THREE_AD);
return adViewHolder3;
}
return null;
}
@Override
public void onBindViewHolder(final TOCHolder holder,final int position) {
if (magazineArticleList.get(position).getViewtype() == CATEGORY) {
if(position==0)
{
Glide.get(mContext).clearMemory();
}
String category = magazineArticleList.
get(position).getCategoryDescription();
if (magazineArticleList.get(position).getLockStatus().equalsIgnoreCase("true")
&& ( !mAccessPreference.isLogin()
|| mAccessPreference.isSubscriptionExpired()
|| mAccessPreference.getSubscriberId().equalsIgnoreCase("") )
) {
holder.mLoackImage.setImageResource(R.drawable.ic_lock);
}
else if(magazineArticleList.get(position).getReadStatus().equalsIgnoreCase("yes")){
holder.mLoackImage.setImageResource(R.drawable.tick_gray);
}
else {
holder.mLoackImage.setImageResource(R.drawable.ico_magazine_arrow);
}
if (position == 0) {
holder.mAdViewTab.setVisibility(View.GONE);
holder.mCategoryTitle.setVisibility(View.VISIBLE);
holder.mTOCImage.setVisibility(View.VISIBLE);
holder.subcat_row_title.setVisibility(View.VISIBLE);
holder.subcat_row_title.setText(Html.fromHtml(magazineArticleList.
get(position).getSubCategory()));
} else if (position > 0) {
// loadAd(holder.mAdView);
if (magazineArticleList.get(position).getSubCategory().equalsIgnoreCase("none")) {
holder.subcat_row_title.setVisibility(View.GONE);
} else if (magazineArticleList.get(position).getSubCategory().equalsIgnoreCase(magazineArticleList.get(position - 1).getSubCategory())) {
holder.subcat_row_title.setVisibility(View.GONE);
} else {
holder.subcat_row_title.setVisibility(View.VISIBLE);
holder.subcat_row_title.setText(Html.fromHtml(magazineArticleList.
get(position).getSubCategory()));
}
if (category.equalsIgnoreCase(magazineArticleList.
get(position - 1).getCategoryDescription())) {
holder.mCategoryTitle.setVisibility(View.GONE);
holder.mTOCImage.setVisibility(View.GONE);
holder.mAdViewTab.setVisibility(View.GONE);
} else {
holder.mCategoryTitle.setVisibility(View.VISIBLE);
holder.mTOCImage.setVisibility(View.VISIBLE);
// holder.mAdView.setVisibility(View.VISIBLE);
}
} else {
holder.mCategoryTitle.setVisibility(View.VISIBLE);
holder.mTOCImage.setVisibility(View.VISIBLE);
// holder.mAdView.setVisibility(View.VISIBLE);
}
holder.mCategoryTitle.setText(Html.fromHtml(category));
holder.mTitleHeaderTxt.setText(Html.fromHtml(magazineArticleList.
get(position).getArticleTitle()));
if (magazineArticleList.
get(position).getImagePath() != null)
if (magazineArticleList.
get(position).getImagePath().length() > 0) {
Glide.with(mContext)
.load(magazineArticleList.
get(position).getImagePath())
.diskCacheStrategy(DiskCacheStrategy.ALL)
.placeholder(R.drawable.placeholder)
.listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
Helper.getInstance().Log_error("IMAGE_@", "IMAGE ERROR" + e + " kjhg" + isFirstResource);
holder.mTOCImage.setVisibility(View.GONE);
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
Helper.getInstance().Log_debug("IMAGE_@", "onResourceReady" + isFirstResource);
return false;
}
})
.into(holder.mTOCImage);
} else {
/**
* Video Url Empty
* ***/
holder.mTOCImage.setVisibility(View.GONE);
holder.playImg.setVisibility(View.GONE);
holder.mTOCImage.setImageResource(R.drawable.placeholder_mag);
// holder.playImg.setVisibility(View.VISIBLE);
}
} else {
loadAd(holder.mAdViewTab, holder.adLayout);
Log.d("CheckScroll", "onBindViewHolder: " + magazineArticleList.get(position).getViewtype()+" po " +position);
}
}
@Override
public int getItemViewType(int position) {
Log.d("count", "getItemCount: "+position);
return magazineArticleList.get(position).getViewtype();
}
@Override
public int getItemCount() {
return magazineArticleList.size();
}
public class TOCHolder extends RecyclerView.ViewHolder {
private TextView mTitleHeaderTxt, mTitleContenadtTxt, mCategoryTitle, subcat_row_title;
private ImageView mTOCImage, mLoackImage, gray_arrow, playImg;
private RelativeLayout contentBaseLayout,adLayout;
private PublisherAdView mAdViewTab;
public TOCHolder(View itemView, int viewType) {
super(itemView);
if (viewType == CATEGORY) {
mTitleHeaderTxt = (TextView) itemView.findViewById(R.id.content_row_title);
mCategoryTitle = (TextView) itemView.findViewById(R.id.txt_category);
mTOCImage = (ImageView) itemView.findViewById(R.id.header_row_image);
subcat_row_title = (TextView) itemView.findViewById(R.id.subcat_row_title);
mLoackImage = (ImageView) itemView.findViewById(R.id.content_row_readimage);
gray_arrow = (ImageView) itemView.findViewById(R.id.gray_arrow);
playImg = (ImageView) itemView.findViewById(R.id.play_img);
contentBaseLayout = (RelativeLayout) itemView.findViewById(R.id.content_root_layout);
mAdViewTab = (PublisherAdView) itemView.findViewById(R.id.ad_view);
gray_arrow.setVisibility(View.GONE);
contentBaseLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = getAdapterPosition();
mOnTOCclickListener.onTOCitemClickListener(position, magazineArticleList.get(position).getLockStatus());
}
});
} else {
mAdViewTab = (PublisherAdView) itemView.findViewById(R.id.ad_view);
adLayout = (RelativeLayout) itemView.findViewById(R.id.ad_rootView);
}
}
}
private void loadAd(final PublisherAdView mAdViewTab, final RelativeLayout adLayout) {
try {
if (Helper.isConnected(mContext)) {
if (Helper.isTablet(HomePage.getInstance())) {
// adLayout.setVisibility(View.GONE);
}
// mAdView.setVisibility(View.VISIBLE);
PublisherAdRequest adRequest = new PublisherAdRequest.Builder().build();
mAdViewTab.loadAd(adRequest);
} else {
mAdViewTab.setVisibility(View.GONE);
adLayout.setVisibility(View.GONE);
notifyDataSetChanged();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
请提前帮助,谢谢。
答案 0 :(得分:1)
在我看来,您必须将recycleview的宽度或高度设置为0dp。
这通常在android studio中显示为警告。
检查您的xml文件。如果回收视图有0 dp,请将其更改为wrap_content
或match_parent
希望这会有所帮助。祝你好运
答案 1 :(得分:0)
public void setImage(){
stringList.clear();
String targetPath=
Environment.getExternalStorageDirectory().getAbsolutePath() + "/My Images/";
File targetDirector = new File(targetPath);
File[] files = targetDirector.listFiles();
Arrays.sort( files, new Comparator()
{
public int compare(Object o1, Object o2) {
if (((File)o1).lastModified() > ((File)o2).lastModified()) {
return -1;
} else if (((File)o1).lastModified() < ((File)o2).lastModified()) {
return +1;
} else {
return 0;
}
}
});
for (File file : files){
stringList.add(file.getAbsolutePath());
}
RecyclerView rView = (RecyclerView)findViewById(R.id.recycler_view);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(),2);
rView.setHasFixedSize(true);
rView.setLayoutManager(layoutManager);
RecyclerViewAdapter rcAdapter = new RecyclerViewAdapter(getApplicationContext(), stringList);
rView.setAdapter(rcAdapter);
}