Android開發過程中的坑及解決方法收錄

  1.某個控件要放在Linearlayout佈局的底部(底部導航條)
  
  <LinearLayout
  
  android:layout_width="match_parent"
  
  android:orientation="vertical"
  
  android:layout_height="match_parent"
  
  ...>
  
  <LinearLayout
  
  android:layout_width="match_parent"
  
  android:orientation="vertical"
  
  android:layout_height="0dp"
  
  android:Layout_weight="2">
  
  ...//嵌套的其他佈局……
  
  </LinearLayout>
  
  ...//嵌套的其他佈局
  
  <LinearLayout
  
  android:layout_width="match_parent"
  
  android:layout_height="wrap_content">
  
  </LinearLayout>
  
  </LinearLayout>
  
  簡單說明一下,上面的代碼中有一個Linearlayout,裏面嵌套了兩個Linearlayout
  
  這裏的關鍵是嵌套裏面的第一個Linearlayout佈局,注意這個佈局裏面的這兩行屬性代碼
  
  `android:layout_height="0dp"`
  
  `android:Layout_weight="2"`
  
  第二個Linearlayout就是可以放在底部的一個Linearlayout(當然你可以寫你自己的佈局)
  
  2.RecyclerView顯示圖片卡頓優化
  
  思路:圖片太多,顯示卡頓的原因主要是因爲在RecyclerView滑動的過程中同時加載網絡的圖片,所以卡頓。
  
  我們實現滑動的時候不加載網絡圖片,當不滑動的時候再加載網絡圖片,這樣流暢度就可以提高許多
  
  在RecyclerView的Adapter(自己寫的)中添加一個判斷RecyclerView是否滑動的boolean變量isScrolling
  
  protected boolean isScrolling = false;
  
  public void setScrolling(boolean scrolling) {
  
  isScrolling = scrolling;
  
  }
  
  之後在Adapter裏面的onBindViewHolder方法控制加載圖片
  
  @Override
  
  public void onBindViewHolder(ViewHolder holder, int position) {
  
  String url = mlist.get(position).getImg().getUrl();
  
  if (!isScrolling){
  
  //我使用的是Ion顯示圖片框架
  
  //如果不在滑動,則加載網絡圖片
  
  Ion.with(holder.imageView.getContext())
  
  .load(url)
  
  .withBitmap()
  
  .placeholder(R.drawable.grey)
  
  .intoImageView(holder.imageView);
  
  }else {
  
  //如果在滑動,就先加載本地的資源圖片
  
  Drawable temp = holder.imageView.getResources().getDrawable(R.drawable.grey, null);
  
  holder.imageView.setImageDrawable(temp);
  
  }
  
  }
  
  在相應的Activity中調用RecyclerView的addOnScrollListener方法,設置一個滑動監聽器
  
  mRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
  
  @Override
  
  public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
  
  if (newState == RecyclerView.SCROLL_STATE_IDLE) { // 滾動靜止時才加載圖片資源,極大提升流暢度
  
  adapter.setScrolling(false);
  
  adapter.notifyDataSetChanged(); // notify調用後onBindViewHolder會響應調用
  
  } else{
  
  adapter.setScrolling(true);
  
  }
  
  super.onScrollStateChanged(recyclerView, newState);
  
  }
  
  });
  
  3.ScrollView與RecyclerView滑動衝突
  
  這裏使用NestedScrollView即可,然後設置RecyclerView的NestedScrollingEnabled屬性爲false
  
  兩種方法設置RecyclerView的NestedScrollingEnabled屬性
  
  - 調用`RecyclerView`的`setNestedScrollingEnabled`方法
  
  - 在xml文件裏面,把`RecyclerView`直接設置爲`flase`
  
  判斷ScrollView是否滑動到底部
  
  給ScrollView添加一個滑動監聽器,然後進行相關處理
  
  mNestedsv.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener(www.michenggw.com) {
  
  @Override
  
  public void onScrollChange(www.mingcheng178.com NestedScrollView v, int scrollX,www.trgj888.com int scrollY, int oldScrollX, int oldScrollY) {
  
  View view = mNestedsv.getChildAt(0);
  
  if (mNestedsv.getHeight(www.yongshiyule178.com )+mNestedsv.getScrollY() ==view.getHeight()){
  
  //相關提示
  
  //相關操作
  
  //下拉刷新,數據更新操作
  
  //...
  
  }
  
  }
  
  });
  
  4.使用okhttp返回數據相同解決方法
  
  看了資料,好像是respone.body().string()只能調用一次,還有okhttp是有緩存的
  
  使用的情景:有一個API接口,每次訪問改接口,都會返回不同的json數據,但是使用okhttp,每次訪問該API返回的數據都是相同
  
  我的解決方法:
  
  給API請求時添加參數,有些API是可以帶參數的,可以修改參數,達到是不同網址的效果
  
  5.RecyclerView數據更新
  
  調用Adapter的notifyDataSetChanged方法即可
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章