在RecyclerView中實現(xiàn)button的跳轉(zhuǎn)功能
一>實現(xiàn)功能
在實驗二中我們已經(jīng)實現(xiàn)了在類微信界面添加recyclview并添加相應(yīng)的imageview,本次實驗就是在recyclview中添加一個button控件并實現(xiàn)監(jiān)聽,使鼠標點擊時可以跳轉(zhuǎn)到另外一個設(shè)計好的界面,具體操作如下。
二>在xml中添加布局文件
首先我們要設(shè)計點擊后的跳轉(zhuǎn)界面,我直接采用了淘寶中的購物界面添加了一個textview,兩個imageview。(以購買華為p50為例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="這是華為p50購買界面"
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>

三>完善java文件
要在微信中添加button,同樣不能在Mainactivity中直接添加,我選擇了在weixinFragement中添加,
和添加textview和imageview類似,我采取了類似的方法進行添加,但是在其中一個問題讓我很苦惱,要建立一個java類型的數(shù)組,不知道用什么型來描述它,經(jīng)過一番研究,我選擇了用object【】來定義它。
在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);
}
四>完善adapter文件
首先在MyViewHolder聲明一個button控件,并進行綁定
Button button2;
button2=(Button) itemView.findViewById(R.id.button2);
然后在onBindViewHolder添加button2的描述
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(),"正在努力跳轉(zhuǎn) :)",Toast.LENGTH_SHORT).show();
context.startActivity(main2);
}
});
和之前的textview和imageview有所不同,他的接受position寫在onClick中,因為有3個position,它要找到正確的位置進行跳轉(zhuǎn)。
五>完善JAVA文件
跳轉(zhuǎn)不可能直接到xml文件,它同樣需要java文件來承載它,并返回相應(yīng)的信息,我們新建了三個java文件,huawei,pingguo和xiaomi。此處我同樣以huawei為例
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..."); //信息在LogCat控制臺輸出
Intent intent = getIntent();
}
}
在setContentView中我們設(shè)置了這個java文件將顯示哪一個xml文件,此處當然顯示的huawei.xml。
最后就是要把上面的串聯(lián)起來,在微信總布局中加入button,點擊實現(xiàn)跳轉(zhuǎn),總界面如圖:





github鏈接:https://github.com/yikemi/ASwork_lbw
庫名:ASwork_lbw
到此這篇關(guān)于在RecyclerView中實現(xiàn)button的跳轉(zhuǎn)功能的文章就介紹到這了,更多相關(guān)RecyclerView實現(xiàn)button跳轉(zhuǎn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android TextView 去掉自適應(yīng)默認的fontpadding的實現(xiàn)方法
這篇文章主要介紹了Android TextView 去掉自適應(yīng)默認的fontpadding的實現(xiàn)方法的相關(guān)資料,希望通過本文大家能夠掌握這部分內(nèi)容,需要的朋友可以參考下2017-09-09
Android基于方法池與回調(diào)實現(xiàn)登錄攔截的場景
這篇文章主要為大家介紹了Android基于方法池與回調(diào)實現(xiàn)登錄攔截的場景詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08
Android動態(tài)修改ToolBar的Menu菜單示例
本篇文章主要介紹了Android動態(tài)修改ToolBar的Menu菜單示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02
android studio 使用adb 命令傳遞文件到android 設(shè)備的方法
這篇文章主要介紹了android studio 使用adb 命令傳遞文件到android 設(shè)備的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-11-11
android中colors.xml顏色設(shè)置資源文件的方法
這篇文章主要介紹了android中colors.xml顏色設(shè)置資源文件,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03

