亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android RecyclerView item選中放大被遮擋問題詳解

 更新時間:2018年04月23日 09:38:03   作者:lbcab  
這篇文章主要介紹了Android RecyclerView item選中放大被遮擋問題詳解,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

在Android TV上一般選中某個View, 都會有焦點突出放大的效果, 但是當在RecyclerView中(ListView或GridView)實現當item View執(zhí)行放大動畫后會被其他的item View遮擋.

原因是: RecyclerView的機制是越靠后的View z-order越高, 所以bringToFront方法是不管用的.

在實現針對TV端的自定義控件 TvRecyclerView 時遇到此問題, 最后的解決方案是:

自定義RecyclerView, 重寫getChildDrawingOrder方法, 讓選中的item最后繪制, 這樣就不會讓其他view遮擋.

public class ScaleRecyclerView extends RecyclerView {

  private int mSelectedPosition = 0;

  public ScaleRecyclerView(Context context) {
    super(context);
    init();
  }

  public ScaleRecyclerView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    init();
  }

  public ScaleRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
  }

  private void init() {
    //啟用子視圖排序功能
    setChildrenDrawingOrderEnabled(true);
  }

  @Override
  public void onDraw(Canvas c) {
    mSelectedPosition = getChildAdapterPosition(getFocusedChild());
    super.onDraw(c);
  }

  @Override
  protected int getChildDrawingOrder(int childCount, int i) {
    int position = mSelectedPosition;
    if (position < 0) {
      return i;
    } else {
      if (i == childCount - 1) {
        if (position > i) {
          position = i;
        }
        return position;
      }
      if (i == position) {
        return childCount - 1;
      }
    }
    return i;
  }
}

最好還需要設置RecyclerView的父類的屬性: clipChildren = false, clipToPadding = false, 避免邊緣的子view被父類遮擋.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:clipChildren="false"
  android:clipToPadding="false">

  <android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="245dp"
    android:layout_centerInParent="true" />

</RelativeLayout>

 使用介紹:

(1) 自定具有放大縮小的布局:

public class FocusRelativeLayout extends RelativeLayout {

  private Animation scaleSmallAnimation;
  private Animation scaleBigAnimation;

  public FocusRelativeLayout(Context context) {
    super(context);
  }

  public FocusRelativeLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public FocusRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
  }

  @Override
  protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
    super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
    if (gainFocus) {
      getRootView().invalidate();
      zoomOut();
    } else {
      zoomIn();
    }
  }

  private void zoomIn() {
    if (scaleSmallAnimation == null) {
      scaleSmallAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.anim_scale_small);
    }
    startAnimation(scaleSmallAnimation);
  }

  private void zoomOut() {
    if (scaleBigAnimation == null) {
      scaleBigAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.anim_scale_big);
    }
    startAnimation(scaleBigAnimation);
  }
}

(2) 放大動畫xml配置如下:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
  android:fillAfter="true"
  android:shareInterpolator="false">
  <scale
    android:duration="150"
    android:fromXScale="1.0"
    android:fromYScale="1.0"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:pivotX="50.0%"
    android:pivotY="50.0%"
    android:repeatCount="0"
    android:toXScale="1.08"
    android:toYScale="1.08" />
</set>

(3) 主布局xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:clipChildren="false"
  android:clipToPadding="false">

  <com.app.tvviewpager.ScaleRecyclerView
    android:id="@+id/main_recyclerView"
    android:layout_width="match_parent"
    android:layout_height="210dp"
    android:layout_centerInParent="true"/>

</RelativeLayout>

(4) 子視圖的xml:

<FocusRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/rl_main_layout"
  android:layout_width="300dp"
  android:layout_height="210dp"
  android:focusable="true">

  <TextView
    android:layout_width="300dp"
    android:layout_height="30dp"
    android:layout_alignParentBottom="true" />
</FocusRelativeLayout>

(5) adapter配置:

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
  private Context mContext;
  private OnItemStateListener mListener;
  private static int[] mColorIds = {R.color.amber, R.color.brown, R.color.cyan,
      R.color.deepPurple, R.color.green, R.color.lightBlue, R.color.lightGreen,
      R.color.lime, R.color.orange, R.color.pink, R.color.cyan, R.color.deepPurple};
  MyAdapter(Context context) {
    mContext = context;
  }

  public void setOnItemStateListener(OnItemStateListener listener) {
    mListener = listener;
  }

  @Override
  public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    return new RecyclerViewHolder(View.inflate(mContext, R.layout.item_recyclerview, null));
  }

  @Override
  public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
    final RecyclerViewHolder viewHolder = (RecyclerViewHolder) holder;
    viewHolder.mRelativeLayout.setBackgroundColor(ContextCompat.getColor(mContext, mColorIds[position]));
  }

  @Override
  public int getItemCount() {
    return mColorIds.length;
  }

  private class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    FocusRelativeLayout mRelativeLayout;

    RecyclerViewHolder(View itemView) {
      super(itemView);
      mRelativeLayout = (FocusRelativeLayout) itemView.findViewById(R.id.rl_main_layout);
      mRelativeLayout.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
      if (mListener != null) {
        mListener.onItemClick(v, getAdapterPosition());
      }
    }
  }

  public interface OnItemStateListener {
    void onItemClick(View view, int position);
  }
}

(6) Activity中的配置:

public class RecyclerActivity extends AppCompatActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recycler);
    final ScaleRecyclerView recyclerView = (ScaleRecyclerView) findViewById(R.id.main_recyclerView);
    GridLayoutManager manager = new GridLayoutManager(RecyclerActivity.this, 1);
    manager.setOrientation(LinearLayoutManager.HORIZONTAL);
    manager.supportsPredictiveItemAnimations();
    recyclerView.setLayoutManager(manager);
    int itemSpace = getResources().
        getDimensionPixelSize(R.dimen.recyclerView_item_space);
    recyclerView.addItemDecoration(new SpaceItemDecoration(itemSpace));
    final MyAdapter mAdapter = new MyAdapter(RecyclerActivity.this);
    recyclerView.setAdapter(mAdapter);
  }

  private class SpaceItemDecoration extends RecyclerView.ItemDecoration {
    private int space;
    SpaceItemDecoration(int space) {
      this.space = space;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
                  RecyclerView.State state) {
      outRect.left = space;
    }
  }
}

效果圖如下:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Android實現簡易版彈鋼琴效果

    Android實現簡易版彈鋼琴效果

    這篇文章主要為大家詳細介紹了Android實現簡易版彈鋼琴效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • Android PraiseTextView實現朋友圈點贊功能

    Android PraiseTextView實現朋友圈點贊功能

    這篇文章主要為大家詳細介紹了PraiseTextView簡單實現朋友圈點贊功能的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • Android 版本、權限適配相關總結

    Android 版本、權限適配相關總結

    針對 Android 6.0 (API 23)已以上版本,Google 增強全新的權限,應用程序在使用敏感權限(如拍照、查閱聯系人或存儲)時需要先征求用戶必須贏得用戶同意。
    2021-05-05
  • Android中實現EditText圓角的方法

    Android中實現EditText圓角的方法

    Android中實現EditText圓角的方法,需要的朋友可以參考一下
    2013-03-03
  • android okhttp的基礎使用【入門推薦】

    android okhttp的基礎使用【入門推薦】

    本文主要總結了Android著名網絡框架-okhttp的基礎使用。具有一定的參考價值,下面跟著小編一起來看下吧
    2017-01-01
  • Android開發(fā)graphics?bufferqueue整體流程

    Android開發(fā)graphics?bufferqueue整體流程

    這篇文章主要為大家介紹了Android開發(fā)graphics?bufferqueue整體流程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-07-07
  • Android利用ViewPager實現用戶引導界面效果的方法

    Android利用ViewPager實現用戶引導界面效果的方法

    這篇文章主要介紹了Android利用ViewPager實現用戶引導界面效果的方法,結合實例形式詳細分析了Android軟件功能界面的初始化、view實例化、動畫功能實現與布局相關技巧,需要的朋友可以參考下
    2016-07-07
  • Android view滑動懸浮固定效果實現代碼示例

    Android view滑動懸浮固定效果實現代碼示例

    本篇文章主要介紹了Android view滑動懸浮固定效果實現代碼示例,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • Android實現史上最簡單自定義開關按鈕的方法

    Android實現史上最簡單自定義開關按鈕的方法

    在平常的開發(fā)中按鈕是經常使用到的控件之一,下面這篇文章主要給大家介紹了關于Android實現史上最簡單自定義開關按鈕的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-04-04
  • Android自定義邊緣凹凸的卡劵效果

    Android自定義邊緣凹凸的卡劵效果

    這篇文章主要介紹了Android自定義邊緣凹凸的卡劵效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05

最新評論