美文网首页
Android TV Leanback分析之创建一个BaseGr

Android TV Leanback分析之创建一个BaseGr

作者: Dongyp | 来源:发表于2020-03-29 17:30 被阅读0次

创建一个简单、简洁的的 VerticalGridView或HorizontalGridView 的BaseGridFragment ,可以当作Header部分或Browser容器使用。

public abstract class BaseGridFragment extends Fragment {
    //ItemBridgeAdapter 和BaseGridView交互数据
    final ItemBridgeAdapter mBridgeAdapter = new ItemBridgeAdapter();
    //需要子类提供一个数据,会设置到ItemBridgeAdapter中
    private ObjectAdapter mAdapter;
    //需要子类提供layout id 
    BaseGridView mGridView;
    //设置一个PresenterSelector,会将此设置到ItemBridgeAdapter中
    private PresenterSelector mPresenterSelector;
    //选中位置
    int mSelectedPosition = -1;
    //视图创建之前准备阶段
    private boolean mPendingTransitionPrepare;
    //监听ChildView选中
    private final OnChildViewHolderSelectedListener mRowSelectedListener =
            new OnChildViewHolderSelectedListener() {
                @Override
                public void onChildViewHolderSelected(RecyclerView parent,
                                                      RecyclerView.ViewHolder view, int position, int subposition) {
                    mSelectedPosition = position;
                    OnChildViewSelected(parent, view, position, subposition);
                }
            };

    //回调:提供给子类,当子视图被选中时,会执行此方法
    void OnChildViewSelected(RecyclerView parent, RecyclerView.ViewHolder view,
                             int position, int subposition) {
    }

    //子类实现GridView的layout id
    abstract int getLayoutResourceId();

    //子类实现BaseGridView的可以是VerticalGridView 或HorizontalGridView,根据layout来决定
    BaseGridView findGridViewFromRoot(View view) {
        return (BaseGridView) view;
    }

    //创建GridView
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(getLayoutResourceId(), container, false);
        view.setFocusable(false);
        view.setSelected(false);
        mGridView = findGridViewFromRoot(view);
        //准备阶段,
        if (mPendingTransitionPrepare) {
            mPendingTransitionPrepare = false;
            onTransitionPrepare();
        }
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        //若setAdapter先执行,或者
        setAdapterAndSelection();
        //注册选中监听
        mGridView.setOnChildViewHolderSelectedListener(mRowSelectedListener);
    }

    //设置数据,必须执行
    public final void setAdapter(ObjectAdapter adapter) {
        if (mAdapter != adapter) {
            mAdapter = adapter;
            updateAdapter();
        }
    }

    void updateAdapter() {
        mBridgeAdapter.setAdapter(mAdapter);
        //BridgeAdapter有两种途径获得resenterSelector
        //第一种是BridgeAdapter自己设置,第二种是ObjectAdapter设置
        mBridgeAdapter.setPresenter(mPresenterSelector);
        if (mGridView != null) {
            setAdapterAndSelection();
        }
    }

    void setAdapterAndSelection() {
        if (mAdapter == null) {
            return;
        }
        if (mGridView.getAdapter() != mBridgeAdapter) {
            mGridView.setAdapter(mBridgeAdapter);
        }
        if (mSelectedPosition >= 0 && mBridgeAdapter.getItemCount() != 0) {
            mGridView.setSelectedPosition(mSelectedPosition);
        }
    }

    //设置选中位置
    public void setSelectedPosition(int position) {
        setSelectedPosition(position, false);
    }

    public void setSelectedPosition(int position, boolean smooth) {
        if (mSelectedPosition == position) {
            return;
        }
        mSelectedPosition = position;
        if (mGridView != null) {
            if (smooth) {
                mGridView.setSelectedPositionSmooth(position);
            } else {
                mGridView.setSelectedPosition(position);
            }
        }
    }

    //准备阶段
    public boolean onTransitionPrepare() {
        if (mGridView != null) {
            mGridView.setAnimateChildLayout(false);
            mGridView.setScrollEnabled(false);
            return true;
        }
        mPendingTransitionPrepare = true;
        return false;
    }

    //执行动画之前
    public void onTransitionStart() {
        if (mGridView != null) {
            mGridView.setPruneChild(false);
            mGridView.suppressLayout(true);
            mGridView.setFocusSearchDisabled(true);
        }
    }

    //动画结局后
    public void onTransitionEnd() {
        if (mGridView != null) {
            mGridView.suppressLayout(false);
            mGridView.setAnimateChildLayout(true);
            mGridView.setPruneChild(true);
            mGridView.setFocusSearchDisabled(false);
            mGridView.setScrollEnabled(true);
        }
    }

    //设置GridView距离顶部的距离
    public void setAlignment(int windowAlignOffsetTop) {
        if (mGridView != null) {
            // align the top edge of item
            mGridView.setItemAlignmentOffset(0);
            mGridView.setItemAlignmentOffsetPercent(
                    BaseGridView.ITEM_ALIGN_OFFSET_PERCENT_DISABLED);

            // align to a fixed position from top
            mGridView.setWindowAlignmentOffset(windowAlignOffsetTop);
            mGridView.setWindowAlignmentOffsetPercent(
                    BaseGridView.WINDOW_ALIGN_OFFSET_PERCENT_DISABLED);
            // window alig high edge
            mGridView.setWindowAlignment(BaseGridView.WINDOW_ALIGN_HIGH_EDGE);
        }
    }

    // 设置Presenter选择器,也可以在Adpter中设置。
    public final void setPresenterSelector(PresenterSelector presenterSelector) {
        if (mPresenterSelector != presenterSelector) {
            mPresenterSelector = presenterSelector;
            updateAdapter();
        }
    }

    public final ObjectAdapter getAdapter() {
        return mAdapter;
    }

    public final ItemBridgeAdapter getBridgeAdapter() {
        return mBridgeAdapter;
    }

    public int getSelectedPosition() {
        return mSelectedPosition;
    }

    public final BaseGridView getGridView() {
        return mGridView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mGridView = null;
    }
}

相关文章

网友评论

      本文标题:Android TV Leanback分析之创建一个BaseGr

      本文链接:https://www.haomeiwen.com/subject/cpduuhtx.html