Android 作业一
目录
一 实验目标
二 实验过程
2.1 activity的生命过程
2.1.1 两个xml文件
2.1.2 item文件
2.1.3 itemactivity代码
2.1.4 resultactivity的代码
三 实验结果
四 代码仓库
一 实验目标
1、请在基础门户页面(作业1)的某个tab页面中添加recycleview,例如:新闻列表页面;
2、请在上一列表页面的基础上进行点击跳转设计。比如,某一tab页是新闻列表,则点击某一行能跳转到新闻详情页面。
二 实验过程
2.1.1下面是itemactivity.xml的布局代码,作为返回页面。
?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ItemActivity">
<TextView
android:id="@+id/item1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/item1"
android:textSize="35sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.431"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.284" />
<Button
android:id="@+id/button1"
android:layout_width="177dp"
android:layout_height="69dp"
android:text="@string/buttom1"
android:layout_marginBottom="124dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.405"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.519" />
</androidx.constraintlayout.widget.ConstraintLayout>
再往下是resultActivity.java关键代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ResultActivity">
<TextView
android:id="@+id/result_textview"
android:layout_width="312dp"
android:layout_height="67dp"
android:text="Result"
android:textSize="35sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
2.1.2
以下是item文件,用于决定了列表的主题展示界面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="这是联系人列表某一列。"
android:textSize="25sp" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:layout_weight="1"
android:text="价格"
android:textSize="25sp" />
</LinearLayout>
2.1.3 itemactivity实现最新的activityforresult功能,新建一个新的itemactivity,在itemactivity上添加按钮可收到resultactivity的回传值。
package com.example.myapplication;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class ItemActivity extends AppCompatActivity {
private Button button;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item);
Context context=this;
button=findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(context,ResultActivity.class);
intent.putExtra("name","cht");
test.launch(intent);
}
});
}
public ActivityResultLauncher test = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>(){
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == 888)
{
TextView textView=findViewById(R.id.item1);
textView.setText(result.getData().getStringExtra("result"));
Log.d("cht", "onActivityResultLauncher...");
}
}
});
@Override
protected void onStop() {
super.onStop();
Log.d("cht","这是ItemActivity的onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d("cht","这是ItemActivity的onDestroy");
}
@Override
protected void onPause() {
super.onPause();
Log.d("cht","这是ItemActivity的onPause");
}
@Override
protected void onResume() {
super.onResume();
Log.d("cht","这是ItemActivity的onResume");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d("cht","这是ItemActivity的onRestart");
}
}
2.1.4
resultActivity.java关键代码:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import java.util.Calendar;
public class ResultActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
Intent intent1=getIntent();
//ResultActivity返回data给ItemActivity
String str1=intent1.getStringExtra("name");
intent1.putExtra("result","本次联系朋友的时间是\n"+Calendar.getInstance().getTime());
setResult(888,intent1);
finish();
}
@Override
protected void onStart() {
super.onStart();
Log.d("cht","这是ResultActivity的onStart");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d("cht","这是ResultActivity的onDestroy");
}
@Override
protected void onStop() {
super.onStop();
Log.d("cht","这是ResultActivity的onStop");
}
@Override
protected void onPause() {
super.onPause();
Log.d("cht","这是ResultActivity的onPause");
}
@Override
protected void onResume() {
super.onResume();
Log.d("cht","这是ResultActivity的onResume");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d("cht","这是ResultActivity的onRestart");
}
}
adpter适配器
在原本的基础上加上监听事件的功能
package com.example.myapplication;
import static com.example.myapplication.R.id.textView8;
import static com.example.myapplication.R.id.imageView1;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
//Adapter解决view压缩问题和数据对应问题
//加<>对viewhoider重写
//Adapter需根据tem随时调整
public class Adapter extends RecyclerView.Adapter<Adapter.Myholder> {
private View inflater;
private Context context;
private List<String> list;
private ArrayList<Drawable> myimage;
public Adapter(Context context, List<String> list,ArrayList<Drawable> image) {
this.context = context;
this.list=list;
this.myimage=image;
}
//onCreateViewHolder()返回一个view 就是把item.xml压缩成一个view
@Override
public Myholder onCreateViewHolder(ViewGroup viewGroup, int i) {
inflater = LayoutInflater.from(context).inflate(R.layout.item,viewGroup, false);
Myholder Myholder=new Myholder(inflater);
return Myholder;
}
//onBindViewHolder() 将数据data与view绑定 给view一个list[j]的数据
//band 循环绑定 onBindViewHolder() 将数据与holder一一对应 不需要写fror循环 band已经做了循环
@Override
public void onBindViewHolder(Myholder holder, int j) {
holder.textView.setText(list.get(j));
}
//getItemCount() 返回view的个数
public int getItemCount() {
return list.size();
}
// 为什么要重写 列表项不一样 要根据自己想要呈现的效果来
public class Myholder extends RecyclerView.ViewHolder{
private TextView textView;
ImageView imageView;
public Myholder(View itemView) {
super(itemView);
textView=itemView.findViewById(R.id.textView8);
imageView=itemView.findViewById(imageView1);
}
}
}
//Adapter
三 实验结果:
四 实验源码: