Android development diary (2) classroom data display and screening

1. Data display

Since the current design simply displays the name of the classroom and the current number of people, there is no additional click control in a single piece of data, so it can be achieved with a simple listview


  1. Create a Room class, which contains all the information of a piece of classroom data
public class Room {
    
    
    private String Cbuilding;    // 教学楼名
    private String Cno;    // 教室号
    private String Cnum;    // 教室当前人数
    private String Csit;    // 教室总座位数
    private String Cfloor;    // 教室所在楼层
    private String Course1;    // 教室上午前两节课
    private String Course2;    // 教室上午后两节课
    private String Course3;    // 教室下午前两节课
    private String Course4;    // 教室下午后两节课
    private String Course5;    // 教室晚上前两节课

    Room(String Cbuilding, String Cno, String Cnum, String Csit, String Cfloor, String Course1, String Course2, String Course3, String Course4, String Course5){
    
    
        this.Cbuilding = Cbuilding;
        this.Cno = Cno;
        this.Cnum = Cnum;
        this.Csit = Csit;
        this.Cfloor = Cfloor;
        this.Course1 = Course1;
        this.Course2 = Course2;
        this.Course3 = Course3;
        this.Course4 = Course4;
        this.Course5 = Course5;
    }

    String getRoomName(){
    
    
        return Cbuilding + "-" + Cno;    // 教学楼名-教室号
    }

    String getPeopleNum(){
    
    
        return Cnum + "/" + Csit;    // 当前人数/总座位数
    }

}
  1. Specify the layout style of a single piece of data
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginStart="20dp"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        >

        <TextView
            android:id="@+id/room_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#000000"
            android:textSize="16sp"
            />

    </LinearLayout>

    <TextView
        android:id="@+id/people_num"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="10dp"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:textSize="14sp"
        android:textColor="#000000"
        tools:ignore="RelativeOverlap" />

</RelativeLayout>
  1. Create a custom adapter to associate ListView and data
public class RoomAdapter extends ArrayAdapter<Room> {
    
    
    private int resourceId;

    RoomAdapter(Context context, int textViewresourceId, List<Room> objects){
    
    
        super(context, textViewresourceId, objects);
        resourceId = textViewresourceId;
    }
	// 使用ViewHolder对控件实例进行缓存, 提高运行效率
    class ViewHolder{
    
    
        TextView roomName;
        TextView peopleNum;
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, @NonNull ViewGroup parent) {
    
    
        Room room = getItem(position);
        View view;
        ViewHolder viewHolder;

		// convertView缓存之前加载的布局, 便于重用, 提高效率
        if (convertView == null){
    
    
            view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.roomName = view.findViewById(R.id.room_name);
            viewHolder.peopleNum = view.findViewById(R.id.people_num);
            view.setTag(viewHolder);
        }else {
    
    
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();

        }
        viewHolder.roomName.setText(room.getRoomName());
        viewHolder.peopleNum.setText(room.getPeopleNum());
        return view;
    }
}
  1. Load data in Activity, first customize some data, this should be a Room list, and then load it
private List<Room> roomList;
private RoomAdapter roomAdapter;

roomList = new ArrayList<>();
roomAdapter = new RoomAdapter(this, R.layout.item_room, roomList);
roomListView.setAdapter(roomAdapter);

  1. Pull-down refresh is very in line with the user's operating habits, and it is relatively simple to implement. First, nest a layer of SwipeRefreshLayout outside the control (ListView) that needs to be pulled- down refresh , and then set the pull-down listener in Activity.
<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipe_refresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
	<ListView
	    android:id="@+id/room_list"
	    android:layout_width="match_parent"
	    android:layout_height="match_parent"
	    />
</android.support.v4.widget.SwipeRefreshLayout>
private SwipeRefreshLayout swipeRefresh;
swipeRefresh = findViewById(R.id.swipe_refresh);
swipeRefresh.setColorSchemeResources(R.color.colorAccent);
// 教室列表下拉刷新
swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
    
    
    @Override
    public void onRefresh() {
    
    
        updateRoomData();
        swipeRefresh.setRefreshing(false);
    }
});

Two, data screening

Android's Spinner control can easily achieve the effect of the drop-down menu. It contains two components, one is its own Spinner, and the other is the AppCompatSpinner provided by the v7 package. Use AppCompatSpinner to get better compatibility

Spinner's data source (the content of the drop-down menu) can introduce data defined in the xml file during layout. This method is relatively rigid and the data source can only be string, so generally set the data source and display effect in the code

The filtering function can be placed on the client side or on the server side, but in order to update the classroom situation in time, the quality control brother puts the filtering function on the server side, so the client only needs to send a request to the server after each screening. Just update the ui with the latest data


  1. Introduce AppCompatSpinner in the layout file
<android.support.v7.widget.AppCompatSpinner
		android:id="@+id/spinner1"
		android:theme="@style/Base.Widget.AppCompat.DropDownItem.Spinner"
		android:layout_width="0dp"
		android:layout_height="30dp"
		android:layout_weight="1"
		android:overlapAnchor="false"
		android:dropDownWidth="1000dp"
		>
</android.support.v7.widget.AppCompatSpinner>

When the spinner is pulled down, I think it’s better for the width to occupy the screen space, but I android:dropDownWidth="match_parent"don’t know why the setting does not work. Simply set it to 1000dp, and the effect is not bad.

  1. Customize text styles in selected and drop-down menus

    1. layout/item_select.xml
    <?xml version="1.0" encoding="utf-8"?>
    <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="40dp"
        android:textSize="14sp"
        />
    
    1. layout/item_drop.xml
    <?xml version="1.0" encoding="utf-8"?>
    <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:gravity="center"
        android:textSize="16sp"
        android:textColor="#000000"
        />
    
  2. Set the data source and add monitoring in the code (the menu item here is a simple string)

There is a problem here is that the spinner will select the first item by default when it is initialized, and at the same time it will trigger the selection event listener, plus the request event after the initialization is added by itself, it will request the server 4 times during initialization! So you must think The method eliminates the default selection when the spinner is initialized or does not trigger the listening event when the default selection is made. Some solutions are found to be invalid. Finally, it is decided to use three flag bits to determine the first selection of each spinner separately, and eliminate the initialization request.

// 用于消除spinner初始化时默认的选择事件
private static boolean spinnerSelectedFirst1=true;
private static boolean spinnerSelectedFirst2=true;
private static boolean spinnerSelectedFirst3=true;
// 用于记录spinner当前的选择
private static String spinnerSelect1;
private static String spinnerSelect2;
private static String spinnerSelect3;

final String[] itmes1 = {
    
    "教学楼", "北1", "北2", "北3", "北4", "北5", "南1", "南2", "南3", "南4", "南5"};
final String[] itmes2 = {
    
    "全部楼层", "1-2层", "3-4层", "4层以上"};
final String[] itmes3 = {
    
    "全部人数", "<30%", "30%-50%", ">50%"};

ArrayAdapter<String> spinnerAdapter1 = new ArrayAdapter<>(this, R.layout.spinner_select, itmes1);
spinnerAdapter1.setDropDownViewResource(R.layout.spinner_drop);
spinnerView1.setAdapter(spinnerAdapter1);

ArrayAdapter<String> spinnerAdapter2 = new ArrayAdapter<>(this, R.layout.spinner_select, itmes2);
spinnerAdapter2.setDropDownViewResource(R.layout.spinner_drop);
spinnerView2.setAdapter(spinnerAdapter2);
        
ArrayAdapter<String> spinnerAdapter3 = new ArrayAdapter<>(this, R.layout.spinner_select, itmes3);
spinnerAdapter3.setDropDownViewResource(R.layout.spinner_drop);
spinnerView3.setAdapter(spinnerAdapter3);

spinnerSelect1 = itmes1[0];
spinnerSelect2 = itmes2[0];
spinnerSelect3 = itmes3[0];
updateRoomData();

// 每个spinner的点击事件
spinnerView1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    
    
	   @Override
	   public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    
    
	       if (spinnerSelectedFirst1) spinnerSelectedFirst1 = false;
	       else {
    
    
	           spinnerSelect1 = itmes1[position];
	           updateRoomData();
	       }
	   }
	   @Override
	   public void onNothingSelected(AdapterView<?> parent) {
    
    }
});
spinnerView2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    
    
	   @Override
	   public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    
    
	       if (spinnerSelectedFirst2) spinnerSelectedFirst2 = false;
	       else {
    
    
	           spinnerSelect2 = itmes2[position];
	           updateRoomData();
	       }
	   }
	   @Override
	   public void onNothingSelected(AdapterView<?> parent) {
    
    }
});
spinnerView3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    
    
	   @Override
	   public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    
    
	       if (spinnerSelectedFirst3) spinnerSelectedFirst3 = false;
	       else {
    
    
	           spinnerSelect3 = itmes3[position];
	           updateRoomData();
	       }
	   }
	   @Override
	   public void onNothingSelected(AdapterView<?> parent) {
    
    }
});

Three, the effect

Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/zzh2910/article/details/88310797