在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中利用C++處理Bitmap對象的實現(xiàn)方法
下面小編就為大家?guī)硪黄狝ndroid中利用C++處理Bitmap對象的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03解決Android studio xml界面無法預(yù)覽問題
這篇文章主要介紹了解決Android studio xml界面無法預(yù)覽問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android布局——Preference自定義layout的方法
PreferenceActivity是一個方便設(shè)置管理的界面,但是對于界面顯示來說比較單調(diào),所以自定義布局就很有必要了,下面與大家分享下Preference中自定義layout的方法2013-06-06Android實現(xiàn)Flip翻轉(zhuǎn)動畫效果
這篇文章主要介紹了Android實現(xiàn)Flip翻轉(zhuǎn)動畫效果,對Android程序設(shè)計人員有很好的參考借鑒價值,需要的朋友可以參考下2014-08-08Templates實戰(zhàn)之更優(yōu)雅實現(xiàn)自定義View構(gòu)造方法詳解
本篇文章介紹如何利用Android Studio提供的Live Templates更優(yōu)雅實現(xiàn)自定義View的構(gòu)造方法,說句人話就是:簡化自定義View構(gòu)造參數(shù)模板代碼的編寫,實現(xiàn)自動生成,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09一文帶你了解Android?Flutter中Transform的使用
flutter的強大之處在于,可以對所有的widget進行Transform,因此可以做出非??犰诺男Ч1疚木蛠泶蠹伊私庖幌耇ransform的具體使用,感興趣的可以了解一下2023-01-01