2016-11-30 67 views
0

我正在開發一個應用程序,其中我正在使用recyclerView和cardView.I從服務器檢索數據並顯示它。當我嘗試在此上實現onclick方法時,它不工作:onClick不工作在cardView

這是我的主要活動碼:

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

你只是執行'onClickListener',你必須'setOnClickListener()'在'onCreateViewHolder視圖()' –

+0

你應該使用監聽器接口進行適當的處​​理點擊操作 –

+0

可以分享card_view_list.xml文件 –

回答

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

     } 
    })); 
-2

它沒有爲我工作過,我所做的就是給一個ID,根佈局元素在我行的佈局(在你的情況是card_view_list)。然後在我的ViewHolder中引用它並將點擊偵聽器設置爲該視圖。試試看。

希望它有幫助。