简体   繁体   English

使用适配器和数组列表时,我需要在回收视图中的项目上实现单击侦听器 class

[英]I need to implementation click listener class on item in recycle view when using adapter and array list

I want when user click on item in recycle move on second activity.我想当用户点击回收中的项目时移动第二个活动。 I have the following adapter:我有以下适配器:

public class AdapterWassafat extends RecyclerView.Adapter<AdapterWassafat.ViewHolder> {
    private ArrayList<Item> itemsList;
    private Context mContext;

    public AdapterWassafat(ArrayList<Item> itemsList, Context mContext) {
        this.itemsList = itemsList;
        this.mContext = mContext;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Item item = itemsList.get(position);
        holder.text.setText(item.getText());
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView text;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            text = itemView.findViewById(R.id.text_card_main);
        }
    }
}

and I have this class to add on item list:我有这个 class 要添加到项目列表中:

public class Item {
    private String text;

    public Item(String text) {
        this.text = text;
    }

    public Item() {
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }
}

and this class has recycle:这个 class 有回收:

private AdapterWassafat adapter;
private RecyclerView recyclerView ;
private ArrayList<Item> myList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_wasafat);

    recyclerView = findViewById(R.id.wasafat_recyclerView);
    recyclerView.setLayoutManager(new LinearLayoutManager(getBaseContext()));

    adapter = new AdapterWassafat(getMyitem(),getBaseContext());
    recyclerView.setAdapter(adapter);
}
    private ArrayList<Item> getMyitem() {
    ArrayList<Item> list = new ArrayList<>();
    Item tips = new Item();
    tips.setText("وصفة التمر ");
    list.add(tips);

    tips = new Item();
    tips.setText("وصفة اللبن ");
    list.add(tips);

    tips = new Item();
    tips.setText("وصفة الشاي الاخضر ");
    list.add(tips);
    return list;}

What do I need to make a recycle view when user click on item move on another activity?当用户单击项目移动到另一个活动时,我需要什么来制作回收视图? Like, as user clicks on item وصفة الشاي, to swap or move on into this activity and so on.比如,当用户点击项目وصفة الشاي,交换或继续进入这个活动等等。 I tried a lot of ways to make it both select or click, but that didn't succeed.我尝试了很多方法使它既 select 或单击,但没有成功。

In the Adapter:在适配器中:

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    Item item = itemsList.get(position);
    Context context = holder.itemView.getContext();
    holder.text.setText(item.getText());
    holder.itemView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = Intent(context, YourActivity.class);
        intent.putExtra("item", item.getText())
        context.startActivity(intent);
     }
  });
}

In your Other Activity, get data as:在您的其他活动中,获取数据为:

// onCreate()
String value = getIntent().getStringExtra("item", "");

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

相关问题 Android:在“项目”上单击“侦听器”以实现列表视图基本适配器实现 - Android: On Item Click Listener for List View Base Adapter implementation 回收视图适配器我如何在单击时突出显示单个项目 - Recycle view adapter how i highlighted single item on click 单击侦听器以查看特定的卡片回收视图 - Click listener for a specific card Recycle View 我什么时候需要回收? - When do I need to recycle? 在列表视图中解析Json并将Click侦听器添加到每个列表项 - Parse Json in list view and add click listener to each list item 使用适配器类从列表视图中删除选中的项目,并从数据库中删除数据 - Delete checked item from list view and data from database, using adapter class 如何使用CardView Item在`RecyclerView.Adapter&#39;中单击侦听器 - How to add an Item Click Listener in `RecyclerView.Adapter' using CardView Item 使用阵列适配器填充列表视图 - Populating a list view using an array adapter 当我单击列表中的每个项目时,我需要获取项目的内容(图像和文本) - I need when I click in every item in the list I want to get the content of item (IMAGE AND TEXT) 如何设置“项目点击侦听器”上的视图寻呼机? 设置点击监听器不起作用 - How can I set view pager's on Item Click Listener? Set On Click Listener is not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM