-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathListViewFragment.java
More file actions
132 lines (107 loc) · 4.59 KB
/
Copy pathListViewFragment.java
File metadata and controls
132 lines (107 loc) · 4.59 KB
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
package com.rahul.bounce;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.rahul.bounce.library.BounceTouchListener;
import java.util.Dictionary;
import java.util.Hashtable;
public class ListViewFragment extends Fragment implements AbsListView.OnScrollListener {
float pivotY1, pivotY2;
int headerHeight;
private ListView listView;
private View header;
private View headerOverlay;
private BounceTouchListener bounceTouchListener;
private Dictionary<Integer, Integer> listViewItemHeights = new Hashtable<>();
public ListViewFragment() {
}
public static ListViewFragment newInstance() {
ListViewFragment fragment = new ListViewFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_list_view, container, false);
header = root.findViewById(R.id.header_image_view);
headerHeight = (int) getResources().getDimension(R.dimen.header_height);
headerOverlay = root.findViewById(R.id.header_overlay);
headerOverlay.setAlpha(0);
listView = (ListView) root.findViewById(R.id.list_view);
listView.setAdapter(new DemoAdapter());
listView.setPivotX(Utils.getScreenWidth(getActivity()) * .5f);
pivotY1 = getResources().getDimension(R.dimen.header_height);
pivotY2 = (Utils.getScreenHeight(getActivity()) - getResources().getDimension(R.dimen.toolbar_size)) * .5f;
listView.setDivider(null);
listView.setDividerHeight(0);
listView.setOnScrollListener(this);
bounceTouchListener = BounceTouchListener.create(listView, new BounceTouchListener.OnTranslateListener() {
@Override
public void onTranslate(float translation) {
if (translation > 0) {
bounceTouchListener.setMaxAbsTranslation(-99);
listView.setPivotY(pivotY1);
float scale = ((2 * translation) / header.getMeasuredHeight()) + 1;
header.setScaleY(scale);
listView.setScaleY(scale);
} else {
bounceTouchListener.setMaxAbsTranslation((int) (pivotY2 * .33f));
listView.setPivotY(pivotY2);
float scale = ((2 * translation) / listView.getMeasuredHeight()) + 1;
listView.setScaleY((float) Math.pow(scale, .5f));
}
}
});
listView.setOnTouchListener(bounceTouchListener);
return root;
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
int headerTranslation = (int) (-getScroll() * .5f);
header.setTranslationY(headerTranslation);
headerOverlay.setTranslationY(headerTranslation);
headerOverlay.setAlpha(Math.min(1, ((float) getScroll()) / headerHeight));
}
private int getScroll() {
View c = listView.getChildAt(0);
if (c == null)
return 0;
int scrollY = -c.getTop();
listViewItemHeights.put(listView.getFirstVisiblePosition(), c.getHeight());
for (int i = 0; i < listView.getFirstVisiblePosition(); ++i) {
if (listViewItemHeights.get(i) != null)
scrollY += listViewItemHeights.get(i);
}
return scrollY;
}
private class DemoAdapter extends ArrayAdapter<Integer> {
public DemoAdapter() {
super(getActivity(), R.layout.item_sroll_view, new Integer[15]);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = getActivity().getLayoutInflater();
rowView = inflater.inflate(R.layout.item_sroll_view, null);
}
View pad = rowView.findViewById(R.id.pad);
pad.setVisibility(position == 0 ? View.VISIBLE : View.GONE);
return rowView;
}
}
}