Example usage for android.widget ListView addHeaderView

List of usage examples for android.widget ListView addHeaderView

Introduction

In this page you can find the example usage for android.widget ListView addHeaderView.

Prototype

public void addHeaderView(View v) 

Source Link

Document

Add a fixed view to appear at the top of the list.

Usage

From source file:org.eyeseetea.malariacare.fragments.DashboardUnsentFragment.java

/**
 * Initializes the listview component, adding a listener for swiping right
 *//*from  w w w. j a v  a  2  s.  c o  m*/
private void initListView() {
    LayoutInflater inflater = LayoutInflater.from(getActivity());
    View header = DashboardHeaderStrategy.getInstance().loadHeader(this.adapter.getHeaderLayout(), inflater);
    final View footer = inflater.inflate(this.adapter.getFooterLayout(), null, false);

    ListView listView = getListView();
    if (header != null) {
        listView.addHeaderView(header);
    }
    listView.addFooterView(footer);
    LayoutUtils.setRowDivider(listView);
    setListAdapter((BaseAdapter) adapter);

    // Create a ListView-specific touch listener. ListViews are given special treatment because
    // by default they handle touches for their list items... i.e. they're in charge of drawing
    // the pressed state (the list selector), handling list item clicks, etc.
    SwipeDismissListViewTouchListener touchListener = new SwipeDismissListViewTouchListener(listView,
            new SwipeDismissListViewTouchListener.DismissCallbacks() {
                @Override
                public boolean canDismiss(int position) {
                    return position > 0 && position <= mSurveyDBs.size();
                }

                @Override
                public void onDismiss(ListView listView, int[] reverseSortedPositions) {
                    for (final int position : reverseSortedPositions) {
                        new AlertDialog.Builder(getActivity())
                                .setTitle(getActivity().getString(R.string.dialog_title_delete_survey))
                                .setMessage(getActivity().getString(R.string.dialog_info_delete_survey))
                                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface arg0, int arg1) {
                                        ((SurveyDB) adapter.getItem(position - 1)).delete();
                                        //Reload data using service
                                        Intent surveysIntent = new Intent(getActivity(), SurveyService.class);
                                        surveysIntent.putExtra(SurveyService.SERVICE_METHOD,
                                                SurveyService.RELOAD_DASHBOARD_ACTION);
                                        getActivity().startService(surveysIntent);
                                    }
                                }).setNegativeButton(android.R.string.no, null).create().show();
                    }

                }
            });
    listView.setOnTouchListener(touchListener);
    // Setting this scroll listener is required to ensure that during ListView scrolling,
    // we don't look for swipes.
    listView.setOnScrollListener(touchListener.makeScrollListener());
}

From source file:com.example.tony.popularmovie.DetailActivityFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    mTrailerAdapter = new TrailerAdapter(getActivity(), null, 0);

    View rootView = inflater.inflate(R.layout.fragment_detail, container, false);
    View headerView = LayoutInflater.from(getActivity()).inflate(R.layout.list_view_header, null);
    View footerView = LayoutInflater.from(getActivity()).inflate(R.layout.list_view_footer, null);

    mTitle = (TextView) headerView.findViewById(R.id.movie_title);
    mReleaseDate = (TextView) headerView.findViewById(R.id.release_date);
    mRatings = (TextView) headerView.findViewById(R.id.movie_ratings);
    mOverview = (TextView) headerView.findViewById(R.id.movie_overview);
    mPoster = (ImageView) headerView.findViewById(R.id.movie_poster);
    mCheckBox = (CheckBox) headerView.findViewById(R.id.checkBox);

    ListView trailerListView = (ListView) rootView.findViewById(R.id.trailer_list);
    trailerListView.addHeaderView(headerView);
    trailerListView.addFooterView(footerView);
    trailerListView.setAdapter(mTrailerAdapter);

    trailerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override/* w  w w.  j a va  2  s. c o  m*/
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (position == parent.getCount() - 1) {
                Intent intent = new Intent(getActivity(), ReviewActivity.class);
                intent.putExtra("movieID", mMovieId);
                getActivity().startActivity(intent);
            } else {
                Uri uri = Uri.parse(BASE_YOUTUBE_URL + sKey);
                Intent intent = new Intent(android.content.Intent.ACTION_VIEW, uri);
                getActivity().startActivity(intent);
            }

        }
    });

    Bundle arguments = getArguments();
    if (arguments != null) {
        trailerListView.setVisibility(View.VISIBLE);
        mMovieId = arguments.getString("movieID");
    } else {
        trailerListView.setVisibility(View.GONE);
    }

    return rootView;
}

From source file:com.airad.zhonghan.PullToRefreshListFragmentActivity.java

/** Called when the activity is first created. */
@Override// ww w.j  ava 2s  .  c o  m
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ptr_list_fragment);

    mPullRefreshListFragment = (PullToRefreshListFragment) getSupportFragmentManager()
            .findFragmentById(R.id.frag_ptr_list);

    // Get PullToRefreshListView from Fragment
    mPullRefreshListView = mPullRefreshListFragment.getPullToRefreshListView();

    // Set a listener to be invoked when the list should be refreshed.
    mPullRefreshListView.setOnRefreshListener(this);

    // You can also just use mPullRefreshListFragment.getListView()
    ListView actualListView = mPullRefreshListView.getRefreshableView();

    mListItems = new LinkedList<String>();
    mListItems.addAll(Arrays.asList(mStrings));
    mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListItems);
    ImageView img = new ImageView(this);
    img.setImageResource(R.drawable.ic_action_search);
    actualListView.addHeaderView(img);
    // You can also just use setListAdapter(mAdapter) or
    // mPullRefreshListView.setAdapter(mAdapter)
    actualListView.setAdapter(mAdapter);

    mPullRefreshListFragment.setListShown(true);
}

From source file:nz.ac.otago.psyanlab.common.designer.channel.ChannelDetailFragment.java

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

    ListView list = (ListView) view.findViewById(R.id.fields);
    list.addHeaderView(inflater.inflate(R.layout.header_data_channel_detail, list, false));

    return view;/*from  w  ww  . j ava2 s .c o  m*/
}

From source file:com.achep.header2actionbar.HeaderFragment.java

public void setListViewAdapter(ListView listView, ListAdapter adapter) {
    isListViewEmpty = adapter == null;//from  ww  w. j  a  v a 2 s . c  om
    listView.setAdapter(null);
    listView.removeHeaderView(mFakeHeader);
    listView.addHeaderView(mFakeHeader);
    listView.setAdapter(adapter);
}

From source file:org.mercycorps.translationcards.activity.TranslationsActivity.java

private void initList() {
    ListView list = (ListView) findViewById(R.id.translations_list);
    LayoutInflater layoutInflater = getLayoutInflater();
    list.addHeaderView(layoutInflater.inflate(R.layout.translation_list_header, list, false));
    findViewById(R.id.translation_list_header).setOnClickListener(null);
    setSwitchClickListener();//  w  w w  .  java2 s .  c o m
    inflateListFooter();

    listAdapter = new CardListAdapter(this, R.layout.translation_item, R.id.origin_translation_text,
            new ArrayList<Translation>());
    list.setAdapter(listAdapter);
}

From source file:com.achep.header2actionbar.HeaderFragmentSupportV4.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final Activity activity = getActivity();
    assert activity != null;
    mFrameLayout = new FrameLayout(activity);

    mHeader = onCreateHeaderView(inflater, mFrameLayout);
    mHeaderHeader = mHeader.findViewById(android.R.id.title);
    mHeaderBackground = mHeader.findViewById(android.R.id.background);
    assert mHeader.getLayoutParams() != null;
    mHeader.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    mHeaderHeight = mHeader.getMeasuredHeight();

    mFakeHeader = new Space(activity);

    View content = onCreateContentView(inflater, mFrameLayout);
    mContentView = content;//  ww  w. j av  a 2  s. c om
    Log.i(TAG, "container:" + container.getMeasuredHeight() + ",mHeaderHeight=" + mHeaderHeight);

    final View topContentView = container;

    if (content instanceof ListView) {
        isListViewEmpty = true;

        final ListView listView = (ListView) content;
        mFakeHeader.setLayoutParams(new ListView.LayoutParams(0, mHeaderHeight));
        listView.addHeaderView(mFakeHeader);
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView absListView, int scrollState) {
                if (mOnScrollListener != null) {
                    mOnScrollListener.onScrollStateChanged(absListView, scrollState);
                }
            }

            @Override
            public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount,
                    int totalItemCount) {
                if (isListViewEmpty) {
                    scrollHeaderTo(0);
                } else {
                    final View child = absListView.getChildAt(0);
                    assert child != null;
                    scrollHeaderTo(child == mFakeHeader ? child.getTop() : -mHeaderHeight);
                }
            }
        });
    } else {
        topContentView.getViewTreeObserver()
                .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @SuppressWarnings("deprecation")
                    @Override
                    public void onGlobalLayout() {
                        //Remove it here unless you want to get this callback for EVERY
                        //layout pass, which can get you into infinite loops if you ever
                        //modify the layout from within this method.
                        topContentView.getViewTreeObserver().removeGlobalOnLayoutListener(this);

                        //Now you can get the width and height from content
                        int actionBarHeight = 0;
                        TypedValue tv = new TypedValue();
                        if (getActivity().getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
                            actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,
                                    getResources().getDisplayMetrics());
                        }
                        Log.i(TAG, "topContentView:" + topContentView.getHeight() + ", actionBarHeight:"
                                + actionBarHeight + ", getStatusBarHeight:" + getStatusBarHeight());
                        ViewGroup.LayoutParams lp = mContentView.getLayoutParams();
                        lp.height = topContentView.getHeight() - actionBarHeight - getStatusBarHeight();
                        mContentView.setLayoutParams(lp);
                    }
                });

        // Merge fake header view and content view.
        final LinearLayout view = new LinearLayout(activity);
        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        view.setOrientation(LinearLayout.VERTICAL);
        mFakeHeader.setLayoutParams(new LinearLayout.LayoutParams(0, mHeaderHeight));
        view.addView(mFakeHeader);
        view.addView(content);

        // Put merged content to ScrollView
        final NotifyingScrollView scrollView = new NotifyingScrollView(activity);
        scrollView.addView(view);
        scrollView.setVerticalScrollBarEnabled(false);
        scrollView.setOverScrollMode(ScrollView.OVER_SCROLL_NEVER);
        scrollView.setOnScrollChangedListener(new NotifyingScrollView.OnScrollChangedListener() {
            @Override
            public void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt) {
                Log.i(TAG, "onScrollChanged scroll ot -t :" + (-t));
                scrollHeaderTo(-t);
            }
        });
        mContentWrapper = scrollView;
        content = scrollView;
    }

    mFrameLayout.addView(content);
    mFrameLayout.addView(mHeader);

    // Content overlay view always shows at the top of content.
    if ((mContentOverlay = onCreateContentOverlayView(inflater, mFrameLayout)) != null) {
        mFrameLayout.addView(mContentOverlay, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
    }

    // Post initial scroll
    mFrameLayout.post(new Runnable() {
        @Override
        public void run() {
            Log.i(TAG, "post initial scroll to 0");
            // SEAN: walk around for scroll position bug
            if (mContentWrapper != null) {
                mContentWrapper.scrollTo(0, 0);
            }
            scrollHeaderTo(0, true);
        }
    });

    return mFrameLayout;
}

From source file:com.achep.header2actionbar.HeaderFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final Activity activity = getActivity();
    assert activity != null;
    mFrameLayout = new FrameLayout(activity);

    mHeader = onCreateHeaderView(inflater, mFrameLayout);
    mHeaderHeader = mHeader.findViewById(android.R.id.title);
    mHeaderBackground = mHeader.findViewById(android.R.id.background);
    assert mHeader.getLayoutParams() != null;
    mHeaderHeight = mHeader.getLayoutParams().height;

    mFakeHeader = new Space(activity);
    mFakeHeader.setLayoutParams(new ListView.LayoutParams(0, mHeaderHeight));

    View content = onCreateContentView(inflater, mFrameLayout);
    if (content instanceof ListView) {
        isListViewEmpty = true;/*from  w w  w .  ja va 2 s.c o  m*/

        final ListView listView = (ListView) content;
        listView.addHeaderView(mFakeHeader);
        onSetAdapter();
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView absListView, int scrollState) {
                if (mOnScrollListener != null) {
                    mOnScrollListener.onScrollStateChanged(absListView, scrollState);
                }
            }

            @Override
            public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount,
                    int totalItemCount) {
                if (mOnScrollListener != null) {
                    mOnScrollListener.onScroll(absListView, firstVisibleItem, visibleItemCount, totalItemCount);
                }

                isListViewEmpty = listView.getAdapter() == null;
                if (isListViewEmpty) {
                    scrollHeaderTo(0);
                } else {
                    final View child = absListView.getChildAt(0);
                    assert child != null;
                    scrollHeaderTo(child == mFakeHeader ? child.getTop() : -mHeaderHeight);
                    //                        Log.v("header height",String.format("mHeaderHeight:%s",mHeaderHeight));
                }
            }
        });
    } else if (content instanceof GridView) {
        isListViewEmpty = true;

        final GridView grid = (GridView) content;
        //isListViewEmpty = grid.getAdapter() == null;
        //            grid.addHeaderView(mFakeHeader);

        // Merge fake header view and content view.
        final LinearLayout view = new LinearLayout(activity);
        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        view.setOrientation(LinearLayout.VERTICAL);
        view.addView(mFakeHeader);
        view.addView(grid);

        grid.setOnScrollListener(new AbsListView.OnScrollListener() {

            private ListViewScrollTracker mScrollTracker;;

            @Override
            public void onScrollStateChanged(AbsListView absListView, int scrollState) {
                if (mOnScrollListener != null) {
                    mOnScrollListener.onScrollStateChanged(absListView, scrollState);
                }
            }

            public int getGridScrollY(GridView grid) {

                Method privateStringMethod = null;
                Integer scrollOffset = null;
                Integer scrollExtent = null;
                Integer scrollRange = null;
                try {
                    privateStringMethod = GridView.class.getDeclaredMethod("computeVerticalScrollOffset", null);
                    privateStringMethod.setAccessible(true);
                    scrollOffset = (Integer) privateStringMethod.invoke(grid, null);

                    privateStringMethod = GridView.class.getDeclaredMethod("computeVerticalScrollExtent", null);
                    privateStringMethod.setAccessible(true);
                    scrollExtent = (Integer) privateStringMethod.invoke(grid, null);

                    privateStringMethod = GridView.class.getDeclaredMethod("computeVerticalScrollRange", null);
                    privateStringMethod.setAccessible(true);
                    scrollRange = (Integer) privateStringMethod.invoke(grid, null);
                } catch (NoSuchMethodException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return scrollRange;//scrollExtent*scrollOffset;  

            }

            @Override
            public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount,
                    int totalItemCount) {
                if (mScrollTracker == null)
                    mScrollTracker = new ListViewScrollTracker(absListView);
                mScrollPosition = mScrollTracker.calculateIncrementalOffset(firstVisibleItem, visibleItemCount);
                Log.v("header", "scrollPosition:" + mScrollPosition);
                if (mOnScrollListener != null) {
                    mOnScrollListener.onScroll(absListView, firstVisibleItem, visibleItemCount, totalItemCount);
                }

                final View child = absListView.getChildAt(0);
                if (child != null) {
                    int gridScrollY = mScrollPosition;//getGridScrollY((GridView) absListView);
                    scrollHeaderTo(-gridScrollY);

                    //change the size of the fake header on scrolling                        
                    int currentHeight = mFakeHeader.getLayoutParams().height;
                    Integer fakeHeaderHeight = (int) (mHeaderHeight - (gridScrollY * 2));
                    mFakeHeader.setLayoutParams(new android.widget.LinearLayout.LayoutParams(
                            mFakeHeader.getWidth(), fakeHeaderHeight));
                    //                  setViewTranslationY(mFrameLayout, -1*(gridScrollY));
                } else
                    scrollHeaderTo(0);
            }
        });
        content = view;
    } else {

        // Merge fake header view and content view.
        final LinearLayout view = new LinearLayout(activity);
        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        view.setOrientation(LinearLayout.VERTICAL);
        view.addView(mFakeHeader);
        view.addView(content);

        // Put merged content to ScrollView
        final NotifyingScrollView scrollView = new NotifyingScrollView(activity);
        scrollView.addView(view);
        scrollView.setOnScrollChangedListener(new NotifyingScrollView.OnScrollChangedListener() {
            @Override
            public void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt) {
                scrollHeaderTo(-t);
            }
        });
        content = scrollView;
    }

    mFrameLayout.addView(content);
    mFrameLayout.addView(mHeader);

    // Content overlay view always shows at the top of content.
    if ((mContentOverlay = onCreateContentOverlayView(inflater, mFrameLayout)) != null) {
        mFrameLayout.addView(mContentOverlay, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
    }

    // Post initial scroll
    mFrameLayout.post(new Runnable() {
        @Override
        public void run() {
            scrollHeaderTo(0, true);
        }
    });

    return mFrameLayout;
}

From source file:com.utils.widget.head.HeaderFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final Activity activity = getActivity();
    assert activity != null;
    mFrameLayout = new FrameLayout(activity);

    mHeader = onCreateHeaderView(inflater, mFrameLayout);
    mHeaderHeader = mHeader.findViewById(android.R.id.title);
    mHeaderBackground = mHeader.findViewById(android.R.id.background);
    assert mHeader.getLayoutParams() != null;
    mHeaderHeight = mHeader.getLayoutParams().height;
    mFakeHeader = new Space(activity);
    mFakeHeader.setLayoutParams(new ListView.LayoutParams(0, mHeaderHeight));

    View content = onCreateContentView(inflater, mFrameLayout);
    if (content instanceof ListView) {
        isListViewEmpty = true;/* ww w  . j a va  2s.  c om*/

        final ListView listView = (ListView) content;
        listView.addHeaderView(mFakeHeader);
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView absListView, int scrollState) {
                if (mOnScrollListener != null) {
                    mOnScrollListener.onScrollStateChanged(absListView, scrollState);
                }
            }

            @Override
            public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount,
                    int totalItemCount) {
                if (mOnScrollListener != null) {
                    mOnScrollListener.onScroll(absListView, firstVisibleItem, visibleItemCount, totalItemCount);
                }

                if (isListViewEmpty) {
                    scrollHeaderTo(0);
                } else {
                    final View child = absListView.getChildAt(0);
                    assert child != null;
                    scrollHeaderTo(child == mFakeHeader ? child.getTop() : -mHeaderHeight);
                }
            }
        });
    } else {

        // Merge fake header view and content view.
        final LinearLayout view = new LinearLayout(activity);
        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        view.setOrientation(LinearLayout.VERTICAL);
        view.addView(mFakeHeader);
        view.addView(content);

        // Put merged content to ScrollView
        final NotifyingScrollView scrollView = new NotifyingScrollView(activity);
        scrollView.addView(view);
        scrollView.setOnScrollChangedListener(new NotifyingScrollView.OnScrollChangedListener() {
            @Override
            public void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt) {
                scrollHeaderTo(-t);
            }
        });
        content = scrollView;
    }

    mFrameLayout.addView(content);
    mFrameLayout.addView(mHeader);

    // Content overlay view always shows at the top of content.
    if ((mContentOverlay = onCreateContentOverlayView(inflater, mFrameLayout)) != null) {
        mFrameLayout.addView(mContentOverlay, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
    }

    // Post initial scroll
    mFrameLayout.post(new Runnable() {
        @Override
        public void run() {
            scrollHeaderTo(0, true);
        }
    });

    return mFrameLayout;
}

From source file:com.social.solution.fragment.MyFragment.java

protected void setDummyDataWithHeader(ListView listView, View headerView) {
    listView.addHeaderView(headerView);
    setDummyData(listView); // testing git
}