在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)聽,使鼠標(biāo)點(diǎn)擊時(shí)可以跳轉(zhuǎn)到另外一個(gè)設(shè)計(jì)好的界面,具體操作如下。
二>在xml中添加布局文件
首先我們要設(shè)計(jì)點(diǎn)擊后的跳轉(zhuǎn)界面,我直接采用了淘寶中的購物界面添加了一個(gè)textview,兩個(gè)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類似,我采取了類似的方法進(jìn)行添加,但是在其中一個(gè)問題讓我很苦惱,要建立一個(gè)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聲明一個(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寫在onClick中,因?yàn)橛?個(gè)position,它要找到正確的位置進(jìn)行跳轉(zhuǎn)。
五>完善JAVA文件
跳轉(zhuǎn)不可能直接到xml文件,它同樣需要java文件來承載它,并返回相應(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中我們設(shè)置了這個(gè)java文件將顯示哪一個(gè)xml文件,此處當(dāng)然顯示的huawei.xml。
最后就是要把上面的串聯(lián)起來,在微信總布局中加入button,點(diǎn)擊實(shí)現(xiàn)跳轉(zhuǎn),總界面如圖:
github鏈接:https://github.com/yikemi/ASwork_lbw
庫名:ASwork_lbw
到此這篇關(guān)于在RecyclerView中實(shí)現(xiàn)button的跳轉(zhuǎn)功能的文章就介紹到這了,更多相關(guān)RecyclerView實(shí)現(xiàn)button跳轉(zhuǎn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android recycleView的應(yīng)用和點(diǎn)擊事件實(shí)例詳解
- Android 中RecycleView實(shí)現(xiàn)item的點(diǎn)擊事件
- Recycleview實(shí)現(xiàn)無限自動(dòng)輪播
- Android實(shí)現(xiàn)Recycleview懸浮粘性頭部外加右側(cè)字母導(dǎo)航
- 去掉RecycleView或者ListView上下滑動(dòng)陰影的方法
- Android移動(dòng)開發(fā)recycleView的頁面點(diǎn)擊跳轉(zhuǎn)設(shè)計(jì)實(shí)現(xiàn)
相關(guān)文章
Android中利用C++處理Bitmap對象的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄狝ndroid中利用C++處理Bitmap對象的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03解決Android studio xml界面無法預(yù)覽問題
這篇文章主要介紹了解決Android studio xml界面無法預(yù)覽問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android布局——Preference自定義layout的方法
PreferenceActivity是一個(gè)方便設(shè)置管理的界面,但是對于界面顯示來說比較單調(diào),所以自定義布局就很有必要了,下面與大家分享下Preference中自定義layout的方法2013-06-06Android實(shí)現(xiàn)Flip翻轉(zhuǎn)動(dòng)畫效果
這篇文章主要介紹了Android實(shí)現(xiàn)Flip翻轉(zhuǎn)動(dòng)畫效果,對Android程序設(shè)計(jì)人員有很好的參考借鑒價(jià)值,需要的朋友可以參考下2014-08-08Android開發(fā)實(shí)現(xiàn)文件存儲(chǔ)功能
這篇文章主要為大家詳細(xì)介紹了Android開發(fā)實(shí)現(xiàn)文件存儲(chǔ)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07Templates實(shí)戰(zhàn)之更優(yōu)雅實(shí)現(xiàn)自定義View構(gòu)造方法詳解
本篇文章介紹如何利用Android Studio提供的Live Templates更優(yōu)雅實(shí)現(xiàn)自定義View的構(gòu)造方法,說句人話就是:簡化自定義View構(gòu)造參數(shù)模板代碼的編寫,實(shí)現(xiàn)自動(dòng)生成,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09一文帶你了解Android?Flutter中Transform的使用
flutter的強(qiáng)大之處在于,可以對所有的widget進(jìn)行Transform,因此可以做出非??犰诺男Ч?。本文就來大家了解一下Transform的具體使用,感興趣的可以了解一下2023-01-01Android入門之計(jì)時(shí)器Chronometer的使用教程
Chronometer是一個(gè)簡單的定時(shí)器,你可以給它一個(gè)開始時(shí)間,并以此定時(shí)。本文將利用個(gè)簡單的示例為大家講解一下它的使用,感興趣的小伙伴可以嘗試一下2022-11-11