如何获得cardView项目的recyclerView的位置? [英] How to get the position of cardView item in recyclerView?

查看:441
本文介绍了如何获得cardView项目的recyclerView的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个适配器类的回收者进行填充cardview布局recyclerview。它工作正常,但是当我点击recyclerview cardView项目,我需要得到那cardview项目的位置,根据我需要写一个意图的活动。随着怎么删除cardview项目时,我刷卡左到右。 MyAdapter 类。

 公共类MyAdapter扩展RecyclerView.Adapter< MyAdapter.ViewHolder> {
    私人NavigationItem [] navigationItem;
    私人语境mContext;

    公共MyAdapter(NavigationItem [] navigationItem){
        this.navigationItem = navigationItem;
    }

    @覆盖
    公共MyAdapter.ViewHolder onCreateViewHolder(ViewGroup中的父母,INT viewType){
        查看itemLayoutView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.navigation_row_cardview,NULL);
        ViewHolder viewHolder =新ViewHolder(itemLayoutView);
        返回viewHolder;
    }

    @覆盖
    公共无效onBindViewHolder(MyAdapter.ViewHolder持有人,INT位置){
        holder.txtViewTitle.setText(navigationItem [位置] .getTitle());
    }

    @覆盖
    公众诠释getItemCount(){
        返回navigationItem.length;
    }

    公共类ViewHolder扩展RecyclerView.ViewHolder {
        @InjectView(R.id.hambergerRecyclerView)
        公共RelativeLayout的recyclerView;
        公众的TextView txtViewTitle;

        公共ViewHolder(最终查看itemLayoutView){
            超(itemLayoutView);
            ButterKnife.inject(这一点,itemLayoutView);
            txtViewTitle =(TextView中)itemLayoutView.findViewById(R.id.navigationItemOptionName);

            itemLayoutView.setOnClickListener(新View.OnClickListener(){
                @覆盖
                公共无效的onClick(视图查看){
                    // INT itemPosition = RecyclerView.getChildPosition(视图);
                    INT itemPosition = recyclerView.getChildAdapterPosition(视图);
                    字符串项= navigationItem.get(itemPosition);
                    Toast.makeText(mContext,项目,Toast.LENGTH_LONG).show();
                }
            });
        }
    }
}
 

解决方案

简答

  1. 获取CardView项目的位置

      

    当我点击recyclerview cardView项目,我需要得到的位置   这car​​dview项目

    使用 setTag()在适配器的onBindViewHolder方法和 getTag()在OnClickListener的实现。

    您也可以使用getAdapterPosition() - 见<一href="http://stackoverflow.com/a/27886776/4658957">http://stackoverflow.com/a/27886776/4658957

  2. 开展的活动

      

    根据我需要写一个意图活性

    创建并在OnClickListener开始实施新的意图

     意图newIntent =新的意图(这一点,YourNewActivity.class);
    startActivity(newIntent);
     

长的答案

如何获得在RecyclerView列表中CardView项目的位置?

  

当我点击recyclerview cardView项目,我需要得到的位置   这car​​dview项目

我用 setTag() getTag()查看的方法。  您还可以使用getAdapterPosition - 见 http://stackoverflow.com/a/27886776/4658957

视图可以是一个TextView,ImageView的,CardView,甚至查看(其中可能包含文本视图,图像视图等等) - 基本确定,如果它从视图对象继承

  • 首先,看看下面的onCreateViewHolder方法。 layoutInflater 膨胀的意见,我的XML布局文件 item_view.xml 。在布局文件中的任何意见会在构造函数中使用。

      @覆盖
    公共myViewHolder onCreateViewHolder(ViewGroup中parentViewGroup,int i)以{
        ...
    
        查看ItemView控件= layoutInflater.inflate(R.layout.item_view,parentViewGroup,假);
        返回新myViewHolder(ItemView控件);
    }
     

  • 定义常量为您的视图对象,如MVIEW并将其设置为你想要的onClickListener设置/得到的位置查看。

     私有类myViewHolder扩展RecyclerView.ViewHolder {
        公众查看MView的;
    
        公共myViewHolder(查看视图){
            超(视图);
            MVIEW =图。
        }
    }
     

    在previous情况下,我设置MVIEW到父视图。

    如果你想从膨胀的onCreateViewHolder的item_view.xml布局文件访问某个特定的视图 - 定义另一个常量,用view.findViewbyId()设置它,不要忘了投视图(见下文)。

     私有类myViewHolder扩展RecyclerView.ViewHolder {
        公共CardView mCardView;
    
        公共myViewHolder(查看视图){
            超(视图);
            mCardView =(CardView)view.findViewById(R.id.card_view);
        }
    }
     

  • 在你的适配器类的onBindViewHolder方法使用setTag()。每个CardView目前拥有的RecyclerView列表作为标记的位置。

      @覆盖
    公共无效onBindViewHolder(myViewHolder viewHolder,INT位置){
        viewHolder.mCardView.setTag(位置);
    }
     

  • 使用getTag()来访问你的RecyclerView列表中的每个CardView的位置:我们有2种方法对这个(活动或XML)。

    设置onClickListener在ViewHolder构造函数(不要忘记实现View.OnClickListener)和编辑的onClick方法

     私有类myViewHolder扩展RecyclerView.ViewHolder实现View.OnClickListener {
               公众查看mCardView;
    
               公共myViewHolder(查看视图){
                   超(视图);
                   mCardView =(CardView)view.findViewById(R.id.card_view);
    
                   mCardView.setOnClickListener(本);
               }
           }
    
    @覆盖
    公共无效的onClick(视图查看){
        INT位置=(INT)view.getTag();
        Toast.makeText(view.getContext(),Integer.toString(位置),Toast.LENGTH_SHORT).show();
    }
     

    设置onclick属性在你的XML布局CardView 安卓的onClick =positionAction

     &LT; android.support.v7.widget.CardView
        机器人:ID =@ + ID / card_view
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:的onClick =positionAction&GT;
    
    &LT; /android.support.v7.widget.CardView>
     

    和创建包含回收视图活性的方法。

     公共无效positionAction(查看视图){
        INT位置=(INT)view.getTag();
        Toast.makeText(view.getContext(),Integer.toString(位置),Toast.LENGTH_SHORT).show();
    }
     

有关上述两个选项,您可以使用字符串位置=位置+ view.getTag.toString(); 而不是 INT位置= (INT)view.getTag; 如果你想获得的位置作为一个字符串

注1:您只能查看使用getTag()对象的setTag()方法被调用(否则你会得到一个NullPointerException异常)

注2:我没有使用一个密钥setTag()和getTag()方法,你可以做,如果你正在设置若干标签为每个视图

如何启动活动的基础上点击CardView?

  

根据我需要写一个意图活性

要开始基于所述用户点击CardView项新的活动,在onClick的方法,或在positionAction方法添加以下两行(基于所述一个选择使用)。

 意图newIntent =新的意图(这一点,YourNewActivity.class);
startActivity(newIntent);
 

如果你想从一个活动传递一个对象到另一个,使用

 意图newIntent =新的意图(这一点,YourNewActivity.class);
newIntent.putExtra(项目对象,itemList.get(位置));
startActivity(newIntent);
 

和新活动(YourNewActivity类):

  itemReceived =新项目();
itemReceived =(项目)getIntent()getSerializableExtra(项目对象)。
 

在这里ITEMLIST是项目的ArrayList()的对象。

参考:计算器 | <一href="http://$c$c.tutsplus.com/tutorials/getting-started-with-recyclerview-and-cardview-on-android--cms-23465"相对=nofollow> TUTS +

I created one adapter class for recycler for populating cardview layout in recyclerview. It is working fine, but when i click cardView item in recyclerview i need to get position of that cardview item , based on that i need to write a Intent activity. Along with how to delete that cardview item when i swipe left to right. MyAdapter class.

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private NavigationItem[] navigationItem;
    private Context mContext;

    public MyAdapter(NavigationItem[] navigationItem) {
        this.navigationItem = navigationItem;
    }

    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemLayoutView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.navigation_row_cardview, null);
        ViewHolder viewHolder = new ViewHolder(itemLayoutView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) {
        holder.txtViewTitle.setText(navigationItem[position].getTitle());
    }

    @Override
    public int getItemCount() {
        return navigationItem.length;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @InjectView(R.id.hambergerRecyclerView)
        public RelativeLayout recyclerView;
        public TextView txtViewTitle;

        public ViewHolder(final View itemLayoutView) {
            super(itemLayoutView);
            ButterKnife.inject(this, itemLayoutView);
            txtViewTitle = (TextView) itemLayoutView.findViewById(R.id.navigationItemOptionName);

            itemLayoutView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    // int itemPosition = RecyclerView.getChildPosition(view);
                    int itemPosition = recyclerView.getChildAdapterPosition(view);
                    String item = navigationItem.get(itemPosition);
                    Toast.makeText(mContext, item, Toast.LENGTH_LONG).show();
                }
            });
        }
    }
}

解决方案

Short Answer

  1. Getting the position of CardView item

    when i click cardView item in recyclerview i need to get position of that cardview item

    use setTag() in the onBindViewHolder method of your adapter and getTag() in your OnClickListener implementation.

    you can also use getAdapterPosition() - see http://stackoverflow.com/a/27886776/4658957

  2. Launching an activity

    based on that i need to write a Intent activity

    create and start a new Intent in your OnClickListener implementation

    Intent newIntent = new Intent(this, YourNewActivity.class);    
    startActivity(newIntent);
    

Long Answer

How to get the position of a CardView item in the RecyclerView list?

when i click cardView item in recyclerview i need to get position of that cardview item

I used the setTag() and getTag() methods of View. You can also use getAdapterPosition - see http://stackoverflow.com/a/27886776/4658957

The View can be a TextView, ImageView, CardView, even a View (which could contain text views, image views and so forth) - basically ok if it inherits from the View Object.

  • first, look at the onCreateViewHolder method below. layoutInflater inflates the views in my XML layout file item_view.xml. Any views in that layout file will be accessible in the constructor.

    @Override
    public myViewHolder onCreateViewHolder(ViewGroup parentViewGroup, int i) {
        ...
    
        View itemView = layoutInflater.inflate(R.layout.item_view, parentViewGroup, false);
        return new myViewHolder(itemView);
    }
    

  • define a constant for your View object such as mView and set it to the View you wish to set the onClickListener to / get the position from.

    private class myViewHolder extends RecyclerView.ViewHolder {
        public View mView;
    
        public myViewHolder(View view) {
            super(view);
            mView = view;
        }
    }
    

    In the previous case, I set mView to the parent View.

    If you want to access a specific View from the item_view.xml layout file inflated in onCreateViewHolder -- define another constant, set it using view.findViewbyId() and do not forget to cast the View (see below).

    private class myViewHolder extends RecyclerView.ViewHolder {
        public CardView mCardView;
    
        public myViewHolder(View view) {
            super(view);
            mCardView = (CardView) view.findViewById(R.id.card_view);
        }
    }
    

  • use setTag() in the onBindViewHolder method of your adapter class. Each CardView now has its position in the RecyclerView list as a tag.

    @Override
    public void onBindViewHolder(myViewHolder viewHolder, int position) {
        viewHolder.mCardView.setTag(position);
    }
    

  • use getTag() to access the position of each CardView in your RecyclerView list: we have 2 ways for this (in Activity or in XML).

    set onClickListener in your ViewHolder constructor (do not forget to implement View.OnClickListener) and edit the onClick method

    private class myViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
               public View mCardView;
    
               public myViewHolder(View view) {
                   super(view);
                   mCardView = (CardView) view.findViewById(R.id.card_view);
    
                   mCardView.setOnClickListener(this);
               }
           }
    
    @Override
    public void onClick(View view) {
        int position = (int) view.getTag();           
        Toast.makeText(view.getContext(),Integer.toString(position),Toast.LENGTH_SHORT).show();
    }
    

    OR set the onClick attribute in your XML layout for CardView android:onClick="positionAction"

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="positionAction">    
    
    </android.support.v7.widget.CardView>
    

    and create a method in the activity that contains the recycler view.

    public void positionAction(View view) {
        int position = (int) view.getTag();
        Toast.makeText(view.getContext(),Integer.toString(position),Toast.LENGTH_SHORT).show();
    }
    

For both options above, you can use String position = "Position " + view.getTag.toString(); instead of int position = (int) view.getTag; if you want to obtain the position as a String.

Note 1: You can only use getTag() on View objects whose setTag() method has been called (or else you will get a NullPointerException).

Note 2: I did not use a key for the setTag() and getTag() methods, which you could have done if you are setting several tags for each View.

How to start an activity based on the CardView clicked?

based on that i need to write a Intent activity

To start a new activity based on the CardView item the user clicks on, add the two lines below in the onClick method or in the positionAction method (based on the one you chose to use).

Intent newIntent = new Intent(this, YourNewActivity.class);    
startActivity(newIntent);

If you want to pass an object from one activity to the other, use

Intent newIntent = new Intent(this, YourNewActivity.class);
newIntent.putExtra("Item Object", itemList.get(position));
startActivity(newIntent);

and in the new activity (YourNewActivity class) :

itemReceived = new Item();
itemReceived = (Item) getIntent().getSerializableExtra("Item Object");

where itemList is an ArrayList of Item() objects.

References: StackOverflow | Tuts+

这篇关于如何获得cardView项目的recyclerView的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆