微信界面之RecycleView设计
功能分析
1.在上次微信界面设计的基础上,选择一个tab文件实现RecyclerView控件的设计开发,内容不限
2.我选择在“通讯录 ”界面进行RecyclerView的纵向布局设计通讯录名单,显示头像和姓名
实现过程
1.UI布局
2.生成项布局文件,RecyclerView每一项为头像和姓名
3.建立t通讯录实体类Person
4.编写继承类MyRecyclerAdapter
public class MyRecycleAdapter extends RecyclerView.Adapter<MyRecycleAdapter.MyViewHolder>{
//通讯录数据
private List<Person> pdata=new ArrayList<>();
//上下文
private Context context;
//构造方法
public MyRecycleAdapter(List<Person> pdata, Context context){
this.pdata=pdata;
this.context=context;
}
@NonNull
@Override
//返回一个自定义的ViewHolder
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//填充布局,获取列表项布局文件
View itemView= LayoutInflater.from(context).inflate(R.layout.contactitem,parent,false);
MyViewHolder myViewHolder=new MyViewHolder(itemView);
return myViewHolder;
}
@Override
//绑定数据,填充onCreateViewHolder方法返回的HOLDER中的控件
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
//获取通讯录数据
Person person=pdata.get(position);
holder.myimg.setImageResource(person.getImg());
holder.myname.setText(person.getName());
}
@Override
//返回通讯录数据个数
public int getItemCount() {
return pdata.size();
}
//定义内部类
class MyViewHolder extends RecyclerView.ViewHolder{
private ImageView myimg;
private TextView myname;
public MyViewHolder(View itemView){
super(itemView);
myimg=itemView.findViewById(R.id.item_img);
myname=itemView.findViewById(R.id.item_name);
}
}
}
5.在contactFragment中实现以下功能:
a.初始化数据
b.初始化控件
c.设置布局管理器
d.初始化数据适配器
e.设置动画,采用默认
f.设置适配器
public class contactFragment<context> extends Fragment {
private RecyclerView myRecycleView;
private MyRecycleAdapter myAdapter;
private Context context;
//定义数组,用于存储通讯录数
private String[] names={"张三","李四","王五","勾三","股四","弦五","小明","小红"};
private int[] imgs={R.mipmap.tx1,R.mipmap.tx2,R.mipmap.tx3,R.mipmap.tx4,R.mipmap.tx5,R.mipmap.tx6,R.mipmap.tx7,R.mipmap.tx8};
//定义一个通讯录列表,作为数据源
private List<Person> contacts=new ArrayList<Person>();
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 contactFragment() {
}
public static contactFragment newInstance(String param1, String param2) {
contactFragment fragment = new contactFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
private void initDatacontacts(){
for(int i=0;i<names.length;i++){
Person person=new Person(names[i],imgs[i]);
contacts.add(person);
}
}
@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.top03, container, false);
context=view.getContext();
//初始化数据
initDatacontacts();
//初始化控件
myRecycleView = view.findViewById(R.id.recylceView_contact);
//设置布局管理器
myRecycleView.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false));
//myRecycleView.setLayoutManager(new LinearLayoutManager());
// LinearLayoutManager manager = new LinearLayoutManager(context);
// manager.setOrientation(LinearLayoutManager.VERTICAL);
// myRecycleView.setLayoutManager(manager);
//初始化数据适配器
myAdapter=new MyRecycleAdapter(contacts,context);
//设置动画,采用默认
myRecycleView.setItemAnimator(new DefaultItemAnimator());
//设置适配器
myRecycleView.setAdapter(myAdapter);
return view;
}
}
6.效果展示