2.2.0開發總結
阿新 • • 發佈:2018-12-19
2.2.0開發總結
1. 遇到的問題
1. 翻頁
顯示屏等待呼叫列表開發需求:
- 每頁顯示6行
- 共展示2頁
- 每頁顯示4s後自動顯示下一頁 最後一頁顯示完後顯示第一頁
List<TTicket> waitingTicketList = Collections.synchronizedList(new ArrayList<TTicket>());//用於儲存等待的號
private int page = 1;
private class PageThread extends Thread {
private volatile boolean running = true;
@Override
public void run() {
while (running) {
try {
showWaitingList();
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace ();
break;
}
}
}
}
private void showWaitingList() {
int start = (page - 1) * 6;
int end = waitingTicketList.size() < page * 6 ? waitingTicketList.size() : (page * 6);
if (waitingTicketList.size() > start) {
List<TTicket> temp = new ArrayList<>(waitingTicketList.subList(start, end));
waitingAdapter.setShowList(temp);//把需要顯示的list設定給Adapter
runOnUiThread(new Runnable() {
@Override
public void run() {
waitingAdapter.notifyDataSetChanged();//讓Adapter更新資料
}
});
page++;
} else {
page = 1;
}
}
public class Adapter extends RecyclerView.Adapter<ItemViewHolder> {
List<TTicket> ticketList = null;
public void setShowList(List<TTicket> list) {
ticketList = list;
}
}
找bug:
- 當等待叫號的列表只有一個號A,此時顯示屏只有顯示A號顯示是正確的,當某臺叫號機把A 號叫走,此時顯示屏顯示不變
- 最後一屏的顯示兩倍時長
修改後:
private void showWaitingList() {
int start = (page - 1) * 6;
int end = waitingTicketList.size() < page * 6 ? waitingTicketList.size() : (page * 6);
if (waitingTicketList.size() > start || (waitingTicketList.size() == start && page == 1)) {
page++;
} else {
page = 1;
start = (page - 1) * 6;
end = waitingTicketList.size() < page * 6 ? waitingTicketList.size() : (page * 6);
page++;
}
List<TTicket> temp = new ArrayList<>(waitingTicketList.subList(start, end));
waitingAdapter.setShowList(temp);
runOnUiThread(new Runnable() {
@Override
public void run() {
waitingAdapter.notifyDataSetChanged();
}
});
}
如何減少這樣的問題:臨界值的處理流程在草稿紙上模擬一下
2.Dialog大小
public class BaseDialog {
protected Activity activity;
private boolean isShowing = false;
private ViewGroup parentView;
protected View dialogView;
public BaseDialog(Activity context) {
this.activity = context;
}
public BaseDialog(Activity context, int resLayoutId) {
this.activity = context;
dialogView = LayoutInflater.from(activity).inflate(resLayoutId, null);
}
public void setContentView(int resLayoutId) {
dialogView = LayoutInflater.from(activity).inflate(resLayoutId, null);
}
public void setContentView(View view) {
dialogView = view;
}
public void show() {
if (dialogView != null && !isShowing) {
parentView=(activity.getWindow().getDecorView().findViewById(android.R.id.content));
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
parentView.addView(dialogView, lp);
dialogView.setClickable(true);
isShowing = true;
}
}
public View findViewById(int id) {
if (dialogView != null) {
return dialogView.findViewById(id);
}
return null;
}
public void dismiss() {
if (isShowing && parentView != null) {
parentView.removeView(dialogView);
isShowing = false;
}
}
public boolean isShowing() {
return isShowing;
}
}
以後要注意的地方
- 修復bug時要全方位考慮否則可能引入更多的bug(語音的初始化)
- 當優化時,如果發現程式碼因為修改而變得混亂或者邏輯不清晰應該及時重新設計程式碼(簽署機)
- 在測試時註釋掉正在使用的程式碼 當測試結束後應該記住把註釋的程式碼解開
- 需求理解透後再用手寫程式碼
- Code Review
附錄
- 不一樣的翻頁
1、
private final int PAGE_SIZE = 8; //單頁顯示留觀時間數量
private final int PER_PAGE_SHOW_TIME = 5 * 1000; //翻頁間隔時間,5秒
class PageThread extends Thread {
private int currentPage = 1;
private volatile boolean running = true;
public PageThread(int currentPage) {
this.currentPage = currentPage;
}
@Override
public void run() {
while (running) {
try {
Thread.sleep(PER_PAGE_SHOW_TIME);
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
if (running) {
runOnUiThread(new Runnable() {
@Override
public void run() {
nextPage();
}
});
}
}
}
private void nextPage() {
if (allObservingTicketList.size() > PAGE_SIZE) {
int totalPage = allObservingTicketList.size() / PAGE_SIZE;
int left = allObservingTicketList.size() % PAGE_SIZE;
if (left > 0) {
totalPage++;
}
if (currentPage < totalPage) {
currentPage++;
int startIndex = (currentPage - 1) * PAGE_SIZE;
adapter.setStartIndex(startIndex);
} else {
currentPage = 1;
dapter.setStartIndex(0);
}
} else {
currentPage = 1;
adapter.setStartIndex(0);
}
adapter.notifyDataSetChanged();
}
public class Adapter extends RecyclerView.Adapter<ItemViewHolder> {
private int startIndex = 0;
public void setStartIndex(int startIndex) {
this.startIndex = startIndex;
}
@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
position = startIndex + position;
}
@Override
public int getItemCount() {
return PAGE_SIZE;
}
}
2、
public class RecyclerViewScrollRunnable implements Runnable {
private static final int SCROLL_DELAY_TIME = 8 * 1000;
private RecyclerView recyclerView;
private LinearLayoutManager layoutManager;
private Handler handler = new Handler(Looper.getMainLooper());
private int delayTime = SCROLL_DELAY_TIME;
private boolean isRunning;
public RecyclerViewScrollRunnable(RecyclerView recyclerView, LinearLayoutManager layoutManager) {
this.recyclerView = recyclerView;
this.layoutManager = layoutManager;
}
public void start() {
if (isRunning) {
return;
}
isRunning = true;
handler.removeCallbacks(this);
handler.postDelayed(this, delayTime);
}
public void stop() {
isRunning = false;
handler.removeCallbacks(this);
}
@Override
public void run() {
if (recyclerView == null || layoutManager == null || recyclerView.getAdapter() == null) {
isRunning = false;
return;
}
RecyclerView.Adapter adapter = recyclerView.getAdapter();
if (adapter instanceof Scrollable) {
Scrollable scrollable = (Scrollable) adapter;
int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
int countVisibleItem = lastVisibleItemPosition - firstVisibleItemPosition + 1;//每頁展示的數目
int realCount = scrollable.getRealCount();
if (realCount > countVisibleItem ) {
int smoothPosition;
if (lastVisibleItemPosition >= realCount - 1 && firstVisibleItemPosition >= 0) {
smoothPosition = 0;
} else {
smoothPosition = lastVisibleItemPosition;
}
smoothMoveToPosition(smoothPosition);
}
handler.postDelayed(this, delayTime);
} else {
stop();
}
}
private void smoothMoveToPosition(int n) {
layoutManager.scrollToPositionWithOffset(n, 0);
}
}
public interface Scrollable {
int getRealCount();
}
public class Adapter extends RecyclerView.Adapter<ItemViewHolder> implements Scrollable {
@Override
public int getRealCount() {
return ticketList.size();
}
}