onClick无法在cardView中运行

时间:2016-11-30 12:45:11

标签: android android-recyclerview android-cardview

我正在开发一个应用程序,其中我正在使用recyclerView和cardView。我正在从服务器检索数据并显示它。当我尝试在此上实现onclick方法时,它无效。:

以下是我的主要活动代码:

public class Tab_1_Activity extends Fragment {

private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView.Adapter adapter;
SwipeRefreshLayout mSwipeRefreshLayout;
private Config config;
private List<ListItem> upcomingJobs = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.activity_tab_1, container, false);
    recyclerView = (RecyclerView) v.findViewById(R.id.recyclerview);
    recyclerView.setHasFixedSize(true);

    layoutManager = new LinearLayoutManager(getActivity());

    recyclerView.setLayoutManager(layoutManager);
    mSwipeRefreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.swifeRefresh);
    mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            getData();

            upcomingJobs.clear();
            upcomingJobs.addAll(upcomingJobs);
            // fire the event
            adapter.notifyDataSetChanged();


            // uAdapter.notifyDataSetChanged();
        }
    });
    //Make call to AsyncTask
    getData();
    return v;
}

适用于适配器类:

    public class CardAdapter  extends RecyclerView.Adapter<CardAdapter.ViewHolder> implements View.OnClickListener {

List<ListItem> items;


public CardAdapter(String[] offer, String[] offerprice, Bitmap[] image, String[] price, String[] url) {
    super();
    items = new ArrayList<ListItem>();
    for (int i = 0; i < offer.length; i++) {
        ListItem item = new ListItem();
        item.setoffer(offer[i]);
        item.seturl(url[i]);
        item.setofferprice(offerprice[i]);
        item.setprice(price[i]);
        item.setimage(image[i]);

        items.add(item);
    }
}


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

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    ListItem list = items.get(position);
    holder.imageView.setImageBitmap(list.getimage());
    holder.offer.setText(list.getoffer());
    holder.url.setText(list.geturl());

    holder.offerprice.setText(list.getofferprice());
    holder.price.setText(list.getprice());
    holder.itemView.setOnClickListener(this) ;


}


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

@Override
public void onClick(View v) {
    Intent i=new Intent(v.getContext(),Settings.class);
    v.getContext().startActivity(i);
}


public static class ViewHolder extends RecyclerView.ViewHolder {
    public ImageView imageView;
    public View view;

    public TextView offer, offerprice, price, url;


    public ViewHolder(View itemView) {
        super(itemView);

        imageView = (ImageView) itemView.findViewById(R.id.imageView);
        offer = (TextView) itemView.findViewById(R.id.offer);
        offerprice = (TextView) itemView.findViewById(R.id.offerprice);
        price = (TextView) itemView.findViewById(R.id.offerprice);
        url = (TextView) itemView.findViewById(R.id.url);


    }




    }
}

card_view_list.xml

    <?xml version="1.0" encoding="utf-8"?>
   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:paddingTop="10dp">


<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:clickable="true"
    android:foreground="?android:attr/selectableItemBackground"
    card_view:cardCornerRadius="2dp"
    card_view:cardUseCompatPadding="true">

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


        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:clickable="true"
            android:foreground="?android:attr/selectableItemBackground"
            android:scaleType="centerCrop"
            />

        <TextView
            android:id="@+id/url"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"

            android:textAllCaps="true"
            android:layout_below="@id/imageView"
            android:textColor="@color/colorAccent"
            android:textSize="18sp"
            android:textStyle="bold"/>

        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginRight="3dp"
            android:orientation="horizontal">

        </LinearLayout>

        <TextView
            android:id="@+id/price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:textColor="@color/textSecondary"
            android:textSize="16dp"
            android:textStyle="bold|italic"
            android:layout_below="@id/url"
            android:layout_marginTop="10dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <TextView
            android:id="@+id/offer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"

            android:textColor="@color/textSecondary"
            android:textSize="16sp" />


        <TextView
            android:id="@+id/offerprice"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/white"
            android:textSize="20sp"
            android:textStyle="italic" />


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

2 个答案:

答案 0 :(得分:0)

你必须为RecyclerVeiw写一个监听器。

  public interface ClickListener {
    void onClick(View view, int position);

    void onLongClick(View view, int position);
}

public static class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {

    private GestureDetector gestureDetector;
    private MainActivity.ClickListener clickListener;

    public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final MainActivity.ClickListener clickListener) {
        this.clickListener = clickListener;
        gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                return true;
            }

            @Override
            public void onLongPress(MotionEvent e) {
                View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
                if (child != null && clickListener != null) {
                    clickListener.onLongClick(child, recyclerView.getChildPosition(child));
                }
            }
        });
    }

    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {

        View child = rv.findChildViewUnder(e.getX(), e.getY());
        if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
            clickListener.onClick(child, rv.getChildPosition(child));
        }
        return false;
    }

    @Override
    public void onTouchEvent(RecyclerView rv, MotionEvent e) {
    }

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

    }
}

用法:

  recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), recyclerView, new RecyclerTouchListener.ClickListener() {
        @Override
        public void onClick(View view, int position) {

            Toast.makeText(getApplicationContext(),  "item is selected!", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onLongClick(View view, int position) {

        }
    }));

答案 1 :(得分:-2)

它对我来说也不起作用,我做的是给我的行布局中的根布局元素一个id(在你的情况下是card_view_list)。然后在我的ViewHolder中引用它,并将click监听器设置为该视图。尝试一下。

希望它有所帮助。