10

Tôi có một card_contents.xml cardViewThêm clickListner cho nút bên trong một cardView dân cư sử dụng một recyclerView

<?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="match_parent"> 
<android.support.v7.widget.CardView android:layout_width="match_parent" 
    android:layout_height="150dp" 
    android:layout_marginTop="10dp" 
    android:layout_marginLeft="5dp" 
    android:layout_marginRight="5dp" 
    android:id="@+id/card_view"> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#ffa3a4a6"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/card_text" 
     android:layout_gravity="center" 
     android:gravity="center" 
     android:text="B.E" 
     android:layout_centerInParent="true" 
     /> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:id="@+id/button1" 
     android:layout_toLeftOf="@+id/view" 
     android:layout_alignParentLeft="true" 
     android:text="2010" 
     android:textColor="#000000" 
     android:background="@android:color/transparent" 
     /> 
     <View 
      android:layout_width="2dp" 
      android:layout_height="@dimen/abc_action_button_min_height_material" 
      android:layout_centerHorizontal="true" 
      android:id="@+id/view" 
      android:background="@android:color/black" 
      android:layout_alignParentBottom="true" 
      /> 
     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/button2" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentRight="true" 
      android:layout_toRightOf="@+id/view" 
      android:background="@android:color/transparent" 
      android:textColor="@android:color/black" 
      android:text="2014"/> 
    </RelativeLayout> 
    </android.support.v7.widget.CardView> 
    </RelativeLayout> 

trong đó sử dụng một cái nhìn tái chế:

public class MainActivity extends ActionBarActivity { 

RecyclerView recyclerView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 



    List<String> list=new ArrayList<String>(); 
    list.add("Hello"); 
    list.add("Hello World"); 
    list.add("Hello World Beings"); 

    recyclerView=(RecyclerView)findViewById(R.id.recycler_view); 
    recyclerView.setItemAnimator(new DefaultItemAnimator()); 
    recyclerView.setHasFixedSize(true); 

    RecyclerView.LayoutManager layoutManager=new LinearLayoutManager(this); 
    recyclerView.setLayoutManager(layoutManager); 

    RecyclerView.Adapter adapter=new MyAdapter(list); 
    recyclerView.setAdapter(adapter); 




} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 
} 

sử dụng adapter

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> { 
List<String> list; 
public MyAdapter(List<String> list){ 
    this.list=list; 
} 

@Override 
public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 
    View v=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_contents,viewGroup,false); 
    return new MyViewHolder(v); 
} 

@Override 
public void onBindViewHolder(MyViewHolder myViewHolder, int i) { 
     myViewHolder.textView.setText(list.get(i)); 
} 

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

public static class MyViewHolder extends RecyclerView.ViewHolder{ 
    TextView textView; 
    MyViewHolder(View view){ 
     super(view); 
     this.textView= (TextView) view.findViewById(R.id.card_text); 
    } 
} 
} 

Tôi thực sự muốn đặt trình nghe nhấp chuột cho các nút bên trong thẻ và khởi chạy hoạt động mới tùy thuộc vào wh nút ich được nhấn và trên thẻ nó được nhấn. Có cách nào để hoàn thành nó không? Tôi đã nhìn xung quanh nhưng did'nt tìm thấy bất kỳ câu trả lời cho thiết lập người nghe nhấp chuột cho các mục bên trong của một cardView.I mới cho android và giúp đỡ bất kỳ sẽ được đánh giá cao. Cảm ơn trước

+0

Ngoài ra tôi muốn sử dụng cùng một bộ điều hợp để điền vào chế độ xem của người tái chế khác chỉ với bản xem trước văn bản. – AnoopDV

Trả lời

32

Thêm vào nghe cho nút bên trong một cardView dân cư sử dụng một recyclerView

Thêm sự kiện click vào nút mà là ở RecyclerView như:

1. Nhận Nút từ xml giống như cách thực hiện cho TextView trong MyViewHolder lớp:

public static class MyViewHolder extends RecyclerView.ViewHolder{ 
    TextView textView; 
    Button btnButton1; 
    MyViewHolder(View view){ 
     super(view); 
     this.textView= (TextView) view.findViewById(R.id.card_text); 
     this.btnButton1= (Button) view.findViewById(R.id.button1); 
     ... do same for other Button 
    } 
} 

2. Thêm setOnClickListener phương pháp cho Button trong onBindViewHolder phương pháp:

@Override 
public void onBindViewHolder(MyViewHolder myViewHolder, int i){ 
    myViewHolder.textView.setText(list.get(i)); 
     myViewHolder.btnButton1.setOnClickListener(new View.OnClickListener(){ 
     @Override 
     public void onClick(View v) { 
      /// button click event 
     } 
    }); 
} 
+0

Cảm ơn nhưng nếu tôi muốn sử dụng cùng một bộ điều hợp để phổ biến chế độ xem tái chế chỉ với chế độ xem văn bản – AnoopDV

+0

@ user3359069: tôi nghĩ bình thường thì không thể, nhưng bạn có thể thực hiện bằng cách sử dụng các điều kiện if-else, hiển thị/ẩn chế độ xem không cách hợp lệ. như gợi ý của tôi tạo ra một Adapter riêng biệt cho thiết kế hàng khác mà theo đó bạn sẽ phát triển các module độc ​​lập được kết hợp rất lỏng lẻo .Thanks –

+0

Làm thế nào để tôi biết trong đó recyclerview nút được nhấn? –

2

Xác định người nghe onClick của bạn về phương pháp onBindViewHolder trong CustomAdapter.

0

Hãy nói rằng bạn có:

<android.support.v7.widget.CardView 
    android:id="@+id/cv" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="4dp" 
    android:elevation="4dp" 
    app:cardCornerRadius="2dp"> 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:padding="16dp"> 

     <TextView 
      android:id="@+id/listText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:padding="8dp" 
      android:text="New Text" /> 

     <ImageButton 
      android:id="@+id/button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignBottom="@+id/listText" 
      android:layout_alignParentRight="true" 
      android:onClick="onClickBotonBorrar" 
      android:background="?android:selectableItemBackground" 
      android:src="@drawable/ic_action_borrar" /> 
    </RelativeLayout> 

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

Lưu ý phần này: android: onClick="onClickBotonBorrar"

On Hoạt động của bạn thêm này:

public void onClickBotonBorrar (View v) { 
//Do whatever you want when user clicks on your ImageButton 
    } 

Đó là khuyến cáo không có sự kiện click bên trong bộ điều hợp của bạn nhưng bên trong hoạt động của bạn.

Lưu ý: Nút này tương tự cho nút

Các vấn đề liên quan