创建Recyclerview后调用函数

时间:2017-01-23 18:07:32

标签: android android-recyclerview

我想使用库在我的Cardview列表中插入一个教程。对于这个库,我需要提供一个视图(元素指向)但我的mRecyclerView.findViewHolderForItemId(mAdapter.getItemId(1)).itemView总是返回null。为什么这会返回null?因为还没有创建回收视图?

我也尝试将其替换为:mRecyclerView.findViewHolderForItemId(mAdapter.getItemId(1)).itemView)

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_players);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view_list_player);

        // use this setting to improve performance if you know that changes
        // in content do not change the layout size of the RecyclerView
        mRecyclerView.setHasFixedSize(true);

        // use a linear layout manager
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);

        // specify an adapter (see also next example)
        mDataset = Data.bdd.get_list_players();
        mAdapter = new CardsViewAdapter(mDataset);
        mRecyclerView.setAdapter(mAdapter);

        // Tutorial
        new MaterialIntroView.Builder(this)
                .enableDotAnimation(true)
                .enableIcon(true)
                .setFocusGravity(FocusGravity.CENTER)
                .setFocusType(Focus.MINIMUM)
                .setDelayMillis(500)
                .enableFadeAnimation(true)
                .setInfoText("Hi There! Click this card and see what happens.")
                .setShape(ShapeType.RECTANGLE)
                .setTarget(mRecyclerView.findViewHolderForItemId(mAdapter.getItemId(1)))
                .show();

}

2 个答案:

答案 0 :(得分:0)

我认为您的CardsViewAdapter库使用默认的getItemId(返回-1)。您可以改用RecyclerView.getChild()。

答案 1 :(得分:0)

对我来说最简单的方法就是像这样修改我的构建器并将我的函数直接放在我的适配器

CardsViewAdapter(List<Player> myDataset, Activity activity) {
        mDataset = myDataset;
        mActivity = activity;
    }