Jump button in recyclview

1> Realize function

In Experiment 2, we have realized adding recyclview and corresponding imageview in the wechat like interface. This experiment is to add a button control in recyclview and realize monitoring, so that when the mouse clicks, it can jump to another designed interface. The specific operations are as follows.

2> Add layout file to xml

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:id="@+id/textView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is Huawei p50 Purchase interface"
        android:textColor="@color/purple_500"
        android:textSize="20dp"/>
    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        app:srcCompat="@drawable/huaweip50tu" />
    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        app:srcCompat="@drawable/huaweip50" />
</LinearLayout>

3> Improve java files

To add a button in wechat, you can't add it directly in Mainactivity. I chose to add it in weixinfragment,
Similar to adding textview and imageview, I adopted a similar method to add, but one of the problems bothered me. I didn't know what type to describe an array of java types. After some research, I chose to define it with object [].
Add code in onCreateview

Object[] simple={huawei.class,pingguo.class,xiaomi.class};
for(int i=0;i< label.length;i++) {
            Map<String, Object> listitem = new HashMap<String, Object>();
            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);
        }

4> Improve the adapter file

First, declare a button control in MyViewHolder and bind it

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

Then add a description of button2 in 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);
            }
        });

Different from the previous textview and imageview, his acceptance position is written in onClick. Because there are three positions, it needs to find the correct position to jump.

5> Improve JAVA files

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

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...");  //Information is output on the cat console
        Intent intent = getIntent();
    }
}

In setContentView, we set which XML file this java file will display. Of course, huawei.xml is displayed here.

Finally, connect the above, add a button in the general layout of wechat, and click to jump. The general interface is shown in the figure:



Tags: Java Android

Posted on Sat, 23 Oct 2021 05:41:31 -0400 by sgtpepper