我首先开发了一个简单的记事本应用程序,我使用listView来显示所有注释。但是现在我正在使用RecyclerView。当我使用 ListView 时,我使用OnItemClickListener将数据传递到另一个 Activity 来编辑笔记
现在我混淆了在Recylerview中的操作

对于listView我正在使用它

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            name = filenames.get(position).getName();
            note = filenames.get(position).getShorttext();
            Alert(); // this method is in main activity

        }
    });*/


  public void Alert()
   {
    final AlertDialog dialog;
    View mview = getLayoutInflater().inflate(R.layout.dialog_pass,null);
    final EditText mEdittext = (EditText) mview.findViewById(R.id.Epass);
    AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
    mBuilder.setView(mview);
    mBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            String col = mEdittext.getText().toString();
            String password = dBhelper.searchpass(col);
            if (col.equals(password)) {
                Intent intent =  new Intent(MainActivity.this,Note2.class);
                intent.putExtra("Name",name);
                intent.putExtra("Note",note);
                startActivity(intent);
            } else {
                Toast temp = Toast.makeText(MainActivity.this, "Password does not match", Toast.LENGTH_SHORT);
                temp.show();
            }

        }
    });
    mBuilder.setNegativeButton("Cancel",null);
    mBuilder.setCancelable(false);
    dialog = mBuilder.create();
    dialog.show();

     }

现在我该怎么做以进行回收查看,请帮助
  public class RecycleViewAdapter extends
  RecyclerView.Adapter<RecycleViewHolder> {// Recyclerview will extend to
  private List<FileName> fileNames;
  private Context context;

  public RecycleViewAdapter(Context context,List<FileName> fileNames) {
    this.context = context;
    this.fileNames = fileNames;

}
@Override
public RecycleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater mInflater = LayoutInflater.from(parent.getContext());

    ViewGroup mainGroup = (ViewGroup) mInflater.inflate(
            R.layout.grid_item, parent, false);
    RecycleViewHolder listHolder = new RecycleViewHolder(mainGroup);
    return listHolder;

}

@Override
public void onBindViewHolder(RecycleViewHolder holder, final int position) {
    final FileName model = fileNames.get(position);
    RecycleViewHolder mainHolder = (RecycleViewHolder) holder;// holder
    mainHolder.title.setText(model.getName());
    mainHolder.note.setText(model.getShorttext());

}

@Override
public int getItemCount() {
    return (null != fileNames ? fileNames.size() : 0);
  }

最佳答案

您可以使用两种方法处理此问题

1)。手势触摸
https://www.google.co.in/amp/sapandiwakar.in/recycler-view-item-click-handler/amp/

2)。使用适配器中的接口(interface)
https://antonioleiva.com/recyclerview-listener/

我建议使用界面的第二种方式

如何使用界面进行回收

public class RecycleViewAdapter extends
  RecyclerView.Adapter<RecycleViewHolder> {// Recyclerview will extend to
  private List<FileName> fileNames;
  private Context context;

//declare interface
 private OnItemClicked onClick;

//make interface like this
    public interface OnItemClicked {
        void onItemClick(int position);
    }

  public RecycleViewAdapter(Context context,List<FileName> fileNames) {
    this.context = context;
    this.fileNames = fileNames;

现在将点击分配给界面
  @Override
    public void onBindViewHolder(RecycleViewHolder holder, final int position) {
        //............//
        holder.title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClick.onItemClick(position);
            }
        });

}

在适配器类的末尾,从结束括号开始,采用一种方法将itemclick分配给接口(interface)
public void setOnClick(OnItemClicked onClick)
{
    this.onClick=onClick;
}

在MainActivity.java中
将项目单击与适配器绑定(bind)
public class MainActivity extends Activity implements OnItemClicked {

    private RecyclerView mRecyclerView;
    private CityAdapter mAdapter;
    private List<City> cities;

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

        mRecyclerView = (RecyclerView)findViewById(R.id.list);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        mAdapter = new CityAdapter(cities, R.layout.row_city, this);
        mRecyclerView.setAdapter(mAdapter);

        mAdapter.setOnClick(MainActivity.this); // Bind the listener
    }

    @Override
    public void onItemClick(int position) {
        // The onClick implementation of the RecyclerView item click
    //ur intent code here
}

对于Kotlin代码,请引用以下链接

How to handle recyclerview item click in kotlin?

希望这项帮助对您有帮助吗?

关于android - 如何将Onclick监听器添加到回收者 View ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44151979/

10-12 06:05