1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
| public class MainActivity extends ActionBarActivity {
ListView listView; Toolbar toolbar; View header; View footer; int touchSlop = 10;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); touchSlop = (int) (ViewConfiguration.get(MainActivity.this).getScaledTouchSlop() * 0.9); listView = (ListView) findViewById(R.id.list_view); footer = findViewById(R.id.footer); toolbar = (Toolbar) findViewById(R.id.action_bar); setSupportActionBar(toolbar);
String[] str = new String[64]; for (int i = 0; i < str.length; i++) { str[i] = "Android " + i; } ArrayAdapter<String> adapter = new ArrayAdapter<>(MainActivity.this, R.layout.simple_layout, str); listView.setAdapter(adapter);
header = new View(MainActivity.this); header.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) getResources().getDimension(R.dimen.abc_action_bar_default_height_material))); header.setBackgroundColor(Color.parseColor("#00000000")); listView.addHeaderView(header);
listView.setOnTouchListener(onTouchListener); listView.setOnScrollListener(onScrollListener); footer.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { listView.smoothScrollByOffset(10); } }); }
@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; }
@Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); }
AnimatorSet backAnimatorSet;
private void animateBack() { if (hideAnimatorSet != null && hideAnimatorSet.isRunning()) { hideAnimatorSet.cancel(); } if (backAnimatorSet != null && backAnimatorSet.isRunning()) { } else { backAnimatorSet = new AnimatorSet(); ObjectAnimator headerAnimator = ObjectAnimator.ofFloat(toolbar, "translationY", toolbar.getTranslationY(), 0f); ObjectAnimator footerAnimator = ObjectAnimator.ofFloat(footer, "translationY", footer.getTranslationY(), 0f); ArrayList<Animator> animators = new ArrayList<>(); animators.add(headerAnimator); animators.add(footerAnimator); backAnimatorSet.setDuration(300); backAnimatorSet.playTogether(animators); backAnimatorSet.start(); } }
AnimatorSet hideAnimatorSet;
private void animateHide() { if (backAnimatorSet != null && backAnimatorSet.isRunning()) { backAnimatorSet.cancel(); } if (hideAnimatorSet != null && hideAnimatorSet.isRunning()) { } else { hideAnimatorSet = new AnimatorSet(); ObjectAnimator headerAnimator = ObjectAnimator.ofFloat(toolbar, "translationY", toolbar.getTranslationY(), -toolbar.getHeight()); ObjectAnimator footerAnimator = ObjectAnimator.ofFloat(footer, "translationY", footer.getTranslationY(), footer.getHeight()); ArrayList<Animator> animators = new ArrayList<>(); animators.add(headerAnimator); animators.add(footerAnimator); hideAnimatorSet.setDuration(200); hideAnimatorSet.playTogether(animators); hideAnimatorSet.start(); } }
View.OnTouchListener onTouchListener = new View.OnTouchListener() {
float lastY = 0f; float currentY = 0f; int lastDirection = 0; int currentDirection = 0;
@Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: lastY = event.getY(); currentY = event.getY(); currentDirection = 0; lastDirection = 0; break; case MotionEvent.ACTION_MOVE: if (listView.getFirstVisiblePosition() > 0) { float tmpCurrentY = event.getY(); if (Math.abs(tmpCurrentY - lastY) > touchSlop) { currentY = tmpCurrentY; currentDirection = (int) (currentY - lastY); if (lastDirection != currentDirection) { if (currentDirection < 0) { animateHide(); } else { animateBack(); } } lastY = currentY; } } break; case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: currentDirection = 0; lastDirection = 0; break; } return false; } };
AbsListView.OnScrollListener onScrollListener = new AbsListView.OnScrollListener() {
int lastPosition = 0; int state = SCROLL_STATE_IDLE;
@Override public void onScrollStateChanged(AbsListView view, int scrollState) { state = scrollState; }
@Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { if (firstVisibleItem == 0) { animateBack(); } if (firstVisibleItem > 0) { if (firstVisibleItem > lastPosition && state == SCROLL_STATE_FLING) { animateHide(); }
if (firstVisibleItem < lastPosition && state == SCROLL_STATE_FLING) { animateBack(); }
} lastPosition = firstVisibleItem; } }; }
|