Android onClick在cardView中不工作

Android onClick在cardView中不工作,android,android-recyclerview,android-cardview,Android,Android Recyclerview,Android Cardview,我正在开发一个应用程序,其中我正在使用recyclerView和cardView。我正在从服务器检索数据并显示它。当我尝试在此服务器上实现onclick方法时,它不起作用: 以下是我的主要活动代码: public class Tab_1_Activity extends Fragment { private RecyclerView recyclerView; private RecyclerView.LayoutManager layoutManager; private RecyclerV

我正在开发一个应用程序,其中我正在使用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;
}
公共类选项卡\u 1\u活动扩展片段{
私人回收站;
private RecyclerView.LayoutManager LayoutManager;
专用循环视图适配器;
SwipeRefreshLayout mSwipeRefreshLayout;
私有配置;
私有列表upcomingJobs=new ArrayList();
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
视图v=充气机。充气(R.layout.activity_tab_1,容器,错误);
recyclerView=(recyclerView)v.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
layoutManager=新的LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
mSwipeRefreshLayout=(SwipeRefreshLayout)v.findViewById(R.id.swifeRefresh);
mSwipeRefreshLayout.setOnRefreshListener(新的SwipeRefreshLayout.OnRefreshListener()){
@凌驾
公共void onRefresh(){
getData();
upcomingJobs.clear();
upcomingJobs.addAll(upcomingJobs);
//解雇事件
adapter.notifyDataSetChanged();
//uAdapter.notifyDataSetChanged();
}
});
//调用AsyncTask
getData();
返回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);


    }




    }
}
公共类CardAdapter扩展了RecyclerView。适配器实现了View.OnClickListener{
清单项目;
公共卡适配器(字符串[]优惠,字符串[]优惠价格,位图[]图像,字符串[]价格,字符串[]url){
超级();
items=newarraylist();
for(int i=0;i
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>


它对我也不起作用,我所做的是在我的行布局中给根布局元素一个id(在你的例子中是
card\u view\u list
)。然后在我的
ViewHolder
中引用它,并将单击侦听器设置为该视图。尝试一下


希望能有所帮助。

您必须为RecyclerView编写一个侦听器

  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) {

        }
    }));

您只是在实现
onClickListener
,您必须
setOnClickListener()
onCreateViewHolder()
中的视图,您应该使用监听器界面进行正确的点击处理操作您可以共享卡片\u视图\u列表.xml文件吗请查看我的更新代码查看我的答案这对您有用吗