[英]Click an ImageButton which belongs to a CardView inside a RecyclerView
SOLVED 解决了
I've fixed it mixing your answers, so I've decided to write down on here. 我已经修好了你的答案,所以我决定写下这里。
The key has been the ViewHolder
constructor: 关键是ViewHolder
构造函数:
I have deleted the setOnClickListener()
and the onClick()
methods and I´ve defined this inner class in my ObjectAdapter.java
. 我删除了setOnClickListener()
和onClick()
方法,我在ObjectAdapter.java
定义了这个内部类。
public static class ViewHolder extends RecyclerView.ViewHolder{
public ViewHolder(View itemView) {
super(itemView);
card_first_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = getAdapterPosition();
Toast.makeText(v.getContext(), object.get(position).getName(), Toast.LENGTH_SHORT).show();
}
});
card_second_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Forward button", Toast.LENGTH_SHORT).show();
}
});
}
}
And that´s all. 就这样。 The rest of the class is the same. 班上的其他人都是一样的。
The ObjectFragment.java
is: ObjectFragment.java
是:
public class ObjectFragment extends Fragment {
public void ObjectFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater layoutInflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this Fragment
return layoutInflater.inflate(R.layout.fragment_object, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final ArrayList<Object> objects = initializeList();
if(!objects.isEmpty()) {
RecyclerView recyclerView = (RecyclerView) getActivity().findViewById(R.id.objects_recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new SimpleDivider(getActivity().getApplicationContext()));
final ObjectAdapter objectAdapter = new ObjectAdapter(objects, R.layout.card_object);
recyclerView.setAdapter(objectAdapter);
}
}
private ArrayList<Object> initializeList() {
ArrayList<Object> objects= new ArrayList<>();
objects.add(new Object(...));
return objects;
}
}
Thank you very much guys. 非常感谢你们。
Original question 原始问题
I have a RecyclerView
which contains some CardViews
. 我有一个包含一些CardViews
的RecyclerView
。 This CardViews
have two different ImageButton
which make different things when you click them (obvious). 这个CardViews
有两个不同的ImageButton
,当你点击它们时会产生不同的东西(很明显)。
Here is the card_object.xml
这是card_object.xml
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/object_card"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="2dp"
card_view:cardBackgroundColor="@color/colorPrimary"
android:clickable="true">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:background="@color/colorPrimary">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/object_card_first_button"
android:src="@drawable/contact_100px"
android:maxHeight="25dp"
android:maxWidth="25dp"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:background="@color/transparentBackgroundPrimary"
android:layout_marginRight="54dp"
android:layout_marginEnd="54dp"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/forward_100px"
android:maxHeight="25dp"
android:maxWidth="25dp"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:background="@color/transparentBackgroundPrimary"
android:id="@+id/object_card_second_button" />
</LinearLayout>
</android.support.v7.widget.CardView>
This is the main code of my ObjectAdapter.java
: 这是我的ObjectAdapter.java
的主要代码:
public class ObjectAdapter
extends RecyclerView.Adapter<ObjectAdapter.ViewHolder>
implements View.OnClickListener {
private ArrayList<Object> objects;
private int itemLayout;
private View.OnClickListener onClickListener;
public ObjectAdapter(ArrayList<Object> objects, int itemLayout) {
this.objects = objects;
this.itemLayout = itemLayout;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext())
.inflate(itemLayout, viewGroup, false);
view.setOnClickListener(this);
return new ViewHolder(view);
}
public void setOnClickListener(View.OnClickListener listener) {
this.onClickListener = listener;
}
public void onClick(View view) {
if(onClickListener != null) {
onClickListener.onClick(view);
}
}
My ObjectFragment.java
is the next: 我的ObjectFragment.java
是下一个:
public class ObjectFragment extends Fragment {
public void ObjectFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater layoutInflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this Fragment
return layoutInflater.inflate(R.layout.fragment_object, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final ArrayList<Object> objects = initializeList();
if(!objects.isEmpty()) {
RecyclerView recyclerView = (RecyclerView) getActivity().findViewById(R.id.objects_recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new SimpleDivider(getActivity().getApplicationContext()));
final ObjectAdapter objectAdapter = new ObjectAdapter(objects, R.layout.card_object);
objectAdapter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Here I can manage my Object attributes,
// but no the ImageButtons itselves
}
});
recyclerView.setAdapter(objectAdapter);
}
}
private ArrayList<Object> initializeList() {
ArrayList<Object> objects= new ArrayList<>();
objects.add(new Object(...));
return objects;
}
}
I can obtain the values of the Object
attributes (name, title, whatever), and click on the CardView
and so on, but I'm not able to make that the ImageButton
have their own onClick. 我可以获取Object
属性的值(名称,标题,等等),然后单击CardView
等,但我无法让ImageButton
拥有自己的onClick。
I don't know if using android:onClick()
it would be possible to display the information of a specific card, but this is another thing. 我不知道是否使用android:onClick()
它可以显示特定卡的信息,但这是另一回事。
So my question is how can I make that this two buttons do have their own OnClickListener
, where I should declare it and how can handle the information of each item of the RecyclerView
after that (this part is almost achieved). 所以我的问题是如何让这两个按钮确实有自己的OnClickListener
,我应该声明它,以及如何处理RecyclerView
的每个项目的信息(这部分几乎实现)。
Any idea? 任何想法? Thanks a lot! 非常感谢!
You can add the button event clicks in your ViewHolder class like this. 您可以像这样在ViewHolder类中添加按钮事件点击。
public class RecyclerViewHolders extends RecyclerView.ViewHolder implements View.OnClickListener{
public ButtonImage firstButton;
public ButtonImage secondButton;
public RecyclerViewHolders(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
firstButton = (ImageButton)itemView.findViewById(R.id.object_card_first_button);
firstButton = (ImageButton)itemView.findViewById(R.id.object_card_second_button);
}
@Override
public void onClick(View view) {
int id = view.getId();
switch (id){
case R.id.object_card_first_button:
// button event
break;
case R.id.object_card_second_button
// button event
break;
}
}
I think something like view.findViewById(R.id.mybutton).setOnClickListener(...); 我认为像view.findViewById(R.id.mybutton).setOnClickListener(...); should do the job. 应该做的工作。
Let me know if that works. 如果有效,请告诉我。
Something like this? 像这样的东西?
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext())
.inflate(itemLayout, viewGroup, false);
view.setOnClickListener(this);
ImageButton btn = (ImageButton)view.findViewById(R.id. object_card_first_button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
something?
}
});
return new ViewHolder(view);
}
This is old but I have a solution that works for me, after a lot of trial and error, and is easier than the others. 这已经过时了但我有一个适合我的解决方案,经过大量的试验和错误,比其他人更容易。 Keeping the ViewHolder RecycleronClickListeners the same and the adapter the same: 保持ViewHolder RecycleronClickListeners相同,适配器相同:
in onClick() in the Fragment: 在片段中的onClick()中:
@Override
public void onClick(View view, int position, int id) {
switch (id) {
case R.id.button_1:
ImageButton button = (ImageButton) view;
button.setImageResource(resource); // Or any other imagebutton action
break;
case R.id.button_2:
ImageButton button = (ImageButton) view;
button.setImageResource(resource); // Or any other imagebutton action
break;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.