Recyclerview findViewHolderForAdapterPosition在滚动到新位置后返回null

时间:2015-08-13 12:56:32

标签: android-recyclerview

RecyclerView recyclerView;
MyAdapter mAdapter;
List<ItemData> itemsData;
Global glb;
LinearLayoutManager llm;

private int valData() {
    String tmpStr;
    Cursor c;
    int position=0;
enter code here
    AutoCompleteTextView tTbl = (AutoCompleteTextView) findViewById(R.id.tblNam);
    tmpStr = tTbl.getText().toString();
    c = db.rawQuery("SELECT * FROM tblDts WHERE tblNam = '" + tmpStr + "'", null);
    if (c.getCount() == 0) {
        showMessage("Error", "Table Name Not Found");
        c.close();
        tTbl.requestFocus();
        return 1;
    }
    c.close();

    for (int i = 0; i < itemsData.size(); i++) {
        tmpStr = itemsData.get(i).getTitle().toString();
        c = db.rawQuery("SELECT * FROM itmDts WHERE itmNam = '" + tmpStr + "';", null);
        if (c.getCount() == 0) {

            c.close();

            //llm.scrollToPositionWithOffset(i,0);
            //recyclerView.smoothScrollToPosition(i);
            //recyclerView.scrollToPosition(i);

我尝试了以上所有的滚动程序,当我使用getChildAt时它只返回前滚动的视图,而不是新的滚动

            //View vw = llm.getChildAt(0);
            tmpStr="";

当在当前显示视图之间上升findviewholderforadapterpostion时,它已正确显示但如果我上升非显示位置则返回null                 RecyclerView.ViewHolder vh = recyclerView.findViewHolderForAdapterPosition(i);                 查看vw = vh.itemView;

            AutoCompleteTextView tTa=(AutoCompleteTextView)vw.findViewById(R.id.item_title);
            tmpStr =tTa.getText().toString();


            showMessage(llm.getChildCount()+"Error" + position, "Item Name Not Found" + tmpStr);

            //recyclerView.setLayoutManager(lm);


            return 1;
        }
        c.close();

    }
    return 0;
}


   }
}

1 个答案:

答案 0 :(得分:-1)

我总是在Android平台上遇到这类问题。我的大多数问题都是通过线程休眠修复的:)这不是一个正确的方法,但是找不到任何其他方法。滚动下一个项目后,您需要添加100毫秒的睡眠时间。

以下代码适用于Xamarin.Android。

public async static void FocusNextItem(Android.Support.V7.Widget.RecyclerView recyclerView, int nextPosition)
    {
        recyclerView.ScrollToPosition(nextPosition);

        var viewHolder = recyclerView.FindViewHolderForAdapterPosition(nextPosition);
        if (viewHolder == null)
        {
            await System.Threading.Tasks.Task.Delay(100);

            viewHolder = recyclerView.FindViewHolderForAdapterPosition(nextPosition);
        }
        viewHolder?.ItemView?.RequestFocus();
    }