1.添加布局
fragment布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="@color/collect_back"
android:layout_height="match_parent"
android:orientation="vertical" >
<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_1"
android:background="@color/collect_back"
/>
<RelativeLayout
android:id="@+id/add_person_layout"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_40"
android:background="@color/home_background">
<ImageView
android:id="@+id/person_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/dp_7"
android:layout_centerVertical="true"
android:src="@mipmap/person_logo"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/person_logo"
android:layout_marginLeft="@dimen/dp_7"
android:layout_centerVertical="true"
android:text="@string/control_add_person"
android:textColor="@color/home_blue"
android:textSize="@dimen/sp_13"
/>
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/person_list"
android:layout_marginTop="@dimen/dp_7"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
item布局:
<?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="@dimen/dp_130"
android:orientation="vertical"
android:background="@color/home_background">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/dp_7"
android:background="@color/collect_back">
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/name"
android:text="张三"
android:textSize="@dimen/sp_12"
android:textColor="@color/info_text"
android:gravity="left"
android:layout_alignParentStart="true"
android:layout_marginLeft="@dimen/dp_14"
android:layout_marginTop="@dimen/dp_14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/number"
android:text="415951199001011113"
android:textSize="@dimen/sp_12"
android:textColor="@color/info_text"
android:gravity="left"
android:layout_toRightOf="@+id/name"
android:layout_alignTop="@+id/name"
android:layout_marginLeft="@dimen/dp_14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/command_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/control_command"
android:textSize="@dimen/sp_12"
android:textColor="@color/info_text"
android:layout_below="@+id/name"
android:layout_alignLeft="@+id/name"
android:layout_marginTop="@dimen/dp_7"
/>
<TextView
android:id="@+id/command"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="缉拿"
android:textSize="@dimen/sp_12"
android:textColor="@color/info_text"
android:layout_toRightOf="@+id/command_title"
android:layout_alignTop="@+id/command_title"
android:layout_marginLeft="@dimen/dp_7"
/>
<TextView
android:id="@+id/time_title"
android:text="布控时间:"
android:textSize="@dimen/sp_9"
android:textColor="@color/control_time"
android:layout_below="@+id/command_title"
android:layout_alignLeft="@+id/name"
android:layout_marginTop="@dimen/dp_7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/start_time"
android:text="1970-01-01 00:00"
android:textSize="@dimen/sp_9"
android:textColor="@color/control_time"
android:layout_alignTop="@+id/time_title"
android:layout_toRightOf="@+id/time_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/to"
android:text="至"
android:textSize="@dimen/sp_9"
android:textColor="@color/control_time"
android:layout_toRightOf="@+id/start_time"
android:layout_alignTop="@+id/start_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/end_time"
android:text="2018-01-01 00:00"
android:textSize="@dimen/sp_9"
android:textColor="@color/control_time"
android:layout_alignTop="@+id/time_title"
android:layout_toRightOf="@+id/to"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_1"
android:layout_below="@+id/end_time"
android:background="@color/collect_back"
android:layout_marginStart="@dimen/dp_14"
android:layout_marginEnd="@dimen/dp_14"
android:layout_marginTop="@dimen/dp_7"
/>
<Button
android:id="@+id/delete"
android:layout_width="@dimen/dp_45"
android:layout_height="@dimen/dp_22"
android:text="@string/control_delete"
android:background="@drawable/radius3_grey_gb"
android:layout_below="@+id/view"
android:layout_marginTop="@dimen/dp_7"
android:layout_alignParentRight="true"
android:layout_marginEnd="@dimen/dp_14"
/>
<Button
android:id="@+id/edit"
android:layout_width="@dimen/dp_45"
android:layout_height="@dimen/dp_22"
android:text="@string/control_edit"
android:background="@drawable/radius3_grey_gb"
android:layout_below="@+id/view"
android:layout_marginTop="@dimen/dp_7"
android:layout_toLeftOf="@id/delete"
android:layout_marginRight="@dimen/dp_18"
/>
</RelativeLayout>
</LinearLayout>
2.在fragment中绑定控件->设置布局管理器->绑定adapter
还可以设置分割线recyclerView.addItemDecoration
public class ControlPersonFragment extends Fragment {
private RecyclerView recyclerView;
private List<ControlPerson> controlPersonList = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = View.inflate(getActivity(), R.layout.fragment_control_person, null);
initPerson();
recyclerView = (RecyclerView) view.findViewById(R.id.person_list);
LinearLayoutManager layoutmanager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutmanager);
ControlPersonAdapter adapter = new ControlPersonAdapter(controlPersonList);
recyclerView.setAdapter(adapter);
// recyclerView.addItemDecoration(new MyItemDecoration(this.getActivity(), MyItemDecoration.HORIZONTAL_LIST));
return view;
}
private void initPerson() {
ControlPerson controlPerson1 = new ControlPerson();
controlPerson1.setCommand("缉拿");
controlPerson1.setEndTime("2018-01-01 00:00");
controlPerson1.setStartTime("2018-01-01 00:00");
controlPerson1.setName("张三");
controlPerson1.setNumber("170726367");
controlPerson1.setInControl(true);
controlPersonList.add(controlPerson1);
ControlPerson controlPerson2 = new ControlPerson();
controlPerson2.setCommand("缉拿");
controlPerson2.setEndTime("2018-01-01 00:00");
controlPerson2.setStartTime("2018-01-01 00:00");
controlPerson2.setName("张三");
controlPerson2.setNumber("170726367");
controlPerson2.setInControl(true);
controlPersonList.add(controlPerson2);
}
}
3.接下来取Adapter进行初始化
①创建内部类ViewHolder,初始化绑定holder中的控件
②保存创建Adapter时候的list数组,作后续提取数据给item用
③OnCreateViewHolder和OnBindViewHolder
onCreateViewHolder()方法,负责承载每个子项的布局。它有两个参数,其中一个是 int viewType;这个viewType是属于list里面的对象的一个属性之一,上边的代码没有用到,使用的话再override一个getItemViewType去获取到这个值就行了,获取到其type后可以对这一条的样式啊、是否根目录啊做些处理。
参考:https://blog.csdn.net/csdn_aiyang/article/details/80094302
onBindViewHolder()方法,负责将每个子项holder绑定数据。俩参数分别是RecyclerView.ViewHolder holder, int position;
public class ControlPersonAdapter extends RecyclerView.Adapter<ControlPersonAdapter.ViewHolder> {
private final List<ControlPerson> controlPerson;
private Context context;
static class ViewHolder extends RecyclerView.ViewHolder{
TextView name;
TextView number;
TextView commandTitle;
TextView command;
TextView startTime;
TextView endTime;
Button edit,delete;
public ViewHolder(View view) {
super(view);
name = (TextView)view.findViewById(R.id.name);
number = (TextView)view.findViewById(R.id.number);
commandTitle = (TextView)view.findViewById(R.id.command_title);
command = (TextView)view.findViewById(R.id.command);
startTime = (TextView)view.findViewById(R.id.start_time);
endTime = (TextView)view.findViewById(R.id.end_time);
edit = (Button)view.findViewById(R.id.edit);
delete = (Button)view.findViewById(R.id.delete);
}
}
public ControlPersonAdapter(List<ControlPerson> controlPerson){
this.controlPerson = controlPerson;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_control,parent,false);
context = parent.getContext();
ViewHolder holder = new ViewHolder(view);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
return holder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.name.setText(controlPerson.get(position).name);
holder.number.setText(controlPerson.get(position).number);
holder.command.setText(controlPerson.get(position).command);
holder.startTime.setText(controlPerson.get(position).startTime);
holder.endTime.setText(controlPerson.get(position).endTime);
if(controlPerson.get(position).isInControl){
holder.command.setTextColor(ContextCompat.getColor(context,R.color.car_normal));
holder.commandTitle.setTextColor(ContextCompat.getColor(context,R.color.car_normal));
}
holder.edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//todo getong 26367 这里要打开新的界面编辑
Toast.makeText(context,"编辑",Toast.LENGTH_SHORT).show();
}
});
holder.delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//todo getong 26367 做删除
Toast.makeText(context,"删除",Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return controlPerson.size();
}
}
第一次用时候有个坑,自动implements的getItemCount返回值默认是0,导致代码不会走onBindViewHolder(),就一条item都不会显示。
代码效果: