Skip to content

Unfoldable details usage

Alex Vasilkov edited this page Feb 24, 2017 · 5 revisions
  1. Create layout for your activity as following:

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ListView
            android:id="@+id/list_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
        <View
            android:id="@+id/touch_interceptor_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
        <FrameLayout
            android:id="@+id/details_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            ...
    
        </FrameLayout>
    
        <com.alexvasilkov.foldablelayout.UnfoldableView
            android:id="@+id/unfoldable_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </FrameLayout>
    
  2. Initialize layout:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(...);
    
        mListView = (ListView) findViewById(R.id.list_view);
        mListView.setAdapter(...);
    
        mListTouchInterceptor = findViewById(R.id.touch_interceptor_view);
        mListTouchInterceptor.setClickable(false);
    
        mDetailsLayout = findViewById(R.id.details_layout);
        mDetailsLayout.setVisibility(View.INVISIBLE);
    
        mUnfoldableView = (UnfoldableView) findViewById(R.id.unfoldable_view);
    
        mUnfoldableView.setOnFoldingListener(new UnfoldableView.SimpleFoldingListener() {
            @Override
            public void onUnfolding(UnfoldableView unfoldableView) {
                mListTouchInterceptor.setClickable(true);
                mDetailsLayout.setVisibility(View.VISIBLE);
            }
    
            @Override
            public void onUnfolded(UnfoldableView unfoldableView) {
                mListTouchInterceptor.setClickable(false);
            }
    
            @Override
            public void onFoldingBack(UnfoldableView unfoldableView) {
                mListTouchInterceptor.setClickable(true);
            }
    
            @Override
            public void onFoldedBack(UnfoldableView unfoldableView) {
                mListTouchInterceptor.setClickable(false);
                mDetailsLayout.setVisibility(View.INVISIBLE);
            }
        });
    }
    

    (Touch interceptor view is used to temporary disable list view scrolling)

  3. Add back button handler:

    @Override
    public void onBackPressed() {
        if (mUnfoldableView != null && (mUnfoldableView.isUnfolded() || mUnfoldableView.isUnfolding())) {
            mUnfoldableView.foldBack();
        } else {
            super.onBackPressed();
        }
    }
    
  4. When user selects item in the list you should initialize details view with corresponding data. After that you can start unfolding details with:

    mUnfoldableView.unfold(coverView, mDetailsLayout);
    

    Where coverView is a view from the list that will be animated.
    Note, that it must not be a list item's root layout, since UnfoldableView will try to remove it from it's parent, and ListView and RecyclerView will not behave correctly in this case.

  5. If your list adapter may be invalidated during details unfolding, you should ensure UnfoldableView is still using correct coverView. That mean you should find correct coverView (scrolling to it if necessary) and update UnfoldableView using:

    mUnfoldableView.changeCoverView(coverView);
    
Clone this wiki locally