在RecyclerView中實(shí)現(xiàn)button的跳轉(zhuǎn)功能
一>實(shí)現(xiàn)功能
在實(shí)驗(yàn)二中我們已經(jīng)實(shí)現(xiàn)了在類微信界面添加recyclview并添加相應(yīng)的imageview,本次實(shí)驗(yàn)就是在recyclview中添加一個(gè)button控件并實(shí)現(xiàn)監(jiān)聽(tīng),使鼠標(biāo)點(diǎn)擊時(shí)可以跳轉(zhuǎn)到另外一個(gè)設(shè)計(jì)好的界面,具體操作如下。
二>在xml中添加布局文件
首先我們要設(shè)計(jì)點(diǎn)擊后的跳轉(zhuǎn)界面,我直接采用了淘寶中的購(gòu)物界面添加了一個(gè)textview,兩個(gè)imageview。(以購(gòu)買華為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購(gòu)買界面"
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類似,我采取了類似的方法進(jìn)行添加,但是在其中一個(gè)問(wèn)題讓我很苦惱,要建立一個(gè)java類型的數(shù)組,不知道用什么型來(lái)描述它,經(jīng)過(guò)一番研究,我選擇了用object【】來(lái)定義它。
在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聲明一個(gè)button控件,并進(jìn)行綁定
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寫(xiě)在onClick中,因?yàn)橛?個(gè)position,它要找到正確的位置進(jìn)行跳轉(zhuǎn)。
五>完善JAVA文件
跳轉(zhuǎn)不可能直接到xml文件,它同樣需要java文件來(lái)承載它,并返回相應(yīng)的信息,我們新建了三個(gè)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控制臺(tái)輸出
Intent intent = getIntent();
}
}
在setContentView中我們?cè)O(shè)置了這個(gè)java文件將顯示哪一個(gè)xml文件,此處當(dāng)然顯示的huawei.xml。
最后就是要把上面的串聯(lián)起來(lái),在微信總布局中加入button,點(diǎn)擊實(shí)現(xiàn)跳轉(zhuǎn),總界面如圖:





github鏈接:https://github.com/yikemi/ASwork_lbw
庫(kù)名:ASwork_lbw
到此這篇關(guān)于在RecyclerView中實(shí)現(xiàn)button的跳轉(zhuǎn)功能的文章就介紹到這了,更多相關(guān)RecyclerView實(shí)現(xiàn)button跳轉(zhuǎn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android recycleView的應(yīng)用和點(diǎn)擊事件實(shí)例詳解
- Android 中RecycleView實(shí)現(xiàn)item的點(diǎn)擊事件
- Recycleview實(shí)現(xiàn)無(wú)限自動(dòng)輪播
- Android實(shí)現(xiàn)Recycleview懸浮粘性頭部外加右側(cè)字母導(dǎo)航
- 去掉RecycleView或者ListView上下滑動(dòng)陰影的方法
- Android移動(dòng)開(kāi)發(fā)recycleView的頁(yè)面點(diǎn)擊跳轉(zhuǎn)設(shè)計(jì)實(shí)現(xiàn)
相關(guān)文章
Android TextView 去掉自適應(yīng)默認(rèn)的fontpadding的實(shí)現(xiàn)方法
這篇文章主要介紹了Android TextView 去掉自適應(yīng)默認(rèn)的fontpadding的實(shí)現(xiàn)方法的相關(guān)資料,希望通過(guò)本文大家能夠掌握這部分內(nèi)容,需要的朋友可以參考下2017-09-09
Android基于方法池與回調(diào)實(shí)現(xiàn)登錄攔截的場(chǎng)景
這篇文章主要為大家介紹了Android基于方法池與回調(diào)實(shí)現(xiàn)登錄攔截的場(chǎng)景詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
Android動(dòng)態(tài)修改ToolBar的Menu菜單示例
本篇文章主要介紹了Android動(dòng)態(tài)修改ToolBar的Menu菜單示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
android studio 使用adb 命令傳遞文件到android 設(shè)備的方法
這篇文章主要介紹了android studio 使用adb 命令傳遞文件到android 設(shè)備的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-11-11
android中colors.xml顏色設(shè)置資源文件的方法
這篇文章主要介紹了android中colors.xml顏色設(shè)置資源文件,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03

