实现方案/核心技术
在实验二的adapter和RecycleView的使用基础上,对有recycleView的页面的项目进行点击跳转设计
功能说明
在实验二中,我在CALL界面添加了recycleView组件,实现了类似通讯录的界面,且添加了点击效果,当点击recycleView的item的头像时,效果为查询联系人,当点击recycleView的item的名称时效果为拨号呼叫该联系人。在此基础上,我新添加了一个呼叫中的活动
,当点击recycleView的item的名称时 会跳转至呼叫活动界面。
跳转目标界面活动以及布局代码及布局效果展示
活动代码:
package com.example.homework;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}
布局代码及效果:
<?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=".MainActivity2">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="@drawable/calling"
tools:layout_editor_absoluteY="178dp"
tools:ignore="MissingConstraints" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:textColor="@color/green"
android:text="通话中"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
布局效果:
跳转功能核心代码
在原有的点击效果上,增加点击点击跳转功能。 下面展示一些 内联代码片 。
package com.example.homework;
import android.content.Context;
import android.content.Intent;
import android.media.Image;
import android.telecom.Call;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.ClipData;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Switch;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import android.widget.LinearLayout;
import java.util.ArrayList;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder> {
public Context context;
private ArrayList<CallItem> al_items;
public ItemAdapter(ArrayList<CallItem> al_items,Context context) {
this.al_items = al_items;
this.context =context;
}
static class ViewHolder extends RecyclerView.ViewHolder {
View itemView;
ImageView item_image;
TextView item_name;
public ViewHolder(@NonNull View itemView) {
super(itemView);
this.itemView=itemView;
item_image = itemView.findViewById(R.id.item_image);
item_name = itemView.findViewById(R.id.item_name);
}
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.call_fragment, parent, false);
final ViewHolder holder = new ViewHolder(view);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent1 = new Intent(context,MainActivity2.class);
context.startActivity(intent1);
int position = holder.getAdapterPosition();
CallItem item =al_items.get(position);
Toast.makeText(view.getContext(), "正在呼叫联系人:"+item.getItem_named(), Toast.LENGTH_SHORT).show();
}
});
holder.item_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = holder.getAdapterPosition();
CallItem item =al_items.get(position);
Toast.makeText(view.getContext(), "查看联系人:"+item.getItem_named(), Toast.LENGTH_SHORT).show();
}
});
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
CallItem item = al_items.get(position);
holder.item_name.setText(item.getItem_named());
holder.item_image.setImageResource(item.getItem_imageId());
}
@Override
public int getItemCount() {
return al_items.size();
}
}
效果展示:
代码仓地址
GITEE代码仓查看项目全部源代码和资源文件
|