RecyclerView单击侦听器无法正常工作

时间:2016-04-14 21:12:40

标签: android android-recyclerview recycler-adapter

我的回收站视图点击监听器无法正常工作 主要活动中我的adapter.setClickListner(this);行显示错误无法解决clicklistner,我在这里做错了什么 我的适配器类在这里:

public class TimeAdaptra extends      
RecyclerView.Adapter<TimeAdaptra.RecyclerViewHolder> {
ArrayList<String> time, status;
Clicklistner clicklistner;

public TimeAdaptra(ArrayList<String> title, ArrayList<String> dec) {
    this.time = title;
    this.status = dec;

}

@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.time_slice, parent, false);
    RecyclerViewHolder recyclerViewHolder = new RecyclerViewHolder(view);
    return recyclerViewHolder;
}

@Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
    holder.myTitle.setText(time.get(position));
    holder.mydec.setText(status.get(position));
}

public void setClicklistner(Clicklistner clicklistner) {
    this.clicklistner = clicklistner;
}


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

public static class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    TextView myTitle;
    TextView mydec;

    public RecyclerViewHolder(View view) {
        super(view);
        view.setOnClickListener(this);
        myTitle = (TextView) view.findViewById(R.id.timee);
        mydec = (TextView) view.findViewById(R.id.status);
    }

    @Override
    public void onClick(View view) {

    }

}

public interface Clicklistner {
    public void itemClicked(View view, int position);
}
}

我正在实施clicklistner的活动是:

public class Slots extends AppCompatActivity implements     
TimeAdaptra.Clicklistner{
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
private ProgressDialog progress;
RecyclerView.LayoutManager manager;
private ArrayList<String> Time = new ArrayList<String>();
private ArrayList<String> Status = new ArrayList<String>();
TextView tv;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_slots);
    tv = (TextView) findViewById(R.id.status);
    conform();
    progress = new ProgressDialog(this);
    progress.setMessage("Nukeing..");
    progress.show();
    for (int i = 0; i <= 200; i++) {
        Time.add("18:00");
        Status.add("Available");
    }
    recyclerView = (RecyclerView) this.findViewById(R.id.timeslice);
    manager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(manager);
    recyclerView.setHasFixedSize(true);
    progress.hide();
    adapter = new TimeAdaptra(Time, Status);
  adapter.setClickListner(this); // THIS LINE IS SHOWING CANNOT RESOLVE CLICKLISTNER

    recyclerView.setAdapter(adapter);
    adapter.notifyDataSetChanged();


}
 @Override
public void itemClicked(View view, int position) {
    Toast.makeText(this, "position " + position + " is selected ",     
Toast.LENGTH_LONG).show();
}
}

logcat的

04-15 02:53:03.668 10814-10814/bhopal.looks.com.lookssalon I/Timeline:         
Timeline: Activity_launch_request id:bhopal.looks.com.lookssalon     
time:98591744
04-15 02:53:03.777 10814-10814/bhopal.looks.com.lookssalon D/Item: Clicked
04-15 02:53:03.945 10814-10814/bhopal.looks.com.lookssalon I/Timeline:     
Timeline: Activity_idle id: android.os.BinderProxy@3840079c time:98592021
04-15 02:53:06.115 10814-10814/bhopal.looks.com.lookssalon    
D/DatePickerDialog: onCreateView: 
04-15 02:53:07.164 10814-10814/bhopal.looks.com.lookssalon I/Timeline:   
Timeline: Activity_launch_request id:bhopal.looks.com.lookssalon   
time:98595240
04-15 02:53:07.448 10814-10814/bhopal.looks.com.lookssalon I/Timeline: 
Timeline: Activity_idle id: android.os.BinderProxy@1416d0da time:98595524

04-15 02:53:09.614 10814-10814/bhopal.looks.com.lookssalon I/Timeline:   
Timeline: Activity_launch_request id:bhopal.looks.com.lookssalon    
time:98597689
04-15 02:53:10.113 10814-10814/bhopal.looks.com.lookssalon I/Timeline:    
Timeline: Activity_idle id: android.os.BinderProxy@4f5e79a time:98598188
04-15 02:53:10.934 10814-10841/bhopal.looks.com.lookssalon D/OpenGLRenderer:   
endAllStagingAnimators on 0xb87bbc68 (RippleDrawable) with handle    
0xb8897818

1 个答案:

答案 0 :(得分:0)

你没有打电话给听众:

public static class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    TextView myTitle;
    TextView mydec;

    public RecyclerViewHolder(View view) {
        super(view);
        view.setOnClickListener(this);
        myTitle = (TextView) view.findViewById(R.id.timee);
        mydec = (TextView) view.findViewById(R.id.status);
    }

    @Override
    public void onClick(View view) {
      //Call your click listener here
      if( clickListener != null ) 
        clickListener.itemClicked(view, getAdapterPostion());
    }

}