简体   繁体   English

Android上下文操作栏 - 获取RecyclerView位置

[英]Android Contextual Action Bar - Get RecyclerView Position

I followed this tutorial ( http://www.startingandroid.com/how-to-use-sqlite-database-in-android/ ) as I am relatively new to Android development and wanted to get an idea of how SQL works. 我遵循了本教程( http://www.startingandroid.com/how-to-use-sqlite-database-in-android/ ),因为我对Android开发相对较新,并希望了解SQL的工作原理。 As I am looking to use the CardView and RecyclerView for an application that I am developing, that tutorial was very helpful in getting me started. 由于我希望将CardView和RecyclerView用于我正在开发的应用程序,因此该教程对我开始非常有帮助。

However, I have gone further and implemented the 'Contextual Action Bar' to be able to 'Favourite' or 'Delete' selected items (my code for this can be seen below). 但是,我已经进一步实施了“上下文操作栏”,以便能够“收藏”或“删除”所选项目(我的代码可以在下面看到)。 So, my question is, how can I retrieve the position of the selected item and extract the SQL ID from that and how would I go about incorporating multi-select with the CAB? 所以,我的问题是,如何检索所选项目的位置并从中提取SQL ID以及如何将多选与CAB结合使用?

@Override
public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_row, parent, false);
UserViewHolder userViewHolder = new UserViewHolder(v);

v.setClickable(true);

v.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Snackbar.make(v, "CLICK", Snackbar.LENGTH_SHORT).show();
    }
});

v.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        Snackbar.make(v, "LONG CLICK", Snackbar.LENGTH_SHORT).show();
        if (mActionMode != null) {
            return false;
        }
        v.startActionMode(new ActionMode.Callback() {
            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                MenuInflater inflater = mode.getMenuInflater();
                inflater.inflate(R.menu.context, menu);
                return true;
            }

            @Override
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                return false;
            }

            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                //get number of selected items

                switch (item.getItemId()) {
                    case R.id.context_favourite:
                        //SQL - Favourite Item
                        mode.finish();
                    case R.id.context_delete:
                        //SQL - Delete Item
                        mode.finish();
                }
                return false;
            }

            @Override
            public void onDestroyActionMode(ActionMode mode) {
                mActionMode = null;
            }
        });
        return true;
    }
});
 return userViewHolder;
}

I had the same problem, ended up saving the id from the cursor in the viewfinder. 我有同样的问题,最终在取景器中保存光标的id。 Do this in your createviewholder func 在createviewholder func中执行此操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM