微信页面跳转设计
實現功能
? 對有recycleView的頁面進行點擊跳轉設計。點擊頁面上的按鈕能夠跳轉到指定的詳情界面。考查的基礎原理是對activity的生命周期的理解以及狀態轉變操作。
創建詳情頁面
? 首先需要創建一個詳情頁面,頁面中展示商品的具體信息(基于實驗的目的,詳情頁面中的內容只有商品圖片和一段文字描述)。
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);} }? 創建一個Object類型的數組,用于存放3個不同的詳情頁面。三個頁面的結構相同:
Object[] activities = {info_huawei.class,info_apple.class,info_xiaomi.class};? 在第一次實驗的基礎上,對于每一個商品類的Map對象,添加了一個詳情內容,便于在Myadapter中調用:
listitem.put("詳情",activities[i]);創建跳轉
? 首先需要修改item.xml文件中的布局,添加一個button。這里使用LinearLayout的垂直布局結構將button和photo放在一起。
? 然后在Myadapter中的MyViewHolder方法中定義button對象并添加綁定:
button = itemView.findViewById(R.id.button);? 在onBindViewHolder方法中添加button的具體實現:
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函數中,需要創建一個intent對象,用于存放需要跳轉的頁面。傳入的第一個參數為當前頁面fragment_friend,也就是Myadapter構造函數接收的參數context;第二個參數為需要跳轉的頁面的類類型,這里就需要調用商品Map對象中的詳情內容,并且強轉為(Class<?>)。最后調用startActivity( )函數實現跳轉。
最后的結果:點擊詳情按鈕后,會跳轉到當前商品的詳情頁面。
github鏈接:https://github.com/masterluoh/Wechat
總結
- 上一篇: window环境下thrift Comp
- 下一篇: 广州二手房价分析与预测