我已經閱讀了幾個關於stackoverflow解決這個問題的問題,但是我無法解決我的問題。 recyclerview顯示網頁瀏覽。我的問題是,適配器,最初,加載正確的價值觀(下圖):RecyclerView適配器在滾動期間採取錯誤的值
但是當我向下滾動,並且再次滾動到第一位置,錯誤值顯示在網頁視圖(如下圖):
我覺得這個問題可以從以前開始加載源自到web視圖,但我沒有怎樣處理這個問題的任何想法。這裏是我的代碼的相關部分:
/* in my notation, pg is equivalent to page.*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_ebook_list, container, false);
mEbookRecyclerView = (RecyclerView) view.findViewById(R.id.ebook_recycler_view);
mLayoutManager = new LinearLayoutManager(getActivity());
mEbookRecyclerView.setLayoutManager(mLayoutManager);
updateUI();
return view;
}
@Override
public void onResume() {
super.onResume();
updateUI();
}
private void updateUI() {
if (mAdapter == null) {
EbookLab ebookLab = EbookLab.get(getActivity());
List<String> pgs = ebookLab.getPgs();
mAdapter = new EbookAdapter(pgs);
mEbookRecyclerView.setAdapter(mAdapter);
}
}
/* ******codes for preparation of menus which I have ignored *******/
private class EbookHolder extends RecyclerView.ViewHolder {
private String mPg;
public EbookHolder(LayoutInflater inflater, ViewGroup container) {
super(inflater.inflate(R.layout.list_item_ebook, container, false));
mListWebView = (WebView) itemView.findViewById(R.id.list_web_View);
mListWebView.getSettings().setLoadWithOverviewMode(true);
}
public void bindEbook(String pg) {
mPg = pg;
mListWebView.loadDataWithBaseURL("file:///android_asset/", "Stack Over Flow " + String.valueOf(glbPos), "text/html", "UTF-8", null);
}
}
private class EbookAdapter extends RecyclerView.Adapter<EbookHolder> {
private List<String> mPgs;
public EbookAdapter(List<String> pgs) {
mPgs = pgs;
}
@Override
public EbookHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
return new EbookHolder(layoutInflater, parent);
}
@Override
public void onBindViewHolder(EbookHolder holder, int position) {
String pg = mPgs.get(position);
glbPos = position;//A global variable for position
holder.bindEbook(pg);
}
@Override
public int getItemCount() {
return mPgs.size();
}
public void setPgs(List<String> pgs) {
mPgs.clear();
mPgs.addAll(pgs);
notifyDataSetChanged();
}
}
任何幫助表示讚賞。
更新:在我的代碼中,我將mListWebView定義爲全局變量。但它應該在private class EbookHolder extends RecyclerView.ViewHolder
內定義。 然後我刪除bindEbook(String pg)
方法,直接更新mListWevView
內public void onBindViewHolder(EbookHolder holder, int position)
:
holder.mListWebView.loadDataWithBaseURL("file:///android_asset/", "Stack Over Flow " + String.valueOf(glbPos), "text/html", "UTF-8", null);
我的問題>>是否調用bindEbook(String pg)
相比,滾動recyclerview或它關係到Android的架構需要更長的時間?
您viewholder是錯誤的 –
@ war_Hero:你能不能給我更多的細節? –
發佈您的適配器生病的解釋,只是作爲一個JIST在視圖內的視圖內部被用來維持位置 –