|
1 |
| -/* |
2 |
| - * Copyright 2018 Hippo Seven |
3 |
| - * |
4 |
| - * Licensed under the Apache License, Version 2.0 (the "License"); |
5 |
| - * you may not use this file except in compliance with the License. |
6 |
| - * You may obtain a copy of the License at |
7 |
| - * |
8 |
| - * http://www.apache.org/licenses/LICENSE-2.0 |
9 |
| - * |
10 |
| - * Unless required by applicable law or agreed to in writing, software |
11 |
| - * distributed under the License is distributed on an "AS IS" BASIS, |
12 |
| - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 |
| - * See the License for the specific language governing permissions and |
14 |
| - * limitations under the License. |
15 |
| - */ |
16 |
| - |
17 |
| -package com.hippo.app; |
18 |
| - |
19 |
| -import android.content.Context; |
20 |
| -import android.preference.PreferenceActivity; |
21 |
| -import android.text.TextUtils; |
22 |
| -import android.view.LayoutInflater; |
23 |
| -import android.view.View; |
24 |
| -import android.view.ViewGroup; |
25 |
| -import android.widget.ArrayAdapter; |
26 |
| -import android.widget.ImageView; |
27 |
| -import android.widget.ListAdapter; |
28 |
| -import android.widget.TextView; |
29 |
| -import androidx.annotation.NonNull; |
30 |
| -import com.hippo.ehviewer.R; |
31 |
| -import java.util.ArrayList; |
32 |
| -import java.util.List; |
33 |
| - |
34 |
| -public abstract class PrettyPreferenceActivity extends AppCompatPreferenceActivity { |
35 |
| - |
36 |
| - @Override |
37 |
| - public void setListAdapter(ListAdapter adapter) { |
38 |
| - if (adapter == null) { |
39 |
| - super.setListAdapter(null); |
40 |
| - return; |
41 |
| - } |
42 |
| - |
43 |
| - int count = adapter.getCount(); |
44 |
| - List<Header> headers = new ArrayList<>(count); |
45 |
| - for (int i = 0; i < count; ++i) { |
46 |
| - headers.add((Header) adapter.getItem(i)); |
47 |
| - } |
48 |
| - |
49 |
| - super.setListAdapter(new HeaderAdapter(this, headers, R.layout.item_preference_header, true)); |
50 |
| - } |
51 |
| - |
52 |
| - private static class HeaderAdapter extends ArrayAdapter<Header> { |
53 |
| - private static class HeaderViewHolder { |
54 |
| - ImageView icon; |
55 |
| - TextView title; |
56 |
| - TextView summary; |
57 |
| - } |
58 |
| - |
59 |
| - private LayoutInflater mInflater; |
60 |
| - private int mLayoutResId; |
61 |
| - private boolean mRemoveIconIfEmpty; |
62 |
| - |
63 |
| - private HeaderAdapter(Context context, List<Header> objects, int layoutResId, |
64 |
| - boolean removeIconBehavior) { |
65 |
| - super(context, 0, objects); |
66 |
| - mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
67 |
| - mLayoutResId = layoutResId; |
68 |
| - mRemoveIconIfEmpty = removeIconBehavior; |
69 |
| - } |
70 |
| - |
71 |
| - @NonNull |
72 |
| - @Override |
73 |
| - public View getView(int position, View convertView, @NonNull ViewGroup parent) { |
74 |
| - HeaderViewHolder holder; |
75 |
| - View view; |
76 |
| - |
77 |
| - if (convertView == null) { |
78 |
| - view = mInflater.inflate(mLayoutResId, parent, false); |
79 |
| - holder = new HeaderViewHolder(); |
80 |
| - holder.icon = view.findViewById(android.R.id.icon); |
81 |
| - holder.title = view.findViewById(android.R.id.title); |
82 |
| - holder.summary = view.findViewById(android.R.id.summary); |
83 |
| - view.setTag(holder); |
84 |
| - } else { |
85 |
| - view = convertView; |
86 |
| - holder = (HeaderViewHolder) view.getTag(); |
87 |
| - } |
88 |
| - |
89 |
| - // All view fields must be updated every time, because the view may be recycled |
90 |
| - Header header = getItem(position); |
91 |
| - if (mRemoveIconIfEmpty) { |
92 |
| - if (header.iconRes == 0) { |
93 |
| - holder.icon.setVisibility(View.GONE); |
94 |
| - } else { |
95 |
| - holder.icon.setVisibility(View.VISIBLE); |
96 |
| - holder.icon.setImageResource(header.iconRes); |
97 |
| - } |
98 |
| - } else { |
99 |
| - holder.icon.setImageResource(header.iconRes); |
100 |
| - } |
101 |
| - holder.title.setText(header.getTitle(getContext().getResources())); |
102 |
| - CharSequence summary = header.getSummary(getContext().getResources()); |
103 |
| - if (!TextUtils.isEmpty(summary)) { |
104 |
| - holder.summary.setVisibility(View.VISIBLE); |
105 |
| - holder.summary.setText(summary); |
106 |
| - } else { |
107 |
| - holder.summary.setVisibility(View.GONE); |
108 |
| - } |
109 |
| - |
110 |
| - return view; |
111 |
| - } |
112 |
| - } |
113 |
| -} |
| 1 | +///* |
| 2 | +// * Copyright 2018 Hippo Seven |
| 3 | +// * |
| 4 | +// * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// * you may not use this file except in compliance with the License. |
| 6 | +// * You may obtain a copy of the License at |
| 7 | +// * |
| 8 | +// * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// * |
| 10 | +// * Unless required by applicable law or agreed to in writing, software |
| 11 | +// * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// * See the License for the specific language governing permissions and |
| 14 | +// * limitations under the License. |
| 15 | +// */ |
| 16 | +// |
| 17 | +//package com.hippo.app; |
| 18 | +// |
| 19 | +//import android.content.Context; |
| 20 | +//import android.text.TextUtils; |
| 21 | +//import android.view.LayoutInflater; |
| 22 | +//import android.view.View; |
| 23 | +//import android.view.ViewGroup; |
| 24 | +//import android.widget.ArrayAdapter; |
| 25 | +//import android.widget.ImageView; |
| 26 | +//import android.widget.ListAdapter; |
| 27 | +//import android.widget.TextView; |
| 28 | +//import androidx.annotation.NonNull; |
| 29 | +//import com.hippo.ehviewer.R; |
| 30 | +//import java.util.ArrayList; |
| 31 | +//import java.util.List; |
| 32 | +// |
| 33 | +//public abstract class PrettyPreferenceActivity extends AppCompatPreferenceActivity { |
| 34 | +// |
| 35 | +// @Override |
| 36 | +// public void setListAdapter(ListAdapter adapter) { |
| 37 | +// if (adapter == null) { |
| 38 | +// super.setListAdapter(null); |
| 39 | +// return; |
| 40 | +// } |
| 41 | +// |
| 42 | +// int count = adapter.getCount(); |
| 43 | +// List<Header> headers = new ArrayList<>(count); |
| 44 | +// for (int i = 0; i < count; ++i) { |
| 45 | +// headers.add((Header) adapter.getItem(i)); |
| 46 | +// } |
| 47 | +// |
| 48 | +// super.setListAdapter(new HeaderAdapter(this, headers, R.layout.item_preference_header, true)); |
| 49 | +// } |
| 50 | +// |
| 51 | +// private static class HeaderAdapter extends ArrayAdapter<Header> { |
| 52 | +// private static class HeaderViewHolder { |
| 53 | +// ImageView icon; |
| 54 | +// TextView title; |
| 55 | +// TextView summary; |
| 56 | +// } |
| 57 | +// |
| 58 | +// private LayoutInflater mInflater; |
| 59 | +// private int mLayoutResId; |
| 60 | +// private boolean mRemoveIconIfEmpty; |
| 61 | +// |
| 62 | +// private HeaderAdapter(Context context, List<Header> objects, int layoutResId, |
| 63 | +// boolean removeIconBehavior) { |
| 64 | +// super(context, 0, objects); |
| 65 | +// mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 66 | +// mLayoutResId = layoutResId; |
| 67 | +// mRemoveIconIfEmpty = removeIconBehavior; |
| 68 | +// } |
| 69 | +// |
| 70 | +// @NonNull |
| 71 | +// @Override |
| 72 | +// public View getView(int position, View convertView, @NonNull ViewGroup parent) { |
| 73 | +// HeaderViewHolder holder; |
| 74 | +// View view; |
| 75 | +// |
| 76 | +// if (convertView == null) { |
| 77 | +// view = mInflater.inflate(mLayoutResId, parent, false); |
| 78 | +// holder = new HeaderViewHolder(); |
| 79 | +// holder.icon = view.findViewById(android.R.id.icon); |
| 80 | +// holder.title = view.findViewById(android.R.id.title); |
| 81 | +// holder.summary = view.findViewById(android.R.id.summary); |
| 82 | +// view.setTag(holder); |
| 83 | +// } else { |
| 84 | +// view = convertView; |
| 85 | +// holder = (HeaderViewHolder) view.getTag(); |
| 86 | +// } |
| 87 | +// |
| 88 | +// // All view fields must be updated every time, because the view may be recycled |
| 89 | +// Header header = getItem(position); |
| 90 | +// if (mRemoveIconIfEmpty) { |
| 91 | +// if (header.iconRes == 0) { |
| 92 | +// holder.icon.setVisibility(View.GONE); |
| 93 | +// } else { |
| 94 | +// holder.icon.setVisibility(View.VISIBLE); |
| 95 | +// holder.icon.setImageResource(header.iconRes); |
| 96 | +// } |
| 97 | +// } else { |
| 98 | +// holder.icon.setImageResource(header.iconRes); |
| 99 | +// } |
| 100 | +// holder.title.setText(header.getTitle(getContext().getResources())); |
| 101 | +// CharSequence summary = header.getSummary(getContext().getResources()); |
| 102 | +// if (!TextUtils.isEmpty(summary)) { |
| 103 | +// holder.summary.setVisibility(View.VISIBLE); |
| 104 | +// holder.summary.setText(summary); |
| 105 | +// } else { |
| 106 | +// holder.summary.setVisibility(View.GONE); |
| 107 | +// } |
| 108 | +// |
| 109 | +// return view; |
| 110 | +// } |
| 111 | +// } |
| 112 | +//} |
0 commit comments