为什么即使在Recyclerview的项目上实现了OnClickListner,也没有调用onClick方法?

时间:2019-04-09 12:34:13

标签: android android-recyclerview

我试图在RecyclerView的每个项目上实现onClick方法。但是,即使在适配器中添加并实现了接口以提供单击功能之后,该功能仍无法正常工作。

我已经在适配器中添加了一个界面来添加点击功能。在界面中,有一种方法可以在recyclerview的每个项目上添加点击功能。然后,该接口由MainActivity实现,以覆盖接口的click方法。但是该操作未执行。

这是我的适配器

公共类AddNewDocAdapter扩展了RecyclerView.Adapter {

private String mTitle[];
Context mContext;
private AddNewDocAdapterOnClickHandler mClickHandler;

public interface AddNewDocAdapterOnClickHandler{
    void onClick(int q);
}

public AddNewDocAdapter(Context context,AddNewDocAdapterOnClickHandler clickHandler){
    mContext = context;
    mClickHandler = clickHandler;
}

@NonNull
@Override
public AddNewDocAdapter.AddNewDocAdapterViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    Context context = viewGroup.getContext();
    int layoutIdForListItem = R.layout.new_doc_list_items;
    LayoutInflater inflater = LayoutInflater.from(context);
    boolean shouldAttachToParentImmediately = false;
    View view = inflater.inflate(layoutIdForListItem, viewGroup, shouldAttachToParentImmediately);
    return new AddNewDocAdapterViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull AddNewDocAdapter.AddNewDocAdapterViewHolder addNewDocAdapterViewHolder, int i) {
    String currentTitle = mTitle[i];
    addNewDocAdapterViewHolder.mDocTitle.setText(currentTitle);
}

@Override
public int getItemCount() {
    if(mTitle.length == 0){
        return 0;
    }else{ return mTitle.length; }
}

public class AddNewDocAdapterViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    TextView mDocTitle;

    public AddNewDocAdapterViewHolder(@NonNull View itemView) {
        super(itemView);
        mDocTitle = itemView.findViewById(R.id.doc_title_tv);
    }

    @Override
    public void onClick(View v) {
        int adapterPosition = getAdapterPosition();
        mClickHandler.onClick(adapterPosition);
        Log.v("AddNewDocAdapter------>",""+adapterPosition);
    }
}

public void setDocTitle(String[] title) { mTitle = title; }

}

这是我的MainActivity

公共类MainActivity扩展了AppCompatActivity实现的AddNewDocAdapter.AddNewDocAdapterOnClickHandler {

private RecyclerView mRecyclerView;
private AddNewDocAdapter mAdapter;
private String[] newList;

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

    String[] initialList = {
            "New Document",
            "New Document",
            "New Document",
            "New Document",
            "New Document",
            "New Document",
            "New Document"
    };

    mRecyclerView = findViewById(R.id.doc_list_rv);
    mAdapter = new AddNewDocAdapter(MainActivity.this,MainActivity.this);
    mAdapter.setDocTitle(initialList);

    LinearLayoutManager layoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
    mRecyclerView.setLayoutManager(layoutManager);
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setAdapter(mAdapter);
}

@Override
public void onClick(int q) {
    Context context = this;
    Log.v("Mainactivity",""+q);
    Class destinationClass = CreatePostActivity.class;
    Intent intentToStartDetailActivity = new Intent(context, destinationClass);
    startActivity(intentToStartDetailActivity);
  }
}

我希望单击任何项​​目都可以启动CreatePostActivity类。但是什么也没发生。

0 个答案:

没有答案