Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I setup OnClickListener in Cardview, but its work only in bottom border of cv. I think trouble in layout files.
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="androidhive.info.materialdesign.activity.FriendsFragment">


    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefreshLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycleralda"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </android.support.v7.widget.RecyclerView>

    </android.support.v4.widget.SwipeRefreshLayout>

</RelativeLayout>


XML
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/tools"
    android:id="@+id/carder"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="7dp"
    card_view:cardElevation="2sp"
    card_view:cardUseCompatPadding="true"
    android:layout_margin="1dp"
    android:layout_marginTop="2dip"
    android:layout_marginBottom="2dip">



<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:baselineAligned="false"
        android:clickable="true"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/imagePreview"
                android:src="@mipmap/ic_launcher"
                android:layout_gravity="right"
                android:layout_marginRight="8dp" />

        </LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:weightSum="1"
            android:layout_gravity="center">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:textSize="16sp"
                android:text="Lorem ipsum"
                android:id="@+id/titleText"
                android:layout_alignParentTop="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_weight="1"
                android:layout_marginLeft="3dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Small Text"
                android:id="@+id/dateText"
                android:layout_gravity="right"
                android:layout_weight="2" />

        </LinearLayout>

    </LinearLayout>

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

Java
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>  {
    private List<News> news;
    private Context mContext;


    public RecyclerViewAdapter(List<News> news, Context mContext) {
        this.news = news;
        this.mContext = mContext;

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.carder_new, parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        News news = this.news.get(position);
        holder.text1.setText(news.getTitle());
        holder.text3.setText(news.getDate());
        Picasso.with(mContext).load(this.news.get(position).getPreviewImage()).resize(150, 120).into
                (holder.imageView);
    }


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



    public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
    {
        public View container;
        public ClipData.Item currentItem;

        private TextView text1;
        private TextView text3;
        private ImageView imageView;

        public ViewHolder(View view) {
            super(view);
            view.setOnClickListener(this);
            text1 = (TextView)view.findViewById(R.id.titleText);
            text3 = (TextView)view.findViewById(R.id.dateText);

            imageView = (ImageView)view.findViewById(R.id.imagePreview);

        }
        @Override
        public void onClick(View view) {
            Log.e("Axixa", "onClick " + getPosition());
        }
    }
}
Posted
Updated 16-May-15 5:30am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900