最新laravel5+vue.js實戰演練視訊播放專案移動APP端+桌面端多平臺
阿新 • • 發佈:2018-12-24
每次系統計時器排程時,都會呼叫run(...)方法。在下面的方法中,我們呼叫上面列出的機場資料模型的Simulate(...)方法。以下方法確定系統時間並過濾掉時間值小於當前系統時間的所有航班專案。之後我們建立一個前面討論過的Recycler檢視控制器的新例項,並將新的航班列表作為其建構函式的引數傳遞給它。之後,我們最終呼叫介面卡的notifyDataSetChange(...)和notifyItemRangeChanged(...)方法,使正在更新的資料無效並在Recycler檢視中反映其更改。
新增自定義搜尋檢視功能
正如我們上面已經討論過的,我們的機場應用程式實現了在應用程式主視窗最頂層呈現的搜尋檢視,以通過部分匹配執行航班資料的索引搜尋。此時,我們要做的就是將搜尋功能新增到以下自定義搜尋檢視中。為此,我們onTextChanged在主應用程式的活動類中實現方法,可以使用按鈕監聽器進行搜尋:
public class SearchableWithButtonListener implements View.OnClickListener, TextWatcher { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { // Instantinating the flights fragment object FlightsFragment flightsFragment = (FlightsFragment) m_FragmentMgr.findFragmentById(R.id.airport_fragment_container); RecyclerView flightsRecyclerView = null; ArrayList<AirportDataModel> DataSet, oldDataSet = null; // Determining the currently select tab TabLayout tabLayout = findViewById(R.id.flights_destination_tabs); if (tabLayout.getTabAt(0).isSelected()) { // Instantinating the currently active recycler view's object flightsRecyclerView = flightsFragment.m_ArrivalsFragment.m_ArrivalsRecyclerView; // Retriving a list of arrival flights oldDataSet = flightsFragment.m_ArrivalsFragment.m_ArrivalsDataSet; } else if (tabLayout.getTabAt(1).isSelected()) { <span Segoe UI",Arial,Sans-Serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-shadow: none; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"> // Instantinating the currently active recycler view's object</span> flightsRecyclerView = flightsFragment.m_DeparturesFragment.m_DeparturesRecyclerView; <span Segoe UI",Arial,Sans-Serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-shadow: none; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"> // Retriving a list of departure flights</span> oldDataSet = flightsFragment.m_DeparturesFragment.m_DeparturesDataSet; } // Perform a check if the string is not empty if (!charSequence.toString().isEmpty()) { // If so instantinate the flights indexed search object and invoke doSearch method // to obtain the list flights which data matches by the partial match DataSet = new FlightsIndexedSearch(). doSearch(charSequence.toString(), oldDataSet); if (DataSet.size() == 0) { DataSet = oldDataSet; } } else DataSet = oldDataSet; // Instantinate the new adapter object and pass the new filtered dataset as argument FlightsRecyclerAdapter recyclerAdapter = new FlightsRecyclerAdapter(DataSet, getBaseContext()); <span Segoe UI",Arial,Sans-Serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; line-height: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-shadow: none; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"> // Setting up the new recycler view's adapter</span> flightsRecyclerView.setAdapter(recyclerAdapter); <span Segoe UI",Arial,Sans-Serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-shadow: none; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"> // Reflect changes in recycler view</span> recyclerAdapter.notifyDataSetChanged(); recyclerAdapter.notifyItemRangeChanged(0, DataSet.size()); } @Override public void afterTextChanged(Editable editable) { } @Override public void onClick(View view) { if (!m_DrawerLayout.isDrawerOpen(GravityCompat.START)) m_DrawerLayout.openDrawer(GravityCompat.START); } }
當用戶在搜尋檢視中鍵入文字時,將onTextChanged 呼叫overriden事件處理方法。在此方法中,我們確定當前活動的回收器檢視並執行doSearch方法以通過部分匹配獲得已過濾的航班物品列表。之後,我們即時啟動新介面卡並將獲取的資料集傳遞給以下介面卡。最後,我們在當前活動的回收站檢視中使這些資料無效。下面列出的程式碼片段包含doSearch方法的實現:
package com.epsilon.arthurvratz.airportapp; import java.util.ArrayList; import java.util.regex.Pattern; public class FlightsIndexedSearch { public ArrayList<AirportDataModel> doSearch(String text, ArrayList<AirportDataModel> dataSet) { // Instantinating the empty flights array list object ArrayList<AirportDataModel> targetDataset = new ArrayList<>(); // Performing a linear search to find all flight items which data values // match the specific pattern for (int index = 0; index < dataSet.size(); index++) { AirportDataModel currItem = dataSet.get(index); // Applying search pattern to the flight destination string value boolean dest = Pattern.compile(".*" + text + ".*", Pattern.CASE_INSENSITIVE).matcher(currItem.m_Destination).matches(); // Applying search pattern to the airlines flight code string value boolean flight = Pattern.compile(".*" + text + ".*", Pattern.CASE_INSENSITIVE).matcher(currItem.m_Airlines.m_Flight).matches(); // Applying search pattern to the flight status string value boolean status = Pattern.compile(".*" + text + ".*", Pattern.CASE_INSENSITIVE).matcher(currItem.m_Status).matches(); // If one of these values matches the pattern add the current item to the // target dataset if (dest != false || flight != false || status != false) { targetDataset.add(currItem); } } return targetDataset; } }
新增底部導航欄功能
底部導航欄的功能與為執行航班索引搜尋而提供的功能非常相似。要提供此功能,我們必須在主應用程式的活動類中設定選定偵聽器的導航項:
m_flightsNavigationView.setOnNavigationItemSelectedListener( new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) { FlightsFragment flightsFragment = (FlightsFragment) m_FragmentMgr.findFragmentById(R.id.airport_fragment_container); RecyclerView recyclerView = null; ArrayList<AirportDataModel> dataSet = null; FlightsRecyclerAdapter recyclerAdapter = null; // Determining the currently selected tab TabLayout tabLayout = findViewById(R.id.flights_destination_tabs); if (tabLayout.getTabAt(0).isSelected()) { // Getting the currently active recycler view object recyclerView = flightsFragment.m_ArrivalsFragment.m_ArrivalsRecyclerView; // Getting the dataset of the currently active recycle view (e.g. arrival flights dataset) dataSet = flightsFragment.m_ArrivalsFragment.m_ArrivalsDataSet; } else if (tabLayout.getTabAt(1).isSelected()) { // Getting the currently active recycler view object recyclerView = flightsFragment.m_DeparturesFragment.m_DeparturesRecyclerView; // Getting the dataset of the currently active recycle view (e.g. departure flights dataset dataSet = flightsFragment.m_DeparturesFragment.m_DeparturesDataSet; } // Get current system time value long curr_time = System.currentTimeMillis(); if (menuItem.getItemId() == R.id.flights_prev) { // Instantinating the new recycler adapter object and pass the filtered list of previous flights items // returned by the filterByTime method recyclerAdapter = new FlightsRecyclerAdapter(m_AirportDataModel.filterByTime(dataSet, curr_time - (long)3.6e+6 * 48, curr_time), getBaseContext()); } else if (menuItem.getItemId() == R.id.flights_now) { // Instantinating the new recycler adapter object and pass the filtered list of current flights items // returned by the filterByTime method recyclerAdapter = new FlightsRecyclerAdapter(m_AirportDataModel.filterByTime(dataSet, curr_time - (long)3.6e+6 * 24, curr_time + (long)3.6e+6 * 24), getBaseContext()); else if (menuItem.getItemId() == R.id.flights_next) { // Instantinating the new recycler adapter object and pass the filtered list of next flights items // returned by the filterByTime method recyclerAdapter = new FlightsRecyclerAdapter(m_AirportDataModel.filterByTime(dataSet, curr_time, curr_time + (long)3.6e+6 * 48), getBaseContext()); } // Setting up the new recycler view's adapter recyclerView.setAdapter(recyclerAdapter); // Reflect changes in recycler view recyclerAdapter.notifyDataSetChanged(); recyclerAdapter.notifyItemRangeChanged(0, dataSet.size()); return true; } });