如何在Fragment的Recycler View中点击项目进行活动?

时间:2015-11-28 21:48:41

标签: android android-recyclerview

我有一个活动,其中有大约4个片段和片段有一个recyclerview,并在每个recyclerview项目中有一个按钮..我有一个文本字段在活动,我想显示否。点击按钮。

这就是我的活动。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="3"
    tools:context="com.example.khaalijeb.newlistview_module.PromoCodeActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.4"
        android:background="#f9f9f9"
        android:elevation="5dp">

        <ImageView
            android:id="@+id/back"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="5dp"
            android:src="@drawable/greyback" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:layout_toRightOf="@+id/back"
            android:text="Fancy Some Deals ?"
            android:textSize="16dp"
            android:textStyle="bold" />

        <Button
            android:id="@+id/skip"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:background="@drawable/roundedcorneredittext"
            android:text="SKIP"
            android:textColor="#0277bd"
            android:textSize="12dp" />


    </RelativeLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.55"
        android:orientation="vertical">

        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabGravity="fill"
            app:tabIndicatorColor="#E12728"
            app:tabMode="scrollable"
            app:tabSelectedTextColor="#E12728"
            app:tabTextAppearance="@style/TextAppearance.Design.Tab" />

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#ffffff" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.05"
        android:background="#f9f9f9"
        android:orientation="vertical"
        android:weightSum="2">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:orientation="vertical"
            android:weightSum="2">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1.2"
                android:orientation="horizontal"
                android:weightSum="2">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="0.5"
                    android:orientation="vertical">

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:background="#727272"

                        />

                    <android.support.design.widget.TextInputLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:theme="@style/TextLabel">

                        <EditText
                            android:id="@+id/spass"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical|center_horizontal"
                            android:hint="Promo Code"
                            android:inputType="textPassword"
                            android:paddingLeft="10dp"
                            android:textColor="@color/material_blue_grey_800"
                            android:textSize="14dp" />

                    </android.support.design.widget.TextInputLayout>
                </LinearLayout>

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1.5"
                    android:orientation="vertical">

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:background="#727272" />

                    <Button
                        android:id="@+id/apply"
                        android:layout_width="match_parent"
                        android:layout_height="35dp"
                        android:layout_marginBottom="10dp"
                        android:layout_marginLeft="5dp"
                        android:layout_marginRight="5dp"
                        android:layout_marginTop="10dp"
                        android:background="@drawable/roundedcorneredittext"
                        android:text="APPLY"
                        android:textColor="#0277bd"
                        android:textSize="12dp" />

                    <Button
                        android:id="@+id/applied"
                        android:layout_width="match_parent"
                        android:layout_height="35dp"
                        android:layout_marginBottom="10dp"
                        android:layout_marginLeft="5dp"
                        android:layout_marginRight="5dp"
                        android:layout_marginTop="10dp"
                        android:background="@drawable/roundedcorneredittext"
                        android:text="APPLIED"
                        android:textColor="#0277bd"
                        android:textSize="12dp"
                        android:visibility="invisible"
                        />

                </RelativeLayout>


            </LinearLayout>

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

                <TextView
                    android:id="@+id/credittext"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="5dp"
                    android:textSize="12dp"
                    android:text="Credited To Jeb No." />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignTop="@+id/credittext"
                    android:layout_toRightOf="@+id/credittext"
                    android:text="C100"
                    android:textSize="12dp"
                    />

                <TextView
                    android:id="@+id/amount"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_alignTop="@+id/credittext"
                    android:layout_marginRight="16dp"
                    android:text="Rs.100"
                    android:textSize="12dp"
                    />


                <TextView
                    android:id="@+id/pickdeal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/credittext"
                    android:layout_margin="5dp"
                    android:text="Haven't Picked Any Deals Yet"
                    android:textColor="#78909C"
                    android:textSize="12dp" />

                <TextView
                    android:id="@+id/dealamount"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_below="@+id/amount"
                    android:text="Rs.50"
                    android:layout_marginRight="16dp"
                    android:layout_alignTop="@+id/pickdeal"
                    android:textSize="12dp"
                    />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_toLeftOf="@+id/dealamount"
                    android:text="+"
                    android:layout_alignTop="@+id/dealamount"
                    android:layout_marginRight="2dp"
                    android:textSize="12dp"
                    />

                <TextView
                    android:id="@+id/discount"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/pickdeal"
                    android:text="Discount"
                    android:layout_alignLeft="@+id/pickdeal"
                    android:textColor="#F44336"
                    android:textSize="12dp"
                    android:layout_marginTop="5dp"
                    />

                <TextView
                    android:id="@+id/discountedamount"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:text="Rs.50"
                    android:textColor="#F44336"
                    android:layout_alignTop="@+id/discount"
                    android:layout_marginRight="16dp"
                    android:textSize="12dp"
                    />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_toLeftOf="@+id/discountedamount"
                    android:layout_alignTop="@+id/discountedamount"
                    android:text="-"
                    android:textColor="#F44336"
                    android:layout_marginRight="2dp"
                    />
            </RelativeLayout>


        </LinearLayout>

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

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#0277bd"
                android:text="PROCEED TO PAY" />


        </RelativeLayout>


    </LinearLayout>


</LinearLayout>

recyelrview项目

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/llContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical"
    android:padding="5dp">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="3dp"
        card_view:cardBackgroundColor="#f9f9f9"
        card_view:cardCornerRadius="1dp"
        card_view:cardElevation="5dp">

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:orientation="vertical"
            android:weightSum="2"

            >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="horizontal"
                android:weightSum="2">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1.7">

                    <ImageView
                        android:id="@+id/icon"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginBottom="5dp"
                        android:layout_marginLeft="5dp"
                        android:layout_marginRight="5dp"
                        android:layout_marginTop="5dp"
                        android:src="@drawable/nike" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.3"
                    android:orientation="horizontal"
                    android:weightSum="2">

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

                        <TextView
                            android:id="@+id/brandname"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="10dp"
                            android:layout_marginTop="10dp"
                            android:text="hello"
                            android:textAllCaps="true"
                            android:textColor="#000000"
                            android:textSize="14dp"
                            android:typeface="sans" />

                        <TextView
                            android:id="@+id/title"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignLeft="@+id/brandname"
                            android:layout_below="@+id/brandname"
                            android:layout_marginTop="10dp"
                            android:text="Get 50% off on Apparels"
                            android:textSize="12dp" />

                        <TextView
                            android:id="@+id/seemore"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignLeft="@id/brandname"
                            android:layout_below="@+id/title"
                            android:layout_marginBottom="10dp"
                            android:layout_marginTop="10dp"
                            android:text="See More"
                            android:textSize="12dp" />

                        <ImageView
                            android:layout_width="16dp"
                            android:layout_height="16dp"
                            android:layout_alignTop="@+id/seemore"
                            android:layout_marginLeft="10dp"
                            android:layout_toRightOf="@+id/seemore"
                            android:src="@drawable/sendgrey" />

                    </RelativeLayout>

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

                        <Button
                            android:id="@+id/free"
                            android:layout_width="match_parent"
                            android:layout_height="30dp"
                            android:layout_centerInParent="true"
                            android:layout_marginBottom="16dp"
                            android:layout_marginLeft="5dp"
                            android:layout_marginRight="5dp"
                            android:layout_marginTop="16dp"
                            android:background="@drawable/roundedcorneredittext"
                            android:text="Free"
                            android:textColor="#0277bd"
                            android:textSize="12dp"
                            />

                        <Button
                            android:id="@+id/taken"
                            android:layout_width="match_parent"
                            android:layout_height="30dp"
                            android:layout_centerInParent="true"
                            android:layout_marginBottom="16dp"
                            android:layout_marginLeft="5dp"
                            android:layout_marginRight="5dp"
                            android:layout_marginTop="16dp"
                            android:background="@drawable/roundedcorner1"
                            android:text="Great!"
                            android:textColor="#ffffff"
                            android:textSize="12dp"
                            android:visibility="invisible"
                            />

                    </RelativeLayout>

                </LinearLayout>

            </LinearLayout>


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

</LinearLayout>

Adapter.java

public class PromoCodeRecyclerViewAdapter extends RecyclerView.Adapter<PromoCodeRecyclerViewAdapter.MyViewholder> {

    private ClickListener clickListener;
    private LayoutInflater inflator;
    private Context mcontext;
    Typeface font;

    public Button b1;
    public ImageButton b2;

    List<promocodedata> data = Collections.emptyList();

    public PromoCodeRecyclerViewAdapter(Context context, List<promocodedata> y, Typeface font) {

        inflator = LayoutInflater.from(context);
        this.data = y;
        this.mcontext = context;
        this.font = font;


    }



    public void setClickListener(ClickListener clickListener) {
        this.clickListener = clickListener;
    }

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

        View view = inflator.inflate(R.layout.promocode_item_layout, parent, false);
        MyViewholder holder = new MyViewholder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewholder holder, int position) {

        promocodedata current = data.get(position);
        holder.brandicon.setImageResource(current.brandicon);
        holder.brandname.setText(current.brandname);
        holder.title.setText(current.title);
        holder.free.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                holder.taken.setVisibility(View.VISIBLE);
                holder.free.setVisibility(View.INVISIBLE);
            }
        });
        holder.taken.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                holder.taken.setVisibility(View.INVISIBLE);
                holder.free.setVisibility(View.VISIBLE);
            }
        });

    }


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

    public interface ClickListener {
        void itemClicked(View v, int position);


    }

    class MyViewholder extends RecyclerView.ViewHolder implements View.OnClickListener {


        ImageView brandicon;
        TextView brandname;
        TextView title;
        Button free;
        Button taken;


        public MyViewholder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);

            brandicon = (ImageView) itemView.findViewById(R.id.icon);
            brandname = (TextView) itemView.findViewById(R.id.brandname);
            title = (TextView) itemView.findViewById(R.id.title);
            free = (Button)itemView.findViewById(R.id.free);
            taken = (Button)itemView.findViewById(R.id.taken);

        }

        @Override
        public void onClick(View v) {

            if (clickListener != null) {
                clickListener.itemClicked(v, getPosition());
            }

        }
    }
}

1 个答案:

答案 0 :(得分:0)

你需要创建你的onitemclick因为RecyclerView没有像Listview这样的OnItemClick,我在Github中有一个例子来检查它。