目录
功能说明
对有recycleView的页面进行点击跳转设计
设计流程
- 实现列表,将上次实验 Chat上的 TextView 删除,添加一个 recyclerview。
- 重写myadapter 中的三个方法:onCreateViewHolder,onBindViewHolder ,重写 getItemCount 。
-
创建适配器 RecyclerView.Adapter。
-
完成适配器后,修改对应的 java 文件。
-
然后进行跳转设计
-
适配器里我们要加入点击监听事件,然后修改ChatFragment.java
在点击第一个事件时,会跳转到Main2Activity,点击其余事件则显示“在线”
核心代码
ChatFragment.java
package com.example.ltone;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class ChatFragment extends Fragment {
private Context context;
private List<chat> mList = new ArrayList<chat>();
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public ChatFragment() {
// Required empty public constructor
}
// TODO: Rename and change types and number of parameters
public static ChatFragment newInstance(String param1, String param2) {
ChatFragment fragment = new ChatFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.wechat, container, false);
context = (Activity) view.getContext();
InitData();
RecyclerView recyclerView = view.findViewById(R.id.recyclerview);
myadapter adapter = new myadapter(context,mList);
recyclerView.setAdapter(adapter);
LinearLayoutManager manager = new LinearLayoutManager(context);
manager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(manager);
recyclerView.addItemDecoration(new DividerItemDecoration(context,LinearLayoutManager.VERTICAL ));
myadapter.setRecyclerItemClickListener(new myadapter.OnRecyclerItemClickListener() {
@Override
public void onRecyclerItemClick(int position) {
Toast.makeText(getActivity(),"在线",Toast.LENGTH_LONG).show();
if (position==0){
Intent intent=new Intent(getActivity(),Main2Activity.class);
startActivity(intent);}
}
});
return view;
}
private void InitData() {
for (int i = 0; i < 10; i++) {
chat friend_1 = new chat(i, R.drawable.friend_1, "李1","在吗");
mList.add(friend_1);
chat friend_2= new chat(i, R.drawable.friend_2, "李2","在吗");
mList.add(friend_2);
chat friend_3 = new chat(i, R.drawable.friend_3, "李3","你好");
mList.add(friend_3);
chat friend_4 = new chat(i, R.drawable.friend_4, "李4","哈喽");
mList.add(friend_4);
chat friend_5 = new chat(i, R.drawable.friend_5, "李5","你好");
mList.add(friend_5);
chat friend_6 = new chat(i, R.drawable.friend_6, "李6","哈喽");
mList.add(friend_6);
chat friend_7 = new chat(i, R.drawable.friend_7, "李7","哈喽");
mList.add(friend_7);
chat friend_8 = new chat(i, R.drawable.friend_8, "李8","早上好");
mList.add(friend_8);
chat friend_9 = new chat(i, R.drawable.friend_9, "李9","你好");
mList.add(friend_9);
chat friend_10 = new chat(i, R.drawable.friend_10, "李10","在吗");
mList.add(friend_10);
}
}
}
myadapter.java
package com.example.ltone;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.collection.CircularArray;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class myadapter extends RecyclerView.Adapter<myadapter.Myviewholder> {
private static OnRecyclerItemClickListener onItemClickListener;
private List<chat> mList;
private Context context;
public myadapter(Context context,List<chat> list) {
this.mList= list;
this.context=context;
}
public myadapter() {
}
@NonNull
@Override
public Myviewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType){
View view=(View)LayoutInflater.from(parent.getContext()).inflate(R.layout.item_down,parent,false);
Myviewholder myviewholder= new Myviewholder((view));
return myviewholder;
}
@Override
public void onBindViewHolder(@NonNull Myviewholder holder, int position) {
chat chat = mList.get(position);
holder.Image.setImageResource(chat.getImageid());
holder.Name.setText(chat.getName());
holder.Message.setText(chat.getMessage());
// holder.itemView.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// Intent intent=new Intent(context,frdFragment.class);
// context.startActivities(new Intent[]{intent});
// }
// });
}
@Override
public int getItemCount() {
return mList.size();
}
public class Myviewholder extends RecyclerView.ViewHolder{
private ImageView Image;
private TextView Name;
private TextView Message;
public Myviewholder(@NonNull View itemView) {
super(itemView);
Image = itemView.findViewById(R.id.item_img);
Name = itemView.findViewById(R.id.item_name);
Message = itemView.findViewById(R.id.item_message);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(onItemClickListener!=null){
onItemClickListener.onRecyclerItemClick(getAdapterPosition());
}
}
});
}
}
public static void setRecyclerItemClickListener(OnRecyclerItemClickListener listener){
onItemClickListener=listener;
}
public interface OnRecyclerItemClickListener{
void onRecyclerItemClick(int position);
}
}
wechat.xml
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutDescription="@xml/wechat_scene" >
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp" />
</LinearLayout>
item_down.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff">
<!--左侧的图片布局盒子-->
<LinearLayout
android:id="@+id/ll_1"
android:layout_width="wrap_content"
android:layout_height="125dp"
android:gravity="center">
<ImageView
android:id="@+id/item_img"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:src="@drawable/com"
android:background="#DFDFDF"/>
</LinearLayout>
<!--右侧文字盒子布局-->
<LinearLayout
android:id="@+id/ll_2_name"
android:layout_width="match_parent"
android:layout_height="125dp"
android:orientation="vertical">
<!--名字TextView所在盒子布局-->
<LinearLayout
android:id="@+id/ll_2_name1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp">
<TextView
android:id="@+id/item_name"
android:layout_width="190dp"
android:layout_height="25dp"
android:text=""
android:textSize="20sp"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll_2_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<TextView
android:id="@+id/item_message"
android:layout_width="190dp"
android:layout_height="25dp"
android:text=""
android:textSize="20sp"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
结果与总结
1.实验结果
列表:
点击李1跳转:
2.总结
本次实验完成了recyclerview的使用和跳转设计,由于对安卓开发组件不熟悉导致实验过程中遇到了一些问题,不过最后都通过与同学讨论得到了解决,本次实验收获很大!
源码开源地址(gitee)