android之GridView&RecycleView
一、GridView
1、简介:GridView是一个在二维可滚动的网格中展示内容的控件。网格中的内容通过使用adapter2
自动插入到布局中。
2、案例:手机的功能菜单界面
3、GridView的xml文件:
<?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="match_parent">
<GridView
android:id="@+id/menu_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"
android:layout_margin="20dp"
/>
</LinearLayout>
4、适配器的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:orientation="vertical">
<ImageView
android:id="@+id/menu_img"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_margin="5dp"/>
<TextView
android:id="@+id/menu_text"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:textSize="24dp"
android:layout_margin="5dp"/>
</LinearLayout>
5、GridView的逻辑代码:
package com.me.androidstudy;
import android.app.Activity;
import android.os.Bundle;
import android.widget.GridView;
import androidx.annotation.Nullable;
import com.me.adapter.MenuAdapter;
import com.me.domain.MenuItem;
import java.util.ArrayList;
import java.util.List;
public class Activity_grid extends Activity {
GridView menu_gridView;
MenuAdapter menuAdapter;
List<MenuItem> data = new ArrayList<>();
public void initView(){
menu_gridView = findViewById(R.id.menu_grid);
menuAdapter = new MenuAdapter();
}
public void action(){
data.add(new MenuItem("二手车",R.drawable.car));
data.add(new MenuItem("彩票",R.drawable.cp));
data.add(new MenuItem("日历",R.drawable.data));
data.add(new MenuItem("记事本",R.drawable.jsb));
data.add(new MenuItem("新闻热点",R.drawable.news));
data.add(new MenuItem("网络设置",R.drawable.wf));
data.add(new MenuItem("天气",R.drawable.tq));
data.add(new MenuItem("视频",R.drawable.tv));
data.add(new MenuItem("应用市场",R.drawable.yysc));
menuAdapter.setData(data);
menu_gridView.setAdapter(menuAdapter);
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grid);
initView();
action();
}
}
6、适配器数据项实体:
package com.me.domain;
public class MenuItem {
private String menuTitle;
private int res_id;
public MenuItem(String menuTitle, int res_id) {
this.menuTitle = menuTitle;
this.res_id = res_id;
}
public String getMenuTitle() {
return menuTitle;
}
public void setMenuTitle(String menuTitle) {
this.menuTitle = menuTitle;
}
public int getRes_id() {
return res_id;
}
public void setRes_id(int res_id) {
this.res_id = res_id;
}
}
7、适配器的逻辑代码:(baseAdapter)
package com.me.adapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.me.androidstudy.R;
import com.me.domain.MenuItem;
import java.util.ArrayList;
import java.util.List;
public class MenuAdapter extends BaseAdapter {
List<MenuItem> data = new ArrayList<>();
public void setData(List<MenuItem> data) {
this.data = data;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View menuItem = LayoutInflater.from(parent.getContext()).inflate(R.layout.menu_adapter, null);
ImageView menu_img = menuItem.findViewById(R.id.menu_img);
TextView menu_text = menuItem.findViewById(R.id.menu_text);
menu_img.setBackgroundResource(data.get(position).getRes_id());
menu_text.setText(data.get(position).getMenuTitle());
return menuItem;
}
}
二、RecycleView
1、简介:RecyclerView是Android一个更强大的控件,其不仅可以实现和ListView同样的效果,还有优化了ListView中的各种不足。其可以实现数据纵向滚动,也可以实现横向滚动(ListView做不到横向滚动)。接下来讲解RecyclerView的用法。
2、案例:学生信息列表(图片+文字)
3、依赖:
dependencies
{
implementation 'androidx.recyclerview:recyclerview:1.1.0'
}
4、RecycleView的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:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/students"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
5、适配器的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="#1DEBE6E6"
android:orientation="horizontal">
<ImageView
android:id="@+id/sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"/>
<TextView
android:id="@+id/cuo"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:textSize="24dp"
android:layout_margin="5dp"/>
</LinearLayout>
6、RecycleView的逻辑代码:
package com.me.androidstudy;
import android.app.Activity;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.me.domain.Student;
import java.util.ArrayList;
import java.util.List;
import com.me.adapter.StudentAdapter;
public class Activity_Rec extends Activity {
RecyclerView studentRec ;
List<Student> data = new ArrayList<>();
StudentAdapter adapter = new StudentAdapter();
public void initView(){
studentRec = findViewById(R.id.students);
}
public void action(){
for (int i = 0; i < 50; i++) {
Student student = new Student();
student.setName("笑天"+i);
student.setId(20180001+i);
student.setSex(i%2==0?"男":"女");
data.add(student);
}
adapter.setData(data);
/**
*
* 必须加 studentRec.setLayoutManager(new LinearLayoutManager(this));
* */
studentRec.setLayoutManager(new LinearLayoutManager(this));
studentRec.setAdapter( adapter);
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.student_rec);
initView();
action();
}
}
7、适配器数据项实体:
package com.me.domain;
public class Student {
private int id;
private String name;
private String sex;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "学号:" + id +
"\n姓名:" + name +
"\n性别:" + sex + "\n";
}
}
8、适配器的逻辑代码:(RecyclerView.Adapter)
package com.me.adapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.me.androidstudy.R;
import com.me.domain.Student;
import java.util.ArrayList;
import java.util.List;
public class StudentAdapter extends RecyclerView.Adapter<com.me.adapter.StudentAdapter.MyViewHolder> {
List<Student> data = new ArrayList<>();
public void setData(List<Student> data) {
this.data = data;
Log.e("rec",data.size()+"");
}
@NonNull
@Override
public com.me.adapter.StudentAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View itemView = layoutInflater.inflate(R.layout.student_adapter,parent,false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull com.me.adapter.StudentAdapter.MyViewHolder holder, int position) {
Log.e("rec",data.get(position).getName()+"p");
String text = data.get(position).getName()+" "+data.get(position).getId()+" "+data.get(position).getSex();
holder.info.setText(text);
if(data.get(position).getSex().equals("男")){
holder.sex.setBackgroundResource(R.drawable.m1);
}else{
holder.sex.setBackgroundResource(R.drawable.f1);
}
}
@Override
public int getItemCount() {
return data.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView info ;
ImageView sex;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
info = itemView.findViewById(R.id.cuo);
sex = itemView.findViewById(R.id.sex);
Log.e("rec",data.get(0).getName());
Log.e("rec","1234");
}
}
}