无法理解RecycleView中多个CardView的onClick事件

时间:2015-03-11 03:00:58

标签: android android-recyclerview android-cardview

我查找了可能的解决方案,但我无法弄清楚如何在我的情况下应用它们。 我正在使用RecyclerView在其中显示多个CardView。最初所有卡都有白色背景,我想在触摸卡时更改背景颜色。每个CardView都会显示名称和ID,如“MyCards”中所述。 class:

    public class MyCards {
    private String sName;
    private int sId;
    private boolean sPresent;

    public String getName() {
        return this.sName;
    }

    public MyCards(String sName, int sId, boolean sPresent) {
        this.sName = sName;
        this.sId = sId;
        this.sPresent = sPresent;
    }

    public void setName(String t) {
        this.sName = t;
    }

    public int getId() {
        return sId;
    }

    public void setId(int id) {
        this.sId = id;
    }

    public boolean isPresent() {
        return sPresent;
    }

    public void setPresent(boolean present) {
        this.sPresent = present;
    }
}

我在' Roster'中制作了这些MyCards对象的ArrayList。 class:

public class Roster extends ActionBarActivity{
private static final int READ_REQUEST_CODE = 42;
private static final String TAG = null;
private RecyclerView rObject;
private MyAdapter adapter;
String abc = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.roster_recycle);
        rObject = (RecyclerView)findViewById(R.id.recycler_view);
        LinearLayoutManager llm = new LinearLayoutManager(this);
        llm.setOrientation(LinearLayoutManager.VERTICAL);
        rObject.setLayoutManager(llm);

        MyCards c1 = new MyCards("Abhinav", 123, true);
        MyCards c2 = new MyCards("Abhishek", 13, false);
        performFileSearch();
        c1.setName("Abhinav");
        c2.setName("Baba");
        List<MyCards> cardList = new ArrayList<MyCards>();
        cardList.add(c1);
        cardList.add(c2);
        adapter = new MyAdapter(Roster.this,cardList);
        rObject.setAdapter(adapter);
    }

CardView在item_layout.xml文件中定义:

<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:clickable="true"
    android:foreground="?android:attr/selectableItemBackground"
    card_view:cardCornerRadius="4dp" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TextView
            android:id="@+id/sName"
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:gravity="center_vertical"
            android:text="contact det"
            android:textSize="14dp" />

        <TextView
            android:id="@+id/sId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/sName"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="10dp"
            android:gravity="center_vertical"
            android:text="Name"
            android:textSize="10dp" />

        <TextView
            android:id="@+id/sPresent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/sId"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="10dp"
            android:gravity="center_vertical"
            android:text="Present"
            android:textSize="10dp" />
    </RelativeLayout>
</android.support.v7.widget.CardView>

我的问题是:如果我点击显示&#39; c1&#39;对象然后如何识别它来自&#39; c1&#39;而不是&#39; c2&#39;。在我们显示多张卡的情况下,如何使用点击处理程序?

对不起,如果我的问题太基本但我真的坚持了。

3 个答案:

答案 0 :(得分:1)

非常感谢您的回复,他们真的很有帮助。最后,我在YouTube上发现了一个视频,以一种很好的方式解释了这个概念。我发布链接,以便其他人也可以受益,如果他们卡住。这是视频 -

请看: - https://www.youtube.com/watch?v=zE1E1HOK_E4

答案 1 :(得分:0)

查看使用带有RecyclerView的onItemClickListener的this答案。

答案 2 :(得分:0)

公共类MyAdapter扩展了RecyclerView.Adapter {

Context mContext;
ArrayList<MyCard> mData;

public MyAdapter(Context context , ArrayList<MyCard> data ) {

    mContext = context;
    mData = data;
}

public class ViewHolder extends RecyclerView.ViewHolder{

    private TextView sName, sId , sPresent;


    public ViewHolder(View itemView) {
        super(itemView);
        sName = (TextView) itemView.findViewById(R.id.sName);
        sId = (TextView) itemView.findViewById(R.id.sId);
        sPresent = (TextView) itemView.findViewById(R.id.sPresent);
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                // your logic

            }
        });
    }

}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout , parent , false);
    ViewHolder vh = new ViewHolder(v);
    return vh;
}

@Override
public int getItemCount() {
    return mData.size();
}

@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
    final MyCard card = mData.get(position);
    holder.sName.setText(card.getName().toString());
    holder.sId.setText(card.getId().toString());
    holder.sPresent.setText(card.getPresent().toString());

}

}