com.axum.darivb.searchview.SlidingTabsBasicFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.axum.darivb.searchview.SlidingTabsBasicFragment.java

Source

/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.axum.darivb.searchview;

import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SearchView;

import com.axum.darivb.listviewAdapters.AllUsersListAdapter;
import com.axum.darivb.muze.R;
import com.parse.ParseUser;

import java.util.ArrayList;
import java.util.List;

public class SlidingTabsBasicFragment extends Fragment {

    private String[] tabs = { "People", "Posts" };
    private SearchView searchView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_sample, container, false);
    }

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

        searchView = (SearchView) view.findViewById(R.id.search_view);
        searchView.setIconified(false);
        ImageView back = (ImageView) view.findViewById(R.id.imageView);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                android.app.FragmentManager fm = getFragmentManager();
                fm.popBackStack();
            }
        });

        ViewPager mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
        mViewPager.setAdapter(new SearchPagerAdapter());

        SlidingTabLayout mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.sliding_tabs);
        mSlidingTabLayout.setViewPager(mViewPager);

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                searchView.setQueryHint("Search " + tabs[position]);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    class SearchPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {

            return tabs.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object o) {
            return o == view;
        }

        @Override
        public CharSequence getPageTitle(int position) {

            searchView.setQueryHint("Search " + tabs[0]);
            return tabs[position];
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            // Inflate a new layout from our resources

            View view = getActivity().getLayoutInflater().inflate(R.layout.listview_layout_with_pb, container,
                    false);
            container.addView(view);

            ListView usersListView = (ListView) view.findViewById(R.id.user_list);

            List<ParseUser> allusers = new ArrayList<>();
            if (tabs[position].equals("People")) {
                final AllUsersListAdapter adapter = new AllUsersListAdapter(getActivity(), allusers);
                usersListView.setAdapter(adapter);

                searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {

                    @Override
                    public void onFocusChange(View v, boolean hasFocus) {
                        // TODO Auto-generated method stub

                    }
                });

                //*** setOnQueryTextListener ***
                searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

                    @Override
                    public boolean onQueryTextSubmit(String query) {
                        // TODO Auto-generated method stub

                        return false;
                    }

                    @Override
                    public boolean onQueryTextChange(String newText) {
                        // TODO Auto-generated method stub
                        adapter.getFilter().filter(newText);
                        return true;
                    }
                });
            }

            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}