Code example for ListFragment

Methods: getListView, onResume

0
     */ 
    private int savedTopMostElementPosition;
 
    @Override 
    public void onResume() { 
        super.onResume(); 
        restoreScrollPosition(); 
    } 
 
    @Override 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ScrollView scroller = new ScrollView(getActivity());
        TextView text = new TextView(getActivity());
        int padding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getActivity()
                .getResources().getDisplayMetrics());
        text.setPadding(padding, padding, padding, padding);
        scroller.addView(text);
        /* 
         * Sets the list of MovieInfo beans to a MovieListAdapter, which is then 
         * set as the adapter for this activity. 
         */ 
        if (currentMovieList != null) {
            MovieListAdapter adapter = new MovieListAdapter(getActivity(), currentMovieList);
            setListAdapter(adapter);
            adapter.sortByTitle();
        } 
    } 
 
    @Override 
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setListShown(currentMovieList != null);
    } 
    /** 
     * If an a item is clicked then a the Movie Detail activity is launched, 
     * passing the corresponding MovieInfo bean in the intent extras. 
     */ 
    @Override 
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        saveScrollPosition(l);
        Intent i = new Intent(getActivity(), MovieDetail.class);
        i.putExtra(MovieDetail.MOVIE_INFO_EXTRA, currentMovieList.get(position));
        startActivity(i);
    } 
 
    /** 
     * This method saves the scroll position. TODO: Position is lost if device 
     * orientation is changed. Maybe use 
     * onSaveInstanceState/onRestoreInstanceState methods. 
     *  
     * @param l ListView 
     */ 
    private void saveScrollPosition(ListView l) {
        this.savedTopMostElementIndex = l.getFirstVisiblePosition();
        View topItem = l.getChildAt(0);
        this.savedTopMostElementPosition = topItem.getTop();
    } 
 
    /** 
     * Restore the previous scroll position on the list. 
     */ 
    private void restoreScrollPosition() { 
        if (this.savedTopMostElementIndex > 0) {
            getListView().setSelectionFromTop(this.savedTopMostElementIndex,
                    this.savedTopMostElementPosition);
        } 
    } 
 
    /** 
Experience pair programming with AI  Get Codota for Java