を通じて、私はitemview
を取得するためにLayoutManager.ChildAt(position)
を呼び出して、私は取得ビューがマッチしitemview
ではない、と私はnotifyItemChanged(position)
を呼び出すときに、アプリのクラッシュ:はRecyclerView.Adapter GET ItemView私のアダプタで位置
E/AndroidRuntime: FATAL EXCEPTION: mainProcess: com.paicaifu.riches, PID: 8502
Java.lang.IllegalArgumentException: Called attach on a child which is not detached: ViewHolder{adb21588 position=0 id=-1, oldPos=-1, pLpos:-1}
at Android.support.v7.widget.RecyclerView$5.attachViewToParent(RecyclerView.java:654)
at android.support.v7.widget.ChildHelper.attachViewToParent(ChildHelper.java:239)
at android.support.v7.widget.RecyclerView.addAnimatingView(RecyclerView.java:1107)
at android.support.v7.widget.RecyclerView.animateChange(RecyclerView.java:3270)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep3(RecyclerView.java:3088)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2917)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3283)
…
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
感謝、これはアダプタで私のコードです:私はちょうど最後の項目を見つけて、それが状態(オープンまたはクローズ)です変更したい
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder,
final int position) {
if (holder instanceof CardHolder) {
final CardHolder cardHolder = (CardHolder) holder;
cardHolder.rootview.setOnClickListener(this);
cardHolder.rootview.setTag(position);
cardHolder.rootview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//get last itemview in adapter,but preView isnot match the
//position,it is the visibable views on screen
View preView = mLinearLayoutManager.getChildAt(preIndex);
//change the datasource
mData.getResult().getCardList().get(preIndex).setOpen(false);
//update item
notifyItemChanged(preIndex);//when the item is out of screen,
//this line will cause crashes
preIndex = position;
}
});
}
項目が画面の外にあるとき、notifyItemChanged(int型の位置)は、問題を表示します。
こんにちは、この質問はかなり広いです。問題のアダプター、ビューホルダー、アクティビティ/フラグメントの領域に関連コードを入れてください。 – BIW
あなたの助けてくれてありがとう、私の貧しい英語とフォーマットされていないコードを許してください、私はこれらについて進歩します – Bayin