微信页面跳转设计
實現(xiàn)功能
? 對有recycleView的頁面進行點擊跳轉(zhuǎn)設計。點擊頁面上的按鈕能夠跳轉(zhuǎn)到指定的詳情界面。考查的基礎原理是對activity的生命周期的理解以及狀態(tài)轉(zhuǎn)變操作。
創(chuàng)建詳情頁面
? 首先需要創(chuàng)建一個詳情頁面,頁面中展示商品的具體信息(基于實驗的目的,詳情頁面中的內(nèi)容只有商品圖片和一段文字描述)。
public class info_apple extends AppCompatActivity {private ImageView imageView;private TextView textView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_info_apple);imageView = findViewById(R.id.apple_pic);textView = findViewById(R.id.apple_text);String text = "We do the right thing, even when it’s not easy.";textView.setText(text);imageView.setImageResource(R.drawable.apple_logo);} }? 創(chuàng)建一個Object類型的數(shù)組,用于存放3個不同的詳情頁面。三個頁面的結構相同:
Object[] activities = {info_huawei.class,info_apple.class,info_xiaomi.class};? 在第一次實驗的基礎上,對于每一個商品類的Map對象,添加了一個詳情內(nèi)容,便于在Myadapter中調(diào)用:
listitem.put("詳情",activities[i]);創(chuàng)建跳轉(zhuǎn)
? 首先需要修改item.xml文件中的布局,添加一個button。這里使用LinearLayout的垂直布局結構將button和photo放在一起。
? 然后在Myadapter中的MyViewHolder方法中定義button對象并添加綁定:
button = itemView.findViewById(R.id.button);? 在onBindViewHolder方法中添加button的具體實現(xiàn):
holder.button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Intent intent = new Intent(context, (Class<?>) data.get(position).get("詳情"));context.startActivity(intent);} });? 在onClick函數(shù)中,需要創(chuàng)建一個intent對象,用于存放需要跳轉(zhuǎn)的頁面。傳入的第一個參數(shù)為當前頁面fragment_friend,也就是Myadapter構造函數(shù)接收的參數(shù)context;第二個參數(shù)為需要跳轉(zhuǎn)的頁面的類類型,這里就需要調(diào)用商品Map對象中的詳情內(nèi)容,并且強轉(zhuǎn)為(Class<?>)。最后調(diào)用startActivity( )函數(shù)實現(xiàn)跳轉(zhuǎn)。
最后的結果:點擊詳情按鈕后,會跳轉(zhuǎn)到當前商品的詳情頁面。
github鏈接:https://github.com/masterluoh/Wechat
總結
- 上一篇: window环境下thrift Comp
- 下一篇: 广州二手房价分析与预测