Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the jump function of button in RecyclerView

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "how to achieve the jump function of button in RecyclerView", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to achieve the jump function of button in RecyclerView" this article.

One > implement function

In experiment 2, we have implemented to add recyclview and corresponding imageview to the Wechat-like interface. This experiment is to add a button control in recyclview and achieve monitoring, so that when the mouse is clicked, we can jump to another designed interface, the specific operations are as follows.

Add layout files to xml

First of all, we have to design the jump interface after clicking. I directly adopted the shopping interface in Taobao and added one textview and two imageview. (take the purchase of Huawei p50 as an example, huawei.xml)

Improve the java document

To add button to Wechat, it also cannot be added directly in Mainactivity. I chose to add it in weixinFragement.

Similar to adding textview and imageview, I took a similar approach to add it, but one of the problems bothered me. I didn't know what type to use to describe an array of type java. After some research, I chose to use object [] to define it.

Add code to onCreateview

Object [] simple= {huawei.class,pingguo.class,xiaomi.class}; for (int item0

< label.length;i++) { Map listitem = new HashMap(); listitem.put("detail",simple[i]); listitem.put("name", label[i]); listitem.put("color", color[i]); listitem.put("price", price[i]); listitem.put("configure", config[i]); listitem.put("tutu", phone[i]); data.add(listitem); }四>

Perfect the adapter file

First declare a button control in MyViewHolder and bind it

Button button2;button2= (Button) itemView.findViewById (R.id.button2)

Then add the description of button2 to onBindViewHolder

Public void onBindViewHolder (@ NonNull MyViewHolder holder, @ SuppressLint ("RecyclerView") int position) {holder.button2.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View view) {Intent main2 = new Intent (context, (Class) data.get (position) .get ("detail")) Toast.makeText (context.getApplicationContext (), "trying to jump:)", Toast.LENGTH_SHORT) .show (); context.startActivity (main2);}})

Unlike the previous textview and imageview, his accepted position is written in onClick, because there are three position, it needs to find the right place to jump.

Improve the JAVA document

It is impossible to jump directly to the xml file. It also needs the java file to host it and return the corresponding information. We have created three new java files, huawei,pingguo and xiaomi. I will also take huawei as an example here.

Package com.example.mywork_lbw;import android.content.Intent;import android.os.Bundle;import android.util.Log;import androidx.appcompat.app.AppCompatActivity;public class huawei extends AppCompatActivity {public huawei () {} @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.huawei); Log.i ("life", "huawei is on create...") / / output Intent intent = getIntent () in the LogCat console;}}

In setContentView, we set which xml file this java file will display, and of course the huawei.xml shown here.

Finally, to concatenate the above, add button to the overall layout of Wechat, and click to jump. The overall interface is shown in the figure:

The above is all the contents of the article "how to achieve the jump function of button in RecyclerView". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report