当我按下cardview内的按钮时,点击了cardview项目和按钮

时间:2016-10-04 01:52:55

标签: android android-cardview

我有一张带有按钮的Cardview

卡片视图布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:card_view="http://schemas.android.com/apk/res-auto"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="vertical">

<android.support.v7.widget.CardView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="0dp"
    android:layout_marginLeft="2dp"
    android:layout_marginRight="2dp"
    android:layout_marginTop="2dp"
    card_view:cardCornerRadius="3dp"
    card_view:cardElevation="0.01dp"
    >

    <LinearLayout

        android:layout_width="fill_parent"
        android:layout_height="123dp"
        android:background="@drawable/posts_card_bg"
        android:orientation="horizontal"
        android:paddingBottom="3dp"
        android:paddingRight="3dp"
        >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_weight="0.8"
            >

            <TextView
                android:id="@+id/post_title"
                android:textColor="@android:color/white"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:alpha="1"
                android:gravity="center_vertical"
                android:maxLines="2"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:text="title"
                android:textSize="30sp"/>


            <TextView
                android:id="@+id/post_body"
                android:layout_width="match_parent"
                android:textColor="@android:color/white"
                android:layout_height="wrap_content"
                android:maxLines="2"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:text="Body"
                android:textSize="20sp"/>
        </LinearLayout>

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="120dp"
            >
        <Button
            android:id="@+id/expand"
            android:layout_width="40dp"
            android:layout_height="37dp"
            android:background="@drawable/expand"
            />
        <TextView
            android:id="@+id/post_id"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@drawable/circle"
            android:gravity="center"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:text="99"
            android:textColor="@android:color/white"

            android:textSize="16sp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentStart="true"/>
        </RelativeLayout>

    </LinearLayout>

</android.support.v7.widget.CardView>

PostCardAdapter.java

    @Override
    public void onBindViewHolder(ContactViewHolder holder, int position) {
   ......
    holder.btn_expand.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //for showing details of item on dialog
            listener.onItemClicked(view);
        }
    });

}

Recyclerview项目单击

  listener = this;
    mRecyclerView = (RecyclerView) findViewById(R.id.cardlist_posts);
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    mRecyclerView.setAdapter(new PostCardAdapter(listener,db.getAllPosts(), R.layout.cardview_post));


    mRecyclerView.addOnItemTouchListener(
            new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int id) {
                    // TODO Handle item click
                    Intent intent = new Intent(MainActivity.this, ShowPostDetails.class);
                    intent.putExtra("KEY_POST_ID", String.valueOf(id + 1));
                    System.out.println(String.valueOf(id + 1));
                    //Starting another activity to show post details
                    startActivity(intent);

                }
            })
    );

MainActivity.java

  @Override
public void onItemClicked(View v) {
    DialogPostFragment df= new DialogPostFragment();
    df.show(getFragmentManager(), "Dialog");
}

按下按钮后,单击cardview项目,同时打开正在进行的后退对话框。 我只想在按下按钮时打开对话框。 谢谢。

1 个答案:

答案 0 :(得分:0)

我曾经遇到过完全相同的问题。这就是我解决问题的方法:

id 添加到cardview内的第一个线性布局:

<LinearLayout
        android:id="@+id/rowLL"
        android:layout_width="fill_parent"
        android:layout_height="123dp"
        android:background="@drawable/posts_card_bg"
        android:orientation="horizontal"
        android:paddingBottom="3dp"
        android:paddingRight="3dp"
        >

现在,在 PostCardAdapter.java

public class PostCardAdapter extends RecyclerView
        .Adapter<ContactViewHolder> {

   ...

   private static MyClickListener myClickListener;

   ...

   public void setOnItemClickListener(MyClickListener myClickListener) {
       PostCardAdapter.myClickListener = myClickListener;
    }

   ...        

    public interface MyClickListener {
        public void onItemClick(int position, View v);
    }
}

然后,在 ContactViewHolder .java

public class ContactViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        private PostCardAdapter.MyClickListener myClickListener;

        ...

        public ContactViewHolder(final Context context, View itemView, PostCardAdapter.MyClickListener clickListener) {
           super(itemView);
            ...
            myClickListener = clickListener;
            itemView.findViewById(R.id.rowLL).setOnClickListener(this);
        }

       @Override
        public void onClick(View v) {
            myClickListener.onItemClick(getLayoutPosition(), v);
        }
}