移动开发第二次作业——Activity页面跳转
一、编辑详情页面
页面设计布局效果如下:
布局介绍:
顶部为返回箭头,用于点击返回上一页;分享图标用于分享到社交软件
新闻标题TextView从上一页得到数据展示,发布人id,签名等都从上一页得到数据
正文部分以及头像,从数据库中获得(目前使用伪数据)。
布局代码如下:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/homepage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/back"
android:layout_width="32dp"
android:layout_height="32dp"
app:srcCompat="@drawable/back" />
<ImageView
android:id="@+id/share"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginLeft="325dp"
app:srcCompat="@drawable/share" />
</LinearLayout>
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="新闻标题"
android:textColor="@color/black"
android:textSize="28sp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:textStyle="bold" />
<LinearLayout
android:id="@+id/home_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal" >
<ImageView
android:id="@+id/profile_photo"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="left"
app:srcCompat="@drawable/me_1" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="10dp"
>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="18sp"
android:textStyle="bold"
android:text="发布人id" />
<TextView
android:id="@+id/status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发布人签名"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
>
<TextView
android:id="@+id/essay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top"
android:text="@string/essay"
/>
</LinearLayout>
</LinearLayout>
二、在原Fragment页面(首页)添加点击跳转功能
由于之前以及在Adapter中写好点击事件接口,所以只需要在FragmentWx中实现接口的方法即可:得到调用点击方法的item的position然后将对应的数据传递给下一个activity。
//private List<MegList> data;
myAdapter = new MyAdapter(data,view.getContext(), new MyAdapter.MyItemClicklistener() {
@Override
public void onClick(int position) {
//Toast.makeText(view.getContext(), "更多内容有待开发", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getActivity(), HomeActivity.class);
Bundle bundle = new Bundle();
bundle.putString("title", data.get(position).getTitle());
bundle.putString("userName",data.get(position).getUsername());
bundle.putString("status",data.get(position).getStatus());
intent.putExtras(bundle);
//携带position的调用可以清楚的知道是哪个item调用的详情页
startActivityForResult(intent,position);
}
});
二、在详情页数据展示
在详情页中获取传递过来的数据,并添加返回按钮的点击事件:点击后调用finish()方法结束这个activity回到上一页
private void createData(){
Bundle extras = getIntent().getExtras();
String heading = extras.getString("title");
String userId = extras.getString("userName");
String userStatus = extras.getString("status");
title.setText(heading);
userName.setText(userId);
status.setText(userStatus);
backImg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
至此页面跳转完成。
三、效果展示
四、实体类说明
即数据形式,将消息的类别、消息标题、发布人id、签名封装到一个对象中。
public class MegList {
//数据本身
private String title;
//组号
private String group;
//id
private String username;
//签名
private String status;
//constructor、get、set
}