我需要的是当有人点击列表中的项目时在Recyclerview适配器类中实现我的自定义侦听器
我确实创建了一个接口类
public interface Selected_Item_Listener {
void onButtonClicked(boolean optional,String product_name,String ID);
}
在主要活动中,我使用了
Selected_Item_Listener selected_item_listener;
calss whatever{
init(savedInstanceState);
selected_item_listener = new Selected_Item_Listener() {
@Override
public void onButtonClicked(boolean optional, String
product_name, String ID) {
};
public void init(Bundle savedInstanceState) {
itemAdapter = new ItemAdapter(item_list,
getContext(),selected_item_listener);
LinearLayoutManager horizontalLayoutManager = new
LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
service_items.setLayoutManager(horizontalLayoutManager);
service_items.setAdapter(itemAdapter);
populateList();
}
private void populatList(){
item_list.add(new
serive_items_list_item(R.drawable.gg,"gg",""));
item_list.add(new
serive_items_list_item(R.drawable.gg,"gg",""));
item_list.add(new serive_items_list_item(R.drawable.gg,"gg
gg",""));
itemAdapter.notifyDataSetChanged();
}
}
在我的适配器类中,我添加以下代码
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemViewHolder> {
Context context;
List<serive_items_list_item> horizontalList;
Selected_Item_Listener selected_item_listener;
public ItemAdapter(List<serive_items_list_item> horizontalList, Context context,Selected_Item_Listener selected_item_listener) {
this.horizontalList = horizontalList;
this.selected_item_listener = selected_item_listener;
this.context = context;
}
@Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//inflate the layout file
View itemProductView = LayoutInflater.from(parent.getContext()).inflate(R.layout.service_item_items, parent, false);
ItemViewHolder gvh = new ItemViewHolder(itemProductView);
return gvh;
}
@Override
public void onBindViewHolder(ItemViewHolder holder, final int position) {
if (horizontalList.size() == 2){
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
params.setMargins(Main.screenWidth/8, 0, Main.screenWidth/9, 0);
holder.lay.setLayoutParams(params);
}else {
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
params.setMargins(Main.screenWidth/28, 0, Main.screenWidth/20, 0);
holder.lay.setLayoutParams(params);
}
holder.imageView.setImageResource(horizontalList.get(position).getImageResource());
holder.txtview.setText(horizontalList.get(position).getText());
holder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String productName = horizontalList.get(position).getText().toString();
if (selected_item_listener != null){
selected_item_listener.onButtonClicked(true, productName, "");
}
}
});
}
@Override
public int getItemCount() {
return horizontalList.size();
}
public class ItemViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
TextView txtview;
LinearLayout lay;
public ItemViewHolder(View view) {
super(view);
imageView=(ImageView) view.findViewById(R.id.imageview_item);
txtview=(TextView) view.findViewById(R.id.textview_title_item);
lay = (LinearLayout)view.findViewById(R.id.linear_view);
}
}
}
由于某种原因,它会在此行抛出空指针异常
selected_item_listener.onButtonClicked(true, productName, "");
selected_item_listener是null,我想知道我错过了什么?,我使用相同的类和代码与另一个扩展Dialogs并正常工作的类
答案 0 :(得分:0)
嗯,这似乎是我在初始化之前尝试将我的听众发送到我的适配器的问题
我试图把init(savedInstanceState);在selected_item_listener = new之后Selected_Item_Listener(){};它现在正在工作