Java Code Examples for android.support.v7.widget.RecyclerView#setItemViewCacheSize()

The following examples show how to use android.support.v7.widget.RecyclerView#setItemViewCacheSize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RecyclerActivity.java    From Colorful with MIT License 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);

	setTheme(R.style.NightTheme);

	setContentView(R.layout.activity_recycler);

	findViewById(R.id.change_btn).setOnClickListener(new OnClickListener() {

		@Override
		public void onClick(View v) {
			changeThemeWithColorful();
		}
	});

	mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
	mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
	mRecyclerView.setItemViewCacheSize(0);
	// 模拟数据
	mockNews();
	mRecyclerView.setAdapter(new NewsAdapter());
	
	// 初始化Colorful
	setupColorful();
}
 
Example 2
Source File: SuntimesActivity.java    From SuntimesWidget with GNU General Public License v3.0 6 votes vote down vote up
/**
 * initialize the card flipper and associated views
 * @param context a context used to access resources
 */
private void initCardViews(Context context)
{
    card_adapter = new CardAdapter(context);
    card_adapter.setCardAdapterListener(cardAdapterListener);

    card_view = (RecyclerView) findViewById(R.id.info_time_flipper1);
    card_view.setHasFixedSize(true);
    card_view.setItemViewCacheSize(7);
    card_view.setLayoutManager(card_layout = new CardLayoutManager(this));
    card_view.addItemDecoration(new CardAdapter.CardViewDecorator(this));

    card_view.setAdapter(card_adapter);
    card_view.scrollToPosition(CardAdapter.TODAY_POSITION);

    SnapHelper snapHelper = new PagerSnapHelper();
    snapHelper.attachToRecyclerView(card_view);
    card_scroller = new CardAdapter.CardViewScroller(context);
    card_view.setOnScrollListener(onCardScrollListener);
}
 
Example 3
Source File: RippleDrawableActivity.java    From UltimateAndroid with Apache License 2.0 6 votes vote down vote up
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View decor = getWindow().getDecorView();

        RecyclerView recyclerView = new RecyclerView(this);
        recyclerView.setPadding(20, 20, 20, 20);

        stateList = getResources().getColorStateList(R.color.ripple_drawable_example_colors_state);

        setContentView(recyclerView);

        recyclerView.setAdapter(new RecyclerAdapter());
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        recyclerView.setHasFixedSize(true);
        recyclerView.setItemViewCacheSize(5);
//        recyclerView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                Toast.makeText(parent.getContext(), position + view.toString(), Toast.LENGTH_SHORT)
//                        .show();
//            }
//        });


    }
 
Example 4
Source File: FragmentExplorer.java    From iGap-Android with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    if (getArguments() != null) {
        mode = getArguments().getString("Mode");

        pickFile = (IPickFile) getArguments().getSerializable("Listener");
    }
    if (mode == null) {
        mode = "normal";
    }

    txtCurentPath = (TextView) view.findViewById(R.id.ae_txt_file_path);
    recyclerView = (RecyclerView) view.findViewById(R.id.ae_recycler_view_explorer);
    recyclerView.setItemViewCacheSize(100);
    mLayoutManager = new LinearLayoutManager(G.fragmentActivity);
    recyclerView.setLayoutManager(mLayoutManager);

    view.findViewById(R.id.ae_ll_toolbar).setBackgroundColor(Color.parseColor(G.appBarColor));

    item = new ArrayList<StructExplorerItem>();
    node = new ArrayList<String>();
    mscroll = new ArrayList<Integer>();

    btnBack = (MaterialDesignTextView) view.findViewById(R.id.ae_btn_back);
    RippleView rippleBack = (RippleView) view.findViewById(R.id.ae_ripple_back);
    rippleBack.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
        @Override
        public void onComplete(RippleView rippleView) {
            G.fragmentActivity.onBackPressed();
        }
    });

    firstfill();
}
 
Example 5
Source File: EquinoxView.java    From SuntimesWidget with GNU General Public License v3.0 5 votes vote down vote up
private void init(Context context, AttributeSet attrs)
{
    initLocale(context);
    themeViews(context);
    LayoutInflater.from(context).inflate(R.layout.layout_view_equinox, this, true);

    if (attrs != null)
    {
        LinearLayout.LayoutParams lp = generateLayoutParams(attrs);
        options.centered = ((lp.gravity == Gravity.CENTER) || (lp.gravity == Gravity.CENTER_HORIZONTAL));
    }

    empty = (TextView)findViewById(R.id.txt_empty);

    card_view = (RecyclerView)findViewById(R.id.info_equinoxsolstice_flipper1);
    card_view.setHasFixedSize(true);
    card_view.setItemViewCacheSize(7);
    card_view.setLayoutManager(card_layout = new CardLayoutManager(context));
    //card_view.addItemDecoration(new CardAdapter.CardViewDecorator(context));

    card_adapter = new EquinoxViewAdapter(context, options);
    card_adapter.setEquinoxViewListener(cardListener);
    card_view.setAdapter(card_adapter);
    card_view.scrollToPosition(EquinoxViewAdapter.CENTER_POSITION);

    SnapHelper snapHelper = new PagerSnapHelper();
    snapHelper.attachToRecyclerView(card_view);
    card_scroller = new CardAdapter.CardViewScroller(context);

    boolean minimized = isMinimized();
    if (!minimized) {
        card_view.setOnScrollListener(onCardScrollListener);
    }
    card_view.setLayoutFrozen(minimized);

    if (isInEditMode()) {
        updateViews(context);
    }
}
 
Example 6
Source File: CityWeatherActivity.java    From LittleFreshWeather with Apache License 2.0 4 votes vote down vote up
@Override
protected void initView() {
    setContentView(R.layout.activity_city_weather);
    //mUnbinder = ButterKnife.bind(this);

    rlLoadingProgress = (RelativeLayout)findViewById(R.id.rl_loading_progress);
    rlFailedRetry = (RelativeLayout)findViewById(R.id.rl_failed_retry);
    srlCityWeather = (SwipeRefreshLayout)findViewById(R.id.srl_city_weather);
    rvCityWeather = (RecyclerView)findViewById(android.R.id.list);
    btFailedRetry = (Button)findViewById(R.id.bt_failed_retry);
    tvToolbarTitle = (TextView)findViewById(R.id.tv_city_weather_toolbar_title);
    ibToolbarCities = (ImageButton)findViewById(R.id.ib_city_weather_toolbar_cities);
    ibToolbarMenu = (ImageButton)findViewById(R.id.ib_city_weather_toolbar_menu);
    rlBackgroundView = (RelativeLayout)findViewById(R.id.rl_city_weather_background_view);

    btFailedRetry.setOnClickListener(this);
    ibToolbarCities.setOnClickListener(this);
    ibToolbarMenu.setOnClickListener(this);
    srlCityWeather.setOnRefreshListener(this);
    srlCityWeather.setColorSchemeResources(R.color.colorLightGreen, R.color.colorLigthBlue, R.color.colorLightRed);

    Intent intent = getIntent();
    String cityId = intent.getStringExtra(CITY_ID);
    FileUtil.putStringToPreferences(getApplicationContext(), Constants.GLOBAL_SETTINGS, Constants.PRF_KEY_CITY_ID, cityId);

    mPresenter = new CityWeatherPresenter();
    mPresenter.attachView(this, cityId);
    mPresenter.start();

    rvCityWeather.setHasFixedSize(true);
    rvCityWeather.setLayoutManager(new LinearLayoutManager(this));
    mCityWeatherAdapter = new CityWeatherAdapter(this, mPresenter, rvCityWeather);
    rvCityWeather.setAdapter(mCityWeatherAdapter);
    rvCityWeather.setItemViewCacheSize(4);
    rvCityWeather.addOnScrollListener(new CityWeatherScrollListener());

    View popMenuView = getLayoutInflater().inflate(R.layout.ll_city_weather_pop_menu, null);
    if (popMenuView != null) {
        mPopupMenu = new PopupWindow(popMenuView, DensityUtil.dp2px(getApplicationContext(), 120), ViewGroup.LayoutParams.WRAP_CONTENT);
        mPopupMenu.setBackgroundDrawable(new ColorDrawable(0));
        mPopupMenu.setFocusable(true);
        View refreshView = popMenuView.findViewById(R.id.ll_pop_menu_item_refresh);
        refreshView.setOnClickListener(this);
        View settingsView = popMenuView.findViewById(R.id.ll_pop_menu_item_settings);
        settingsView.setOnClickListener(this);
    }
}
 
Example 7
Source File: MoonApsisView.java    From SuntimesWidget with GNU General Public License v3.0 4 votes vote down vote up
private void init(Context context, AttributeSet attrs)
{
    SuntimesUtils.initDisplayStrings(context);
    LayoutInflater.from(context).inflate(R.layout.layout_view_moonapsis, this, true);
    content = (LinearLayout)findViewById(R.id.moonapsis_layout);

    card_layout = new LinearLayoutManager(context);
    card_layout.setOrientation(LinearLayoutManager.HORIZONTAL);

    card_view = (RecyclerView)findViewById(R.id.moonapsis_card);
    card_view.setHasFixedSize(true);
    card_view.setItemViewCacheSize(7);
    card_view.setLayoutManager(card_layout);

    card_adapter = new MoonApsisAdapter(context);
    card_adapter.setItemWidth(Resources.getSystem().getDisplayMetrics().widthPixels / 3);  // initial width; 3 to screen; reassigned later in onSizeChanged

    card_view.setAdapter(card_adapter);
    card_view.scrollToPosition(MoonApsisAdapter.CENTER_POSITION);

    GravitySnapHelper snapHelper = new GravitySnapHelper(Gravity.START); // new LinearSnapHelper();
    snapHelper.attachToRecyclerView(card_view);

    forwardButton = (ImageButton)findViewById(R.id.info_time_nextbtn);
    forwardButton.setOnClickListener(onResetClick1);
    forwardButton.setVisibility(GONE);

    backButton = (ImageButton)findViewById(R.id.info_time_prevbtn);
    backButton.setOnClickListener(onResetClick0);
    backButton.setVisibility(VISIBLE);
    backButton.postDelayed(new Runnable() {
        @Override
        public void run() {
            int position = card_layout.findFirstVisibleItemPosition();
            if (position == MoonApsisAdapter.CENTER_POSITION) {
                ViewUtils.fadeOutButton(backButton, ViewUtils.ANIM_VERYLONG);
            }
        }
    }, 1200);

    card_view.setOnScrollListener(onScrollChanged);

    initTheme(context);
    if (isInEditMode()) {
        updateViews(context);
    }
}
 
Example 8
Source File: MoonPhasesView1.java    From SuntimesWidget with GNU General Public License v3.0 4 votes vote down vote up
private void init(Context context, AttributeSet attrs)
{
    initLocale(context);
    LayoutInflater.from(context).inflate(R.layout.layout_view_moonphases1, this, true);

    if (attrs != null)
    {
        LayoutParams lp = generateLayoutParams(attrs);
        centered = ((lp.gravity == Gravity.CENTER) || (lp.gravity == Gravity.CENTER_HORIZONTAL));
    }

    empty = (TextView)findViewById(R.id.txt_empty);
    card_view = (RecyclerView)findViewById(R.id.moonphases_card);

    card_layout = new LinearLayoutManager(context);
    card_layout.setOrientation(LinearLayoutManager.HORIZONTAL);

    card_view.setHasFixedSize(true);
    card_view.setItemViewCacheSize(7);
    card_view.setLayoutManager(card_layout);

    card_adapter = new PhaseAdapter(context);
    card_adapter.setItemWidth(Resources.getSystem().getDisplayMetrics().widthPixels / 4);  // initial width; reassigned later in onSizeChanged

    card_view.setAdapter(card_adapter);
    card_view.scrollToPosition(PhaseAdapter.CENTER_POSITION);

    GravitySnapHelper snapHelper = new GravitySnapHelper(Gravity.START); // new LinearSnapHelper();
    snapHelper.attachToRecyclerView(card_view);

    //card_scroller = new CardAdapter.CardViewScroller(context);
    //card_view.setOnScrollListener(onCardScrollListener);

    forwardButton = (ImageButton)findViewById(R.id.info_time_nextbtn);
    forwardButton.setOnClickListener(onResetClick1);
    forwardButton.setVisibility(GONE);

    backButton = (ImageButton)findViewById(R.id.info_time_prevbtn);
    backButton.setOnClickListener(onResetClick0);
    backButton.setVisibility(VISIBLE);
    backButton.postDelayed(new Runnable() {
        @Override
        public void run() {
            int position = card_layout.findFirstVisibleItemPosition();
            if (position == PhaseAdapter.CENTER_POSITION) {
                ViewUtils.fadeOutButton(backButton, ViewUtils.ANIM_VERYLONG);
            }
        }
    }, 1200);

    card_view.setOnScrollListener(onScrollChanged);

    initTheme(context);
    if (isInEditMode()) {
        updateViews(context);
    }
}
 
Example 9
Source File: HistoryView.java    From Slide with GNU General Public License v3.0 4 votes vote down vote up
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_verticalcontent, container, false);

    final RecyclerView rv = ((RecyclerView) v.findViewById(R.id.vertical_content));
    final PreCachingLayoutManager mLayoutManager;

    mLayoutManager = new PreCachingLayoutManager(getContext());


    rv.setLayoutManager(mLayoutManager);
    rv.setItemViewCacheSize(2);
    v.findViewById(R.id.post_floating_action_button).setVisibility(View.GONE);
    final SwipeRefreshLayout mSwipeRefreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.activity_main_swipe_refresh_layout);

    mSwipeRefreshLayout.setColorSchemeColors(Palette.getColors("default", getActivity()));

    //If we use 'findViewById(R.id.header).getMeasuredHeight()', 0 is always returned.
    //So, we estimate the height of the header in dp
    mSwipeRefreshLayout.setProgressViewOffset(false,
            Constants.TAB_HEADER_VIEW_OFFSET - Constants.PTR_OFFSET_TOP,
            Constants.TAB_HEADER_VIEW_OFFSET + Constants.PTR_OFFSET_BOTTOM);

    mSwipeRefreshLayout.post(new Runnable() {
        @Override
        public void run() {
            mSwipeRefreshLayout.setRefreshing(true);
        }
    });

    posts = new HistoryPosts();
    adapter = new ContributionAdapter(getActivity(), posts, rv);
    rv.setAdapter(adapter);

    posts.bindAdapter(adapter, mSwipeRefreshLayout);

    //TODO catch errors
    mSwipeRefreshLayout.setOnRefreshListener(
            new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    posts.loadMore(adapter, true);

                    //TODO catch errors
                }
            }
    );
    rv.addOnScrollListener(new ToolbarScrollHideHandler((Toolbar) getActivity().findViewById(R.id.toolbar), getActivity().findViewById(R.id.header)) {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            visibleItemCount = rv.getLayoutManager().getChildCount();
            totalItemCount = rv.getLayoutManager().getItemCount();
            if (rv.getLayoutManager() instanceof PreCachingLayoutManager) {
                pastVisiblesItems = ((PreCachingLayoutManager) rv.getLayoutManager()).findFirstVisibleItemPosition();
            } else {
                int[] firstVisibleItems = null;
                firstVisibleItems = ((CatchStaggeredGridLayoutManager) rv.getLayoutManager()).findFirstVisibleItemPositions(firstVisibleItems);
                if (firstVisibleItems != null && firstVisibleItems.length > 0) {
                    pastVisiblesItems = firstVisibleItems[0];
                }
            }

            if (!posts.loading) {
                if ((visibleItemCount + pastVisiblesItems) + 5 >= totalItemCount && !posts.nomore) {
                    posts.loading = true;
                    posts.loadMore(adapter, false);
                }
            }
        }
    });
    return v;
}
 
Example 10
Source File: ContributionsView.java    From Slide with GNU General Public License v3.0 4 votes vote down vote up
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_verticalcontent, container, false);

    final RecyclerView rv = ((RecyclerView) v.findViewById(R.id.vertical_content));
    final PreCachingLayoutManager mLayoutManager;

    mLayoutManager = new PreCachingLayoutManager(getContext());


    rv.setLayoutManager(mLayoutManager);
    rv.setItemViewCacheSize(2);
    v.findViewById(R.id.post_floating_action_button).setVisibility(View.GONE);
    final SwipeRefreshLayout mSwipeRefreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.activity_main_swipe_refresh_layout);

    mSwipeRefreshLayout.setColorSchemeColors(Palette.getColors(id, getActivity()));

    //If we use 'findViewById(R.id.header).getMeasuredHeight()', 0 is always returned.
    //So, we estimate the height of the header in dp
    mSwipeRefreshLayout.setProgressViewOffset(false,
            Constants.TAB_HEADER_VIEW_OFFSET - Constants.PTR_OFFSET_TOP,
            Constants.TAB_HEADER_VIEW_OFFSET + Constants.PTR_OFFSET_BOTTOM);

    mSwipeRefreshLayout.post(new Runnable() {
        @Override
        public void run() {
            mSwipeRefreshLayout.setRefreshing(true);
        }
    });

    if (where.equals("saved") && getActivity() instanceof Profile)
        posts = new ContributionPostsSaved(id, where, ((Profile) getActivity()).category);
    else
        posts = new ContributionPosts(id, where);

    //noinspection StringEquality
    if (where == "hidden") adapter = new ContributionAdapter(getActivity(), posts, rv, true);
    else adapter = new ContributionAdapter(getActivity(), posts, rv);
    rv.setAdapter(adapter);

    posts.bindAdapter(adapter, mSwipeRefreshLayout);
    //TODO catch errors
    mSwipeRefreshLayout.setOnRefreshListener(
            new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    posts.loadMore(adapter, id, true);
                    //TODO catch errors
                }
            }
    );
    rv.addOnScrollListener(new ToolbarScrollHideHandler((Toolbar) getActivity().findViewById(R.id.toolbar), getActivity().findViewById(R.id.header)) {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            visibleItemCount = rv.getLayoutManager().getChildCount();
            totalItemCount = rv.getLayoutManager().getItemCount();
            if (rv.getLayoutManager() instanceof PreCachingLayoutManager) {
                pastVisiblesItems = ((PreCachingLayoutManager) rv.getLayoutManager()).findFirstVisibleItemPosition();
            } else {
                int[] firstVisibleItems = null;
                firstVisibleItems = ((CatchStaggeredGridLayoutManager) rv.getLayoutManager()).findFirstVisibleItemPositions(firstVisibleItems);
                if (firstVisibleItems != null && firstVisibleItems.length > 0) {
                    pastVisiblesItems = firstVisibleItems[0];
                }
            }

            if (!posts.loading) {
                if ((visibleItemCount + pastVisiblesItems) + 5 >= totalItemCount && !posts.nomore) {
                    posts.loading = true;
                    posts.loadMore(adapter, id, false);
                }
            }
        }
    });
    return v;
}