在RecyclerView中冻结布局

时间:2017-06-12 02:09:53

标签: android android-recyclerview

在Android应用中,我使用RecyclerView来填充我的数据。我想冻结第一行并滚动其他行。我首先使用滑块视图。

以下是代码:

滑块视图,我需要在屏幕上将其冻结,

layout_image_slider.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <com.daimajia.slider.library.SliderLayout
        android:id="@+id/slider"
        android:layout_width="match_parent"
        android:layout_height="@dimen/slider_height"
        custom:indicator_visibility="invisible" />

    <com.daimajia.slider.library.Indicators.PagerIndicator
        android:id="@+id/custom_indicator"
        style="@style/AppTheme.AndroidImageSliderCornerOvalBlue"
        android:layout_gravity="bottom|center_horizontal" />

</FrameLayout>

DealAdapter.java

 @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case SLIDER:
                return new ViewHolderSlider(LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_image_slider, null));
            case DEAL:
                return new ViewHolderDeal(LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_deal_item, null));
            default:
                return null;
        }
    }

查看绑定,AllDealsFragment.java

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View mView = inflater.inflate(R.layout.fragment_all_deals, container, false);

    mProgressBar = ProgressBar.getInstance(mView.findViewById(R.id.progress_container));
    mREM = new ResponseErrorMessageHandler(mContext, this);
    mREM.cancelable(false);
    mREM.showCancelButton(true);

    getBanners();

    RecyclerView mRecyclerView = (RecyclerView) mView.findViewById(R.id.rv_all_deals);
    mRecyclerView.setHasFixedSize(true);

    StaggeredGridLayoutManager mGridLayoutManager =
            new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
    mRecyclerView.setLayoutManager(mGridLayoutManager);

    mDealAdapter = new DealAdapter(mContext, mBanners, mDeals, this);

    //Add your adapter to the sectionAdapter
    SimpleSectionedRecyclerViewAdapter.Section[] dummy =
            new SimpleSectionedRecyclerViewAdapter.Section[mSections.size()];

    mSectionedAdapter = new SimpleSectionedRecyclerViewAdapter(
            mContext, R.layout.layout_deal_header_item, R.id.tvTitle,mDealAdapter);
    mSectionedAdapter.setSections(mSections.toArray(dummy));

    //Apply this adapter to the RecyclerView
    mRecyclerView.setAdapter(mSectionedAdapter);

fragment_all_deals.xml

 <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv_all_deals"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </RelativeLayout>

更新::

    class ViewHolderSlider extends RecyclerView.ViewHolder {

            public SliderLayout mSlider;

            public ViewHolderSlider(View itemView) {
                super(itemView);
                mSlider = (SliderLayout) itemView.findViewById(R.id.slider);
                mSlider.setPresetTransformer(SliderLayout.Transformer.ZoomOutSlide);
                mSlider.setPresetIndicator(SliderLayout.PresetIndicators.Center_Top);
                mSlider.setCustomAnimation(new DescriptionAnimation());
                mSlider.setCustomIndicator((PagerIndicator) itemView.findViewById(R.id.custom_indicator));
                mSlider.setDuration(5000);
            }
        }


logs ::
06-12 15:23:49.990 1377-1377/com.syncbridge.colombodeals E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.syncbridge.colombodeals, PID: 1377
                                                                           java.lang.NullPointerException: Attempt to invoke virtual method 'void com.daimajia.slider.library.SliderLayout.setPresetTransformer(com.daimajia.slider.library.SliderLayout$Transformer)' on a null object reference
                                                                               at com.syncbridge.colombodeals.adapters.DealAdapter$ViewHolderSlider.<init>(DealAdapter.java:186)
                                                                               at com.syncbridge.colombodeals.adapters.DealAdapter.onCreateViewHolder(DealAdapter.java:58)
                                                                               at com.syncbridge.colombodeals.adapters.SimpleSectionedRecyclerViewAdapter.onCreateViewHolder(SimpleSectionedRecyclerViewAdapter.java:69)
                                                                               at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6290)
                                                                               at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5478)
                                                                               at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5363)
                                                                               at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5359)
                                                                               at android.support.v7.widget.LayoutState.next(LayoutState.java:100)
                                                                               at android.support.v7.widget.StaggeredGridLayoutManager.fill(StaggeredGridLayoutManager.java:1574)
                                                                               at android.support.v7.widget.StaggeredGridLayoutManager.onLayoutChildren(StaggeredGridLayoutManager.java:684)
                                                                               at android.sup

1 个答案:

答案 0 :(得分:1)

将您的两个文件更改为:

将Slider视图作为fragment_all_deals.xml的一部分包含在内。将它放在Recyclerview上面:

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <com.daimajia.slider.library.SliderLayout
            android:id="@+id/slider"
            android:layout_width="match_parent"
            android:layout_height="@dimen/slider_height"
            custom:indicator_visibility="invisible" />

        <android.support.v7.widget.RecyclerView

                android:id="@+id/rv_all_deals"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                 android:layout_below="@id/slider" />

        </RelativeLayout>

layout_image_slider.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">


    <com.daimajia.slider.library.Indicators.PagerIndicator
        android:id="@+id/custom_indicator"
        style="@style/AppTheme.AndroidImageSliderCornerOvalBlue"
        android:layout_gravity="bottom|center_horizontal" />

</FrameLayout>