我正在创建一个使用RecyclerView填充动态按钮和字符串的GridLayout。每个按钮和字符串一次一个地添加到视图中,每次添加一个按钮和字符串时,我想将动态字符串发送到我的按钮onClick in RecyclerViewHolder 。
我试着用这个问题作为指导:
how to send data using bundle inside RecyclerView onclick method
但他们有一组字符串和drawables发送到他们的RecyclerView.ViewHolder,而我的动态创建。这是我所在的地方,任何帮助将不胜感激!
RecyclerView.ViewHolder我想将我的字符串发送到onClick。在我的日志中,我想看到我点击弹出的按钮的名称。
public class RecyclerViewHolders extends RecyclerView.ViewHolder
implements View.OnClickListener
{
public TextView AName;
public ImageButton AButton;
ItemObject itemObject;
List<ItemObject> namelist=HomeFragment.getListdata();
public RecyclerViewHolders(View itemView) {
super(itemView);
AName = (TextView) itemView.findViewById(R.id.new_name);
AButton = (ImageButton) itemView.findViewById(R.id.new_button);
itemView.findViewById(R.id.new_button).setOnClickListener(this);
}
public void onClick(View view) {
String name = namelist.get(getAdapterPosition()).getName();
Log.d("tag_name", "Name of button that was clicked" + name);
}
}
我的Activity启动RecyclerView,我试图从中发送字符串。 &#34; createButton&#34; method是drawable和string首先从另一个方法发送到的地方:
public class HomeFragment extends Fragment {
private GridLayoutManager lLayout;
RecyclerViewAdapter rcAdapter;
private static Context mContext;
static String Name;
static Drawable draw;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = getContext();
List<ItemObject> myList = new ArrayList<>();
rcAdapter = new RecyclerViewAdapter(getActivity(), myList);
}
// onCreateView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.home_fragment, container, false);
Log.d("tag_name", "Go through onCreateView");
lLayout = new GridLayoutManager(getActivity(), 2,
GridLayoutManager.HORIZONTAL, false);
RecyclerView rView = (RecyclerView) view.findViewById(R.id.recycler_view);
rView.setHasFixedSize(true);
rView.setLayoutManager(lLayout);
rView.setAdapter(rcAdapter);
return view;
}
public void createButton(Drawable d, String aName) {
Name = aName;
draw = d;
Log.d("tag_name", "Make sure createButton is called" + rcAdapter);
rcAdapter.addItem(new ItemObject(aName, d));
}
public static List<ItemObject> getListdata() {
Log.d("tag_name", "List to send to onClickListener" + Name);
List<ItemObject> namelist = new ArrayList<>();
namelist.add(new ItemObject(Name,draw));
return namelist;
}
}
和ItemObject:
public class ItemObject {
private String name;
private Drawable d;
public ItemObject(String name, Drawable d) {
this.name = name;
this.d = d;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Drawable getPhoto() {
return d;
}
public void setPhoto(Drawable d) {
this.d = d;
}
}
我当前的错误,发生在RecyclerViewHolders中,我在其中定义&#34;字符串名称&#34;:
05-24 14:12:53.350 7295-7295/it.anddev.bradipao.janus W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x437c0160)
05-24 14:12:53.360 7295-7295/it.anddev.bradipao.janus E/AndroidRuntime: FATAL EXCEPTION: main
Process: it.anddev.bradipao.janus, PID: 7295
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at it.anddev.bradipao.janus.RecyclerViewHolders.onClick(RecyclerViewHolders.java:50)
at android.view.View.performClick(View.java:4487)
at android.view.View$PerformClick.run(View.java:18746)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:788)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:604)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
向OnClickListener
实施Fragment
,并将构造函数传递给Adapter
,然后传递给每个RecyclerViewHolder
您知道保留static
Drawable
和Name
会在您添加的每个片段中产生相同的绘图和名称吗? (最后一组)
答案 1 :(得分:1)
看起来getAdapterPosition()
返回的值大于namelist
的长度(它只是一个项目)。试着解决这个问题。
答案 2 :(得分:0)
我找到了一个帮助我解决这个问题的链接,尽管我的解决方案略有不同。对于可能有同样问题的其他人。
Passing data on click in RecyclerView.Adapter
提醒我想要完成的事情:&#34;我想看到我点击的按钮的名称弹出。&#34;因此,每个按钮单击都将是唯一的,我指定了按钮的名称。
我意识到我可以访问我的点击位置和与RecyclerViewAdapter中的点击相关联的字符串,并将我的onClickListener附加到RecyclerViewHolder。通过将我的字符串列表发送到RecyclerViewHolder,这使得解决方案比我尝试的方式更简单。
所以,在我的RecyclerViewAdapter中,我在自定义方法中创建了一个onClickListener:
private class downloadOnClickListener implements View.OnClickListener{
String aName;
public downloadOnClickListener(String file){
this.aName = file;
}
@Override
public void onClick(View v) {
Log.d("tag_name", "Name of button being pressed" + aName);
}
}
在onBindViewHolder中,我将侦听器附加到RecyclerHolder
@Override
public void onBindViewHolder(RecyclerViewHolders holder, int position) {
ItemObject itemObject = itemList.get(position);
holder.itemObject = itemObject;
holder.AButton.setOnClickListener(new downloadOnClickListener(itemList.get(position).getName()));
}