Java tutorial
/* * Copyright 2014 Soichiro Kashima * * 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.babyspace.mamshare.app.activity; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.os.Bundle; import android.support.v7.widget.Toolbar; import android.view.View; import android.widget.AbsListView; import com.babyspace.mamshare.R; import com.babyspace.mamshare.adapter.GenericsAdapter; import com.babyspace.mamshare.bean.HomeGuidanceEvent; import com.babyspace.mamshare.bean.TestBean; import com.babyspace.mamshare.commons.AppConstants; import com.babyspace.mamshare.commons.UrlConstants; import com.google.gson.JsonObject; import com.michael.core.okhttp.OkHttpExecutor; import com.michael.library.debug.L; import com.michael.library.widget.ParallaxToolbar.BaseActivity; import com.michael.library.widget.ParallaxToolbar.observablescrollview.ObservableListView; import com.michael.library.widget.ParallaxToolbar.observablescrollview.ObservableScrollViewCallbacks; import com.michael.library.widget.ParallaxToolbar.observablescrollview.ScrollState; import com.michael.library.widget.ParallaxToolbar.observablescrollview.ScrollUtils; import com.nineoldandroids.view.ViewHelper; import com.squareup.okhttp.Call; import java.util.ArrayList; import java.util.List; import de.greenrobot.event.EventBus; public class ParallaxToolbarListViewActivity extends BaseActivity implements ObservableScrollViewCallbacks { // ? BaseActivity AppCompatActivity ?? private View mImageView; private View mToolbarView; private View mListBackgroundView; private ObservableListView listView; private int mParallaxImageHeight; private int firstVisiblePosition; private final int BACK_TOP_COUNT = 5; private final int queryNum = 10; private int queryStart = 0; private int queryCount = 0; private boolean isRefreshAdd = true; private boolean isMoreData = true; GenericsAdapter adapter; private Call queryCall; List<TestBean> data; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_parallaxtoolbarlistview); EventBus.getDefault().register(this); data = new ArrayList<>(); adapter = new GenericsAdapter(this, AppConstants.page_default); setSupportActionBar((Toolbar) findViewById(R.id.toolbar)); mImageView = findViewById(R.id.image); mToolbarView = findViewById(R.id.toolbar); mToolbarView.setBackgroundColor(ScrollUtils.getColorWithAlpha(0, getResources().getColor(R.color.primary))); mParallaxImageHeight = getResources().getDimensionPixelSize(R.dimen.parallax_image_height); listView = (ObservableListView) findViewById(R.id.list); listView.setScrollViewCallbacks(this); // Set padding view for ListView. This is the flexible space. View paddingView = new View(this); AbsListView.LayoutParams lp = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, mParallaxImageHeight); paddingView.setLayoutParams(lp); // This is required to disable header's list selector effect paddingView.setClickable(true); listView.addHeaderView(paddingView); listView.setSelector(new ColorDrawable(Color.TRANSPARENT)); adapter.refresh(AppConstants.page_default, data); listView.setAdapter(adapter); setDummyData(listView); // mListBackgroundView makes ListView's background except header view. mListBackgroundView = findViewById(R.id.list_background); listView.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { switch (scrollState) { //?@ case AbsListView.OnScrollListener.SCROLL_STATE_IDLE: // if (listView.getLastVisiblePosition() == (listView.getCount() - 1)) { if (isMoreData) { // ? isRefreshAdd = true; queryData(); } } // if (listView.getFirstVisiblePosition() == 0) { L.d("Michael", ""); //mSwipeLayout.setRefreshing(true); } break; case AbsListView.OnScrollListener.SCROLL_STATE_FLING: L.d("Michael", "SCROLL_STATE_FLING"); break; case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL: break; } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { firstVisiblePosition = listView.getFirstVisiblePosition(); if (firstVisiblePosition > BACK_TOP_COUNT) { //mBackTop.setVisibility(View.VISIBLE); } else { //mBackTop.setVisibility(View.INVISIBLE); } } }); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); onScrollChanged(listView.getCurrentScrollY(), false, false); } @Override public void onScrollChanged(int scrollY, boolean firstScroll, boolean dragging) { int baseColor = getResources().getColor(R.color.primary); float alpha = Math.min(1, (float) scrollY / mParallaxImageHeight); mToolbarView.setBackgroundColor(ScrollUtils.getColorWithAlpha(alpha, baseColor)); ViewHelper.setTranslationY(mImageView, -scrollY / 2); // Translate list background ViewHelper.setTranslationY(mListBackgroundView, Math.max(0, -scrollY + mParallaxImageHeight)); } @Override public void onDownMotionEvent() { } @Override public void onUpOrCancelMotionEvent(ScrollState scrollState) { } private void queryData() { //mSwipeLayout.setRefreshing(true); ++queryCount; /* footerProgressBar.setVisibility(View.VISIBLE); footerText.setText("...");*/ // Start0 if (!isRefreshAdd) queryStart = 0; JsonObject jsonParameter = new JsonObject(); jsonParameter.addProperty("num", queryNum); jsonParameter.addProperty("start", queryStart); //showLoadingProgress(); if (queryCall != null) queryCall.cancel(); queryCall = OkHttpExecutor.query(UrlConstants.HomeGuidanceList, jsonParameter, HomeGuidanceEvent.class, false, this); } /** * EventBus ? * * @param event */ public void onEventMainThread(HomeGuidanceEvent event) { L.d(OkHttpExecutor.TAG, "onEventMainThread-ParallaxToolbarListViewActivity>" + event.getResultStr()); List<TestBean> responseData = new ArrayList<>(); if (queryCount <= 6) { for (int i = 0; i < queryNum; i++) { responseData.add(new TestBean("More " + queryCount + " i " + i, false)); } } else { for (int i = 0; i < queryNum - 1; i++) { responseData.add(new TestBean("Last " + queryCount + " i " + i, false)); } } if (responseData.size() < queryNum) { /* footerProgressBar.setVisibility(View.INVISIBLE); footerText.setText("??~");*/ isMoreData = false; } else { /* footerProgressBar.setVisibility(View.INVISIBLE); footerText.setText("");*/ } if (isRefreshAdd) { queryStart += queryNum; data.addAll(responseData); isRefreshAdd = false; } else { data = responseData; // ? ? isMoreData = true; queryStart += queryNum; } adapter.refresh(AppConstants.page_default, data); } }