Skip to content
Etienne Lawlor edited this page Feb 1, 2015 · 3 revisions

The example below demonstrates QuickReturn view as a header in a ListView with a snap animation

QuickReturnHeaderListViewFragment.java
public class QuickReturnHeaderListViewFragment extends ListFragment {

    // region Member Variables
    private String[] mValues;
    private QuickReturnAnimationType mQuickReturnAnimationType;

    @InjectView(android.R.id.list)
    ListView mListView;
    @InjectView(R.id.quick_return_tv)
    TextView mQuickReturnTextView;
    // endregion

    // region Constructors
    public static QuickReturnHeaderListViewFragment newInstance(Bundle extras) {
        QuickReturnHeaderListViewFragment fragment = new QuickReturnHeaderListViewFragment();
        fragment.setRetainInstance(true);
        fragment.setArguments(extras);
        return fragment;
    }
    
    public static QuickReturnHeaderListViewFragment newInstance() {
        QuickReturnHeaderListViewFragment fragment = new QuickReturnHeaderListViewFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    public QuickReturnHeaderListViewFragment() {
    }
    // endregion

    // region Lifecycle Methods
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_listview_quick_return_header, container, false);
        ButterKnife.inject(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mValues = getResources().getStringArray(R.array.countries);

        ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(),
                R.layout.list_item, R.id.item_tv, mValues);

        mListView.setAdapter(adapter);

        int headerHeight = getActivity().getResources().getDimensionPixelSize(R.dimen.header_height2);

        QuickReturnListViewOnScrollListener scrollListener = new QuickReturnListViewOnScrollListener.Builder(QuickReturnViewType.HEADER)
                        .header(mQuickReturnTextView)
                        .minHeaderTranslation(-headerHeight)
                        .isSnappable(true)
                        .build();
        mListView.setOnScrollListener(mScrollListener);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    // endregion
}
fragment_listview_quick_return_header.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@android:id/list"/>

    <com.etiennelawlor.quickreturn.views.CustomFontTextView
        android:id="@+id/quick_return_tv"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:background="@color/transparent_black_ninty"
        android:textSize="22sp"
        android:text="@string/header"
        android:textColor="@android:color/white"
        android:gravity="center"
        app:textFont="Roboto_Bold"/>

</RelativeLayout>
Clone this wiki locally