繁体   English   中英

如何点击 Recycler View 并使用 equals 进行扫描? - Java

[英]How can I make a click for Recycler View and scan with equals? - Java

由于 ListView 不够优化,我决定切换到 Recycler View。 打我的第一个问题是这个。

我的 RecyclerView 适配器:

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {

    private List<String> mData;
    private LayoutInflater mInflater;
    private ItemClickListener mClickListener;

    // data is passed into the constructor
    MyRecyclerViewAdapter(Context context, List<String> data) {
        this.mInflater = LayoutInflater.from(context);
        this.mData = data;
    }

    // inflates the row layout from xml when needed
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.adapter_box, parent, false);
        return new ViewHolder(view);
    }

    // binds the data to the TextView in each row
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        String animal = mData.get(position);
        holder.myTextView.setText(animal);
    }

    // total number of rows
    @Override
    public int getItemCount() {
        return mData.size();
    }


    // stores and recycles views as they are scrolled off screen
    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView myTextView;

        ViewHolder(View itemView) {
            super(itemView);
            myTextView = itemView.findViewById(R.id.text_adapter);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
        }
    }

    // convenience method for getting data at click position
    String getItem(int id) {
        return mData.get(id);
    }

    // allows clicks events to be caught
    void setClickListener(ItemClickListener itemClickListener) {
        this.mClickListener = itemClickListener;
    }

    // parent activity will implement this method to respond to click events
    public interface ItemClickListener {
        void onItemClick(View view, int position);
    }

使用 ListView 我可以这样做:

ListView listView = findViewById(R.id.testL);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (parent.getItemAtPosition(position).equals("hello")) {
                    TextView details = word_dialog.findViewById(R.id.word_edit_desc);
                    details.setText("hello");
                }
            }
        });

我如何才能获得相同的结果,但只能使用回收视图?:

 @Override
    public void onItemClick(View view, int position) {

    }

如果您能帮助我,我将不胜感激!

我希望能够单击 MainActivity.java 中的回收器视图项目,我已经做到了,现在我需要能够对使用等号排序的每一行执行自己的操作


        ArrayList<String> animalNames = new ArrayList<>();
        animalNames.add("Dog");
        animalNames.add("Cow");
        animalNames.add("Camel");
        animalNames.add("Sheep");
        animalNames.add("Goat");

        // set up the RecyclerView
        recyclerView = findViewById(R.id.myList);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new MyRecyclerViewAdapter(this, animalNames);
        adapter.setClickListener(this);
        recyclerView.setAdapter(adapter);

 if (somecode.equals("Dog")){
            soundPlay(MediaPlayer.create(getBaseContext(), R.raw.star));
        }
        if (somecode.equals("Camel")){
            soundPlay(MediaPlayer.create(getBaseContext(), R.raw.tick));
        }

这是 Recyclerview 的 Inbuild Click 事件和另一种使用界面的方法

 holder.itemView.setOnClickListener(v ->

        if (animal.equals("Your animal String"){
    //Your code
    }
); 

如果想使用界面那么这是参考链接

您可以在适配器中定义一个接口,如下所示

public interface ClickListener{
    void onClick();            
}   

在片段或活动中实施您的适配器:

ClickListener listener = () ->{
    TextView details = word_dialog.findViewById(R.id.word_edit_desc);
    details.setText("hello");
}

然后使用构造函数或 setter 将接口传递给适配器,您可以在绑定时在 viewHolder 中使用接口,如下所示:

itemView.setOnClickListener(()->{
   if (your_list.get(getAdapterPosition()).equals("hello")) {
       interface_var_name.onClick()             
   }
});

                  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM