├── .gitignore ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── hadlink │ │ └── recyclerviewhelpper │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── hadlink │ │ │ └── recyclerviewhelpper │ │ │ ├── MainActivity.java │ │ │ ├── aty │ │ │ ├── GridViewAty.java │ │ │ ├── ListViewAty.java │ │ │ ├── RecyclerViewAty.java │ │ │ └── RecyclerViewMulAty.java │ │ │ └── unrelated │ │ │ ├── DataEngine.java │ │ │ ├── FuelAdapter.java │ │ │ ├── FuelBean.java │ │ │ ├── LabelView_New.java │ │ │ ├── MathUtil.java │ │ │ └── MultiAdapter.java │ └── res │ │ ├── drawable │ │ ├── selector_fuel_item.xml │ │ ├── selector_text_fuel_item_b.xml │ │ └── selector_text_fuel_item_n.xml │ │ ├── layout │ │ ├── activity_gv.xml │ │ ├── activity_lv.xml │ │ ├── activity_main.xml │ │ ├── activity_rv.xml │ │ ├── content_main.xml │ │ ├── item1.xml │ │ └── item2.xml │ │ ├── menu │ │ └── menu_main.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-v21 │ │ └── styles.xml │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── attrs.xml │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── hadlink │ └── recyclerviewhelpper │ └── ExampleUnitTest.java ├── build.gradle ├── example.gif ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── rvhelpperlib ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── hadlink │ │ └── rvhelpperlib │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── hadlink │ │ │ └── rvhelpperlib │ │ │ ├── adapter │ │ │ ├── AdapterViewAdapter.java │ │ │ ├── AdapterViewHolder.java │ │ │ ├── OnItemChildCheckedChangeListener.java │ │ │ ├── OnItemChildClickListener.java │ │ │ ├── OnItemChildLongClickListener.java │ │ │ ├── OnRVItemClickListener.java │ │ │ ├── OnRVItemLongClickListener.java │ │ │ ├── RecyclerViewAdapter.java │ │ │ ├── RecyclerViewHolder.java │ │ │ └── ViewHolderHelper.java │ │ │ ├── decoration │ │ │ ├── CommonItemDecoration.java │ │ │ └── GridItemDecoration.java │ │ │ ├── manager │ │ │ ├── WRGridLayoutManager.java │ │ │ └── WRLinearLayoutManager.java │ │ │ └── utils │ │ │ └── DensityUtils.java │ └── res │ │ └── values │ │ └── strings.xml │ └── test │ └── java │ └── com │ └── hadlink │ └── rvhelpperlib │ └── ExampleUnitTest.java └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | .DS_Store 5 | /build 6 | /captures 7 | .idea 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 lyao lomoliger@hotmail.com 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | RecyclerViewHelpper 2 | -------- 3 | ![Bintray](http://i.imgur.com/ohkyaD6.png) 4 | 5 | Android library project that intends to simplify the usage of Adapters for ListView/GridView and RecyclerView. You won't have to code any boring adapter again! 6 | 7 | It helps to keep boilerplate to a minimum and adds the possibility of easily changing between BaseAdapter / RecyclerView.Adapter adapter styles without changing any code. It also allows painless usage of multiple models / view types for the same list or grid 8 | ## Feature ## 9 | - adapter for RecyclerView & AdapterView 10 | 11 | 12 | - height WRAP-CONTENT for RecyclerView(LinearLayout & GirdLayout) 13 | 14 | 15 | - CommonItemDecoration for RecyclerView 16 | 17 | 18 | ---------- 19 | ## Adding to your project ## 20 | 21 | Add this to your dependencies: 22 | 23 | compile 'com.hadlink:rvhelpperlib:1.0.3' 24 | 25 | 26 | ---------- 27 | 28 | ![image](https://github.com/vihuela/RecyclerViewHelpper/blob/master/example.gif ) 29 | 30 | ---------- 31 | ## Code ## 32 | 33 | > RecyclerView **SingleItem** 单个条目 34 | 35 | 36 | 37 | 38 | - adapter 39 | 40 | public class FuelAdapter extends RecyclerViewAdapter { 41 | public FuelAdapter(RecyclerView recyclerView, List list) { 42 | super(recyclerView, R.layout.item2, list); 43 | } 44 | 45 | @Override protected void setItemChildListener(ViewHolderHelper viewHolderHelper) { 46 | super.setItemChildListener(viewHolderHelper); 47 | viewHolderHelper.setItemChildClickListener(R.id.faveVal); 48 | viewHolderHelper.setItemChildClickListener(R.id.priceLayout); 49 | viewHolderHelper.setItemChildClickListener(R.id.labelView); 50 | } 51 | 52 | @Override protected void fillData(ViewHolderHelper viewHolderHelper, int position, FuelBean model) { 53 | LabelView_New labelViewNew = viewHolderHelper.getView(R.id.labelView); 54 | labelViewNew.setText(model.discount + "折"); 55 | viewHolderHelper.setText(R.id.faveVal, model.faceVal + ""); 56 | viewHolderHelper.setText(R.id.price, model.price + ""); 57 | viewHolderHelper.getView(R.id.mainContent).setSelected(model.isSelect); 58 | } 59 | } 60 | 61 | 62 | 63 | - activity 64 | 65 | rv.setLayoutManager(new WRGridLayoutManager(this, 2)); 66 | final FuelAdapter fuelAdapter = new FuelAdapter(rv, DataEngine.S_MOCK4); 67 | /** 68 | * item点击 69 | */ 70 | fuelAdapter.setOnRVItemClickListener(new OnRVItemClickListener() { 71 | @Override public void onRVItemClick(ViewGroup parent, View itemView, int position) { 72 | fuelAdapter.setSelectItem(position, null); 73 | } 74 | }); 75 | /** 76 | * item子条目点击 77 | */ 78 | fuelAdapter.setOnItemChildClickListener(new OnItemChildClickListener() { 79 | @Override public void onItemChildClick(ViewGroup parent, View childView, int position) { 80 | FuelBean item = fuelAdapter.getItem(position); 81 | switch (childView.getId()) { 82 | case R.id.faveVal: 83 | Toast.makeText(parent.getContext(), item.faceVal + "", Toast.LENGTH_SHORT).show(); 84 | break; 85 | case R.id.priceLayout: 86 | Toast.makeText(parent.getContext(), item.price + "", Toast.LENGTH_SHORT).show(); 87 | break; 88 | case R.id.labelView: 89 | Toast.makeText(parent.getContext(), item.discount + "", Toast.LENGTH_SHORT).show(); 90 | break; 91 | } 92 | } 93 | }); 94 | rv.addItemDecoration(new GridItemDecoration(2, DensityUtils.dip2px(this, 14f), true)); 95 | rv.setAdapter(fuelAdapter); 96 | 97 | 98 | > RecyclerView **MultiItem** 多个条目 99 | 100 | - adapter 101 | 102 | 103 | public class MultiAdapter extends RecyclerViewAdapter { 104 | public final static int TYPE_1 = 1; 105 | public final static int TYPE_2 = 2; 106 | 107 | public MultiAdapter(RecyclerView recyclerView) { 108 | super(recyclerView, R.layout.item2); 109 | } 110 | 111 | @Override protected int getItemViewResId(int viewType) { 112 | switch (viewType) { 113 | case TYPE_1: 114 | return R.layout.item1; 115 | case TYPE_2: 116 | return R.layout.item2; 117 | } 118 | return super.getItemViewResId(viewType); 119 | } 120 | 121 | @Override public int getItemViewType(int position) { 122 | 123 | return (getItem(position) instanceof String) ? TYPE_1 : TYPE_2; 124 | } 125 | 126 | @Override protected void fillData(ViewHolderHelper viewHolderHelper, int position, Object model) { 127 | switch (getItemViewType(position)) { 128 | case TYPE_1: 129 | String s = (String) model; 130 | viewHolderHelper.setText(R.id.tv, s); 131 | break; 132 | case TYPE_2: 133 | FuelBean fuelBean = (FuelBean) model; 134 | LabelView_New labelViewNew = viewHolderHelper.getView(R.id.labelView); 135 | labelViewNew.setText(fuelBean.discount + "折"); 136 | viewHolderHelper.setText(R.id.faveVal, fuelBean.faceVal + ""); 137 | viewHolderHelper.setText(R.id.price, fuelBean.price + ""); 138 | viewHolderHelper.getView(R.id.mainContent).setSelected(fuelBean.isSelect); 139 | break; 140 | } 141 | 142 | } 143 | 144 | } 145 | 146 | - activity 147 | 148 | // 149 | 150 | > ListView or GirdView 151 | 152 | ListView lv = (ListView) findViewById(R.id.lv); 153 | final AdapterViewAdapter adapter = new AdapterViewAdapter(this, R.layout.item1, DataEngine.S_MOCK1) { 154 | 155 | @Override protected void fillData(ViewHolderHelper viewHolderHelper, int position, String model) { 156 | viewHolderHelper.setText(R.id.tv, model); 157 | } 158 | 159 | @Override protected void setItemChildListener(ViewHolderHelper viewHolderHelper) { 160 | viewHolderHelper.setItemChildClickListener(R.id.rootView); 161 | } 162 | }; 163 | adapter.setOnItemChildClickListener(new OnItemChildClickListener() { 164 | @Override public void onItemChildClick(ViewGroup parent, View childView, int position) { 165 | if (childView.getId() == R.id.rootView) 166 | Toast.makeText(parent.getContext(), adapter.getItem(position), Toast.LENGTH_SHORT).show(); 167 | } 168 | }); 169 | lv.setAdapter(adapter); 170 | 171 | 172 | 173 | 174 | 175 | 176 | > **Check Demo for Detail** 177 | 178 | 179 | 180 | 181 | Common issues 182 | ------------- 183 | 184 | RecyclerView item height must be Consistency for wrap-centent in linerLayoutManager 185 | 186 | Contributing 187 | ------------ 188 | Forks, patches and other feedback are welcome. 189 | 190 | Creators 191 | -------- 192 | 193 | vihuela - Github [@vihuela](https://github.com/vihuela) 194 | License 195 | ------- 196 | 197 | [MIT License](LICENSE) 198 | 199 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, lyao. lomoliger@hotmail.com 4 | * 5 | * Part of the code from the open source community, 6 | * thanks stackOverflow & gitHub . 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * 19 | */ 20 | 21 | apply plugin: 'com.android.application' 22 | 23 | android { 24 | compileSdkVersion 23 25 | buildToolsVersion "23.0.2" 26 | 27 | defaultConfig { 28 | applicationId "com.hadlink.recyclerviewhelpper" 29 | minSdkVersion 10 30 | targetSdkVersion 23 31 | versionCode 1 32 | versionName "1.0" 33 | } 34 | buildTypes { 35 | release { 36 | minifyEnabled false 37 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 38 | } 39 | } 40 | } 41 | 42 | dependencies { 43 | compile fileTree(include: ['*.jar'], dir: 'libs') 44 | testCompile 'junit:junit:4.12' 45 | compile 'com.android.support:appcompat-v7:23.1.1' 46 | compile 'com.android.support:design:23.1.1' 47 | compile 'com.hadlink:rvhelpperlib:1.0.2' 48 | } 49 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in F:\android-studio\sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/hadlink/recyclerviewhelpper/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, lyao. lomoliger@hotmail.com 4 | * 5 | * Part of the code from the open source community, 6 | * thanks stackOverflow & gitHub . 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * 19 | */ 20 | 21 | package com.hadlink.recyclerviewhelpper; 22 | 23 | import android.app.Application; 24 | import android.test.ApplicationTestCase; 25 | 26 | /** 27 | * Testing Fundamentals 28 | */ 29 | public class ApplicationTest extends ApplicationTestCase { 30 | public ApplicationTest() { 31 | super(Application.class); 32 | } 33 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 21 | 22 | 25 | 26 | 32 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /app/src/main/java/com/hadlink/recyclerviewhelpper/MainActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, lyao. lomoliger@hotmail.com 4 | * 5 | * Part of the code from the open source community, 6 | * thanks stackOverflow & gitHub . 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * 19 | */ 20 | 21 | package com.hadlink.recyclerviewhelpper; 22 | 23 | import android.content.Intent; 24 | import android.os.Bundle; 25 | import android.support.v7.app.AppCompatActivity; 26 | import android.support.v7.widget.Toolbar; 27 | import android.view.View; 28 | 29 | import com.hadlink.recyclerviewhelpper.aty.GridViewAty; 30 | import com.hadlink.recyclerviewhelpper.aty.ListViewAty; 31 | import com.hadlink.recyclerviewhelpper.aty.RecyclerViewAty; 32 | import com.hadlink.recyclerviewhelpper.aty.RecyclerViewMulAty; 33 | 34 | public class MainActivity extends AppCompatActivity { 35 | 36 | 37 | @Override 38 | protected void onCreate(Bundle savedInstanceState) { 39 | super.onCreate(savedInstanceState); 40 | setContentView(R.layout.activity_main); 41 | Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 42 | setSupportActionBar(toolbar); 43 | } 44 | 45 | 46 | public void rv_single(View v) { 47 | startActivity(new Intent(this, RecyclerViewAty.class)); 48 | } 49 | 50 | public void rv_multi(View v) { 51 | startActivity(new Intent(this, RecyclerViewMulAty.class)); 52 | } 53 | 54 | public void lv(View v) { 55 | startActivity(new Intent(this, ListViewAty.class)); 56 | } 57 | 58 | public void gv(View v) { 59 | startActivity(new Intent(this, GridViewAty.class)); 60 | } 61 | 62 | 63 | } 64 | -------------------------------------------------------------------------------- /app/src/main/java/com/hadlink/recyclerviewhelpper/aty/GridViewAty.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, lyao. lomoliger@hotmail.com 4 | * 5 | * Part of the code from the open source community, 6 | * thanks stackOverflow & gitHub . 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * 19 | */ 20 | 21 | package com.hadlink.recyclerviewhelpper.aty; 22 | 23 | import android.content.Context; 24 | import android.os.Bundle; 25 | import android.support.v7.app.AppCompatActivity; 26 | import android.view.View; 27 | import android.view.ViewGroup; 28 | import android.widget.GridView; 29 | 30 | import com.hadlink.recyclerviewhelpper.R; 31 | import com.hadlink.recyclerviewhelpper.unrelated.DataEngine; 32 | import com.hadlink.recyclerviewhelpper.unrelated.FuelBean; 33 | import com.hadlink.recyclerviewhelpper.unrelated.LabelView_New; 34 | import com.hadlink.rvhelpperlib.adapter.AdapterViewAdapter; 35 | import com.hadlink.rvhelpperlib.adapter.OnItemChildClickListener; 36 | import com.hadlink.rvhelpperlib.adapter.ViewHolderHelper; 37 | 38 | import java.util.List; 39 | 40 | /** 41 | * @author Created by lyao on 2016/2/1. 42 | * @description 43 | */ 44 | public class GridViewAty extends AppCompatActivity { 45 | @Override protected void onCreate(Bundle savedInstanceState) { 46 | super.onCreate(savedInstanceState); 47 | setContentView(R.layout.activity_gv); 48 | 49 | GridView gv = (GridView) findViewById(R.id.gv); 50 | final GridViewAdp adapter = new GridViewAdp(this, R.layout.item2, DataEngine.S_MOCK4); 51 | adapter.setOnItemChildClickListener(new OnItemChildClickListener() { 52 | @Override public void onItemChildClick(ViewGroup parent, View childView, int position) { 53 | if (childView.getId() == R.id.mainContent) { 54 | adapter.setSelectItem(position); 55 | } 56 | } 57 | }); 58 | gv.setAdapter(adapter); 59 | } 60 | 61 | private class GridViewAdp extends AdapterViewAdapter { 62 | 63 | public GridViewAdp(Context context, int itemLayoutId, List list) { 64 | super(context, itemLayoutId, list); 65 | } 66 | 67 | @Override protected void setItemChildListener(ViewHolderHelper viewHolderHelper) { 68 | super.setItemChildListener(viewHolderHelper); 69 | viewHolderHelper.setItemChildClickListener(R.id.mainContent); 70 | } 71 | 72 | @Override protected void fillData(ViewHolderHelper viewHolderHelper, int position, FuelBean model) { 73 | LabelView_New labelViewNew = viewHolderHelper.getView(R.id.labelView); 74 | labelViewNew.setText(model.discount + "折"); 75 | viewHolderHelper.setText(R.id.faveVal, model.faceVal + ""); 76 | viewHolderHelper.setText(R.id.price, model.price + ""); 77 | viewHolderHelper.getView(R.id.mainContent).setSelected(model.isSelect); 78 | } 79 | 80 | public void setSelectItem(int position) { 81 | List datas = getDatas(); 82 | FuelBean fuelBean = datas.get(position); 83 | if (!fuelBean.isSelect) { 84 | for (FuelBean f : datas) { 85 | f.isSelect = false; 86 | } 87 | fuelBean.isSelect = true; 88 | notifyDataSetChanged(); 89 | } 90 | } 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /app/src/main/java/com/hadlink/recyclerviewhelpper/aty/ListViewAty.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, lyao. lomoliger@hotmail.com 4 | * 5 | * Part of the code from the open source community, 6 | * thanks stackOverflow & gitHub . 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * 19 | */ 20 | 21 | package com.hadlink.recyclerviewhelpper.aty; 22 | 23 | import android.os.Bundle; 24 | import android.support.v7.app.AppCompatActivity; 25 | import android.view.View; 26 | import android.view.ViewGroup; 27 | import android.widget.ListView; 28 | import android.widget.Toast; 29 | 30 | import com.hadlink.recyclerviewhelpper.R; 31 | import com.hadlink.recyclerviewhelpper.unrelated.DataEngine; 32 | import com.hadlink.rvhelpperlib.adapter.AdapterViewAdapter; 33 | import com.hadlink.rvhelpperlib.adapter.OnItemChildClickListener; 34 | import com.hadlink.rvhelpperlib.adapter.ViewHolderHelper; 35 | 36 | /** 37 | * @author Created by lyao on 2016/2/1. 38 | * @description 39 | */ 40 | public class ListViewAty extends AppCompatActivity { 41 | @Override protected void onCreate(Bundle savedInstanceState) { 42 | super.onCreate(savedInstanceState); 43 | setContentView(R.layout.activity_lv); 44 | 45 | ListView lv = (ListView) findViewById(R.id.lv); 46 | final AdapterViewAdapter adapter = new AdapterViewAdapter(this, R.layout.item1, DataEngine.S_MOCK1) { 47 | 48 | @Override protected void fillData(ViewHolderHelper viewHolderHelper, int position, String model) { 49 | viewHolderHelper.setText(R.id.tv, model); 50 | } 51 | 52 | @Override protected void setItemChildListener(ViewHolderHelper viewHolderHelper) { 53 | viewHolderHelper.setItemChildClickListener(R.id.rootView); 54 | } 55 | }; 56 | adapter.setOnItemChildClickListener(new OnItemChildClickListener() { 57 | @Override public void onItemChildClick(ViewGroup parent, View childView, int position) { 58 | if (childView.getId() == R.id.rootView) 59 | Toast.makeText(parent.getContext(), adapter.getItem(position), Toast.LENGTH_SHORT).show(); 60 | } 61 | }); 62 | lv.setAdapter(adapter); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /app/src/main/java/com/hadlink/recyclerviewhelpper/aty/RecyclerViewAty.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, lyao. lomoliger@hotmail.com 4 | * 5 | * Part of the code from the open source community, 6 | * thanks stackOverflow & gitHub . 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * 19 | */ 20 | 21 | package com.hadlink.recyclerviewhelpper.aty; 22 | 23 | import android.os.Bundle; 24 | import android.support.v7.app.AppCompatActivity; 25 | import android.support.v7.widget.LinearLayoutManager; 26 | import android.support.v7.widget.RecyclerView; 27 | import android.view.View; 28 | import android.view.ViewGroup; 29 | import android.widget.Toast; 30 | 31 | import com.hadlink.recyclerviewhelpper.R; 32 | import com.hadlink.recyclerviewhelpper.unrelated.DataEngine; 33 | import com.hadlink.recyclerviewhelpper.unrelated.FuelAdapter; 34 | import com.hadlink.recyclerviewhelpper.unrelated.FuelBean; 35 | import com.hadlink.rvhelpperlib.adapter.OnItemChildClickListener; 36 | import com.hadlink.rvhelpperlib.adapter.OnRVItemClickListener; 37 | import com.hadlink.rvhelpperlib.adapter.RecyclerViewAdapter; 38 | import com.hadlink.rvhelpperlib.adapter.ViewHolderHelper; 39 | import com.hadlink.rvhelpperlib.decoration.GridItemDecoration; 40 | import com.hadlink.rvhelpperlib.manager.WRGridLayoutManager; 41 | import com.hadlink.rvhelpperlib.manager.WRLinearLayoutManager; 42 | import com.hadlink.rvhelpperlib.utils.DensityUtils; 43 | 44 | /** 45 | * @author Created by lyao on 2016/2/1. 46 | * @description 47 | */ 48 | public class RecyclerViewAty extends AppCompatActivity { 49 | private RecyclerView rv; 50 | 51 | @Override protected void onCreate(Bundle savedInstanceState) { 52 | super.onCreate(savedInstanceState); 53 | setContentView(R.layout.activity_rv); 54 | rv = (RecyclerView) findViewById(R.id.rv); 55 | 56 | demo2(); 57 | } 58 | 59 | private void demo2() { 60 | rv.setLayoutManager(new WRGridLayoutManager(this, 2)); 61 | final FuelAdapter fuelAdapter = new FuelAdapter(rv, DataEngine.S_MOCK4); 62 | /** 63 | * item点击 64 | */ 65 | fuelAdapter.setOnRVItemClickListener(new OnRVItemClickListener() { 66 | @Override public void onRVItemClick(ViewGroup parent, View itemView, int position) { 67 | fuelAdapter.setSelectItem(position, null); 68 | } 69 | }); 70 | /** 71 | * item子条目点击 72 | */ 73 | fuelAdapter.setOnItemChildClickListener(new OnItemChildClickListener() { 74 | @Override public void onItemChildClick(ViewGroup parent, View childView, int position) { 75 | FuelBean item = fuelAdapter.getItem(position); 76 | switch (childView.getId()) { 77 | case R.id.faveVal: 78 | Toast.makeText(parent.getContext(), item.faceVal + "", Toast.LENGTH_SHORT).show(); 79 | break; 80 | case R.id.priceLayout: 81 | Toast.makeText(parent.getContext(), item.price + "", Toast.LENGTH_SHORT).show(); 82 | break; 83 | case R.id.labelView: 84 | Toast.makeText(parent.getContext(), item.discount + "", Toast.LENGTH_SHORT).show(); 85 | break; 86 | } 87 | } 88 | }); 89 | rv.addItemDecoration(new GridItemDecoration(2, DensityUtils.dip2px(this, 14f), true)); 90 | rv.setAdapter(fuelAdapter); 91 | } 92 | 93 | private void demo1() { 94 | rv.setLayoutManager(new WRLinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)); 95 | rv.setAdapter(new RecyclerViewAdapter(rv, R.layout.item1, DataEngine.S_MOCK2) { 96 | 97 | @Override protected void fillData(ViewHolderHelper viewHolderHelper, int position, String model) { 98 | viewHolderHelper.setText(R.id.tv, model); 99 | } 100 | }); 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /app/src/main/java/com/hadlink/recyclerviewhelpper/aty/RecyclerViewMulAty.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, lyao. lomoliger@hotmail.com 4 | * 5 | * Part of the code from the open source community, 6 | * thanks stackOverflow & gitHub . 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * 19 | */ 20 | 21 | package com.hadlink.recyclerviewhelpper.aty; 22 | 23 | import android.os.Bundle; 24 | import android.support.v7.app.AppCompatActivity; 25 | import android.support.v7.widget.LinearLayoutManager; 26 | import android.support.v7.widget.RecyclerView; 27 | import android.view.View; 28 | import android.view.ViewGroup; 29 | import android.widget.Toast; 30 | 31 | import com.hadlink.recyclerviewhelpper.R; 32 | import com.hadlink.recyclerviewhelpper.unrelated.DataEngine; 33 | import com.hadlink.recyclerviewhelpper.unrelated.MultiAdapter; 34 | import com.hadlink.rvhelpperlib.adapter.OnRVItemClickListener; 35 | 36 | /** 37 | * @author Created by lyao on 2016/2/1. 38 | * @description 39 | */ 40 | public class RecyclerViewMulAty extends AppCompatActivity { 41 | private RecyclerView rv; 42 | 43 | @Override protected void onCreate(Bundle savedInstanceState) { 44 | super.onCreate(savedInstanceState); 45 | setContentView(R.layout.activity_rv); 46 | rv = (RecyclerView) findViewById(R.id.rv); 47 | 48 | demo2(); 49 | } 50 | 51 | private void demo2() { 52 | 53 | rv.setLayoutManager(new LinearLayoutManager(this)); 54 | final MultiAdapter multiAdapter = new MultiAdapter(rv); 55 | multiAdapter.setOnRVItemClickListener(new OnRVItemClickListener() { 56 | @Override public void onRVItemClick(ViewGroup parent, View itemView, int position) { 57 | int itemViewType = multiAdapter.getItemViewType(position); 58 | Object item = multiAdapter.getItem(position); 59 | switch (itemViewType) { 60 | case MultiAdapter.TYPE_1: 61 | Toast.makeText(parent.getContext(), (String) item, Toast.LENGTH_SHORT).show(); 62 | 63 | break; 64 | case MultiAdapter.TYPE_2: 65 | // Toast.makeText(parent.getContext(), ((FuelBean) item).price + "", Toast.LENGTH_SHORT).show(); 66 | multiAdapter.setSelectItem(position); 67 | break; 68 | } 69 | } 70 | }); 71 | rv.setAdapter(multiAdapter); 72 | multiAdapter.setDatas(DataEngine.S_MOCK3); 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /app/src/main/java/com/hadlink/recyclerviewhelpper/unrelated/DataEngine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, lyao. lomoliger@hotmail.com 4 | * 5 | * Part of the code from the open source community, 6 | * thanks stackOverflow & gitHub . 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * 19 | */ 20 | 21 | package com.hadlink.recyclerviewhelpper.unrelated; 22 | 23 | import java.util.ArrayList; 24 | import java.util.Collections; 25 | import java.util.List; 26 | 27 | /** 28 | * @author Created by lyao on 2016/2/1. 29 | * @description 30 | */ 31 | public class DataEngine { 32 | public static List S_MOCK1 = new ArrayList<>(); 33 | public static List S_MOCK2 = new ArrayList<>(); 34 | public static List S_MOCK3 = new ArrayList<>(); 35 | public static List S_MOCK4 = new ArrayList<>(); 36 | 37 | static { 38 | Collections.addAll(S_MOCK1, 39 | "item1item1item1item1item1item1item1item1item1item1item1item1item1item1item1item1item1item1item1item1item1item1item1item1item1item1item1item1item1item1item1item1item1item1item1item1item1item1item1item1item1item1item1item1item1item1item1item1item1", 40 | "item2", 41 | "item3", 42 | "item4", 43 | "WRAP-CONTENT"); 44 | Collections.addAll(S_MOCK2, 45 | "RecyclerView", 46 | "RecyclerView", 47 | "RecyclerView", 48 | "Height", 49 | "WRAP-CONTENT"); 50 | 51 | S_MOCK4.add(new FuelBean("1000", 96, "1000加油卡")); 52 | S_MOCK4.add(new FuelBean("100", 94, "100加油卡")); 53 | S_MOCK4.add(new FuelBean("10000", 98, "10000加油卡")); 54 | S_MOCK4.add(new FuelBean("500", 93, "500加油卡")); 55 | S_MOCK4.add(new FuelBean("500", 93, "500加油卡")); 56 | S_MOCK4.add(new FuelBean("500", 93, "500加油卡")); 57 | S_MOCK4.add(new FuelBean("500", 93, "500加油卡")); 58 | 59 | S_MOCK3.add(new FuelBean("1000", 96, "1000加油卡")); 60 | S_MOCK3.add("1000加油卡"); 61 | S_MOCK3.add(new FuelBean("100", 94, "100加油卡")); 62 | S_MOCK3.add("100加油卡"); 63 | S_MOCK3.add(new FuelBean("10000", 98, "10000加油卡")); 64 | S_MOCK3.add("10000加油卡"); 65 | S_MOCK3.add(new FuelBean("500", 93, "500加油卡")); 66 | S_MOCK3.add("500加油卡"); 67 | S_MOCK3.add(new FuelBean("500", 93, "500加油卡")); 68 | S_MOCK3.add("500加油卡"); 69 | S_MOCK3.add(new FuelBean("500", 93, "500加油卡")); 70 | S_MOCK3.add("500加油卡"); 71 | S_MOCK3.add(new FuelBean("500", 93, "500加油卡")); 72 | S_MOCK3.add("500加油卡"); 73 | S_MOCK3.add(new FuelBean("500", 93, "500加油卡")); 74 | S_MOCK3.add("500加油卡"); 75 | S_MOCK3.add(new FuelBean("500", 93, "500加油卡")); 76 | S_MOCK3.add("500加油卡"); 77 | S_MOCK3.add(new FuelBean("500", 93, "500加油卡")); 78 | S_MOCK3.add("500加油卡"); 79 | S_MOCK3.add(new FuelBean("500", 93, "500加油卡")); 80 | S_MOCK3.add("500加油卡"); 81 | S_MOCK3.add(new FuelBean("500", 93, "500加油卡")); 82 | S_MOCK3.add("500加油卡"); 83 | S_MOCK3.add(new FuelBean("500", 93, "500加油卡")); 84 | S_MOCK3.add("500加油卡"); 85 | S_MOCK3.add(new FuelBean("500", 93, "500加油卡")); 86 | S_MOCK3.add("500加油卡"); 87 | S_MOCK3.add(new FuelBean("500", 93, "500加油卡")); 88 | S_MOCK3.add("500加油卡"); 89 | S_MOCK3.add(new FuelBean("500", 93, "500加油卡")); 90 | S_MOCK3.add("500加油卡"); 91 | S_MOCK3.add(new FuelBean("500", 93, "500加油卡")); 92 | S_MOCK3.add("500加油卡"); 93 | S_MOCK3.add(new FuelBean("500", 93, "500加油卡")); 94 | S_MOCK3.add("500加油卡"); 95 | S_MOCK3.add(new FuelBean("500", 93, "500加油卡")); 96 | S_MOCK3.add("500加油卡"); 97 | S_MOCK3.add(new FuelBean("500", 93, "500加油卡")); 98 | S_MOCK3.add("500加油卡"); 99 | S_MOCK3.add(new FuelBean("500", 93, "500加油卡")); 100 | S_MOCK3.add("500加油卡"); 101 | S_MOCK3.add(new FuelBean("500", 93, "500加油卡")); 102 | S_MOCK3.add("500加油卡"); 103 | S_MOCK3.add(new FuelBean("500", 93, "500加油卡")); 104 | S_MOCK3.add("500加油卡"); 105 | S_MOCK3.add(new FuelBean("500", 93, "500加油卡")); 106 | S_MOCK3.add("500加油卡"); 107 | S_MOCK3.add(new FuelBean("500", 93, "500加油卡")); 108 | S_MOCK3.add("500加油卡"); 109 | S_MOCK3.add(new FuelBean("500", 93, "500加油卡")); 110 | S_MOCK3.add("500加油卡"); 111 | S_MOCK3.add(new FuelBean("500", 93, "500加油卡")); 112 | S_MOCK3.add("500加油卡"); 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /app/src/main/java/com/hadlink/recyclerviewhelpper/unrelated/FuelAdapter.java: -------------------------------------------------------------------------------- 1 | package com.hadlink.recyclerviewhelpper.unrelated; 2 | 3 | import android.support.v7.widget.RecyclerView; 4 | 5 | import com.hadlink.recyclerviewhelpper.R; 6 | import com.hadlink.rvhelpperlib.adapter.RecyclerViewAdapter; 7 | import com.hadlink.rvhelpperlib.adapter.ViewHolderHelper; 8 | 9 | import java.util.List; 10 | 11 | /** 12 | * @author Created by lyao on 2016/1/18. 13 | * @update 14 | * @description 15 | */ 16 | public class FuelAdapter extends RecyclerViewAdapter { 17 | public FuelAdapter(RecyclerView recyclerView, List list) { 18 | super(recyclerView, R.layout.item2, list); 19 | } 20 | 21 | @Override protected void setItemChildListener(ViewHolderHelper viewHolderHelper) { 22 | super.setItemChildListener(viewHolderHelper); 23 | viewHolderHelper.setItemChildClickListener(R.id.faveVal); 24 | viewHolderHelper.setItemChildClickListener(R.id.priceLayout); 25 | viewHolderHelper.setItemChildClickListener(R.id.labelView); 26 | } 27 | 28 | @Override protected void fillData(ViewHolderHelper viewHolderHelper, int position, FuelBean model) { 29 | LabelView_New labelViewNew = viewHolderHelper.getView(R.id.labelView); 30 | labelViewNew.setText(model.discount + "折"); 31 | viewHolderHelper.setText(R.id.faveVal, model.faceVal + ""); 32 | viewHolderHelper.setText(R.id.price, model.price + ""); 33 | viewHolderHelper.getView(R.id.mainContent).setSelected(model.isSelect); 34 | } 35 | 36 | public void setSelectItem(int position, Runnable r) { 37 | List datas = getDatas(); 38 | FuelBean fuelBean = datas.get(position); 39 | if (!fuelBean.isSelect) { 40 | for (FuelBean f : datas) { 41 | f.isSelect = false; 42 | } 43 | fuelBean.isSelect = true; 44 | notifyDataSetChanged(); 45 | if (r != null) r.run(); 46 | } 47 | } 48 | } -------------------------------------------------------------------------------- /app/src/main/java/com/hadlink/recyclerviewhelpper/unrelated/FuelBean.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, lyao. lomoliger@hotmail.com 4 | * 5 | * Part of the code from the open source community, 6 | * thanks stackOverflow & gitHub . 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * 19 | */ 20 | 21 | package com.hadlink.recyclerviewhelpper.unrelated; 22 | 23 | import java.math.BigDecimal; 24 | 25 | public class FuelBean { 26 | public float faceVal; 27 | public float price; 28 | public int discount; 29 | public boolean isSelect; 30 | public String des;//描述 31 | 32 | public FuelBean(String faceVal, int discount, String des) { 33 | this.faceVal = MathUtil.floatTransfer(new BigDecimal(faceVal).floatValue(), 2); 34 | this.discount = discount; 35 | this.price = MathUtil.floatTransfer(this.faceVal * discount / 100, 2); 36 | this.des = des; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /app/src/main/java/com/hadlink/recyclerviewhelpper/unrelated/LabelView_New.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, lyao. lomoliger@hotmail.com 4 | * 5 | * Part of the code from the open source community, 6 | * thanks stackOverflow & gitHub . 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * 19 | */ 20 | 21 | package com.hadlink.recyclerviewhelpper.unrelated; 22 | 23 | import android.content.Context; 24 | import android.content.res.TypedArray; 25 | import android.graphics.Canvas; 26 | import android.graphics.Color; 27 | import android.graphics.Paint; 28 | import android.graphics.Path; 29 | import android.util.AttributeSet; 30 | import android.view.Gravity; 31 | import android.view.View; 32 | 33 | import com.hadlink.recyclerviewhelpper.R; 34 | 35 | 36 | public class LabelView_New extends View { 37 | private String mTextContent; 38 | private int mTextColor; 39 | private float mTextSize; 40 | private boolean mTextBold; 41 | private boolean mFillTriangle; 42 | private boolean mTextAllCaps; 43 | private int mBackgroundColor; 44 | private float mMinSize; 45 | private float mPadding; 46 | private int mGravity; 47 | private static final int DEFAULT_DEGREES = 45; 48 | 49 | private Paint mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 50 | private Paint mBackgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 51 | private Path mPath = new Path(); 52 | 53 | public LabelView_New(Context context) { 54 | this(context, null); 55 | } 56 | 57 | public LabelView_New(Context context, AttributeSet attrs) { 58 | super(context, attrs); 59 | 60 | obtainAttributes(context, attrs); 61 | mTextPaint.setTextAlign(Paint.Align.CENTER); 62 | } 63 | 64 | private void obtainAttributes(Context context, AttributeSet attrs) { 65 | TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.LabelView); 66 | mTextContent = ta.getString(R.styleable.LabelView_lv_text); 67 | mTextColor = ta.getColor(R.styleable.LabelView_lv_text_color, Color.parseColor("#ffffff")); 68 | mTextSize = ta.getDimension(R.styleable.LabelView_lv_text_size, sp2px(11)); 69 | mTextBold = ta.getBoolean(R.styleable.LabelView_lv_text_bold, true); 70 | mTextAllCaps = ta.getBoolean(R.styleable.LabelView_lv_text_all_caps, true); 71 | mFillTriangle = ta.getBoolean(R.styleable.LabelView_lv_fill_triangle, false); 72 | mBackgroundColor = ta.getColor(R.styleable.LabelView_lv_background_color, Color.parseColor("#FF4081")); 73 | mMinSize = ta.getDimension(R.styleable.LabelView_lv_min_size, mFillTriangle ? dp2px(35) : dp2px(50)); 74 | mPadding = ta.getDimension(R.styleable.LabelView_lv_padding, dp2px(3.5f)); 75 | mGravity = ta.getInt(R.styleable.LabelView_lv_gravity, Gravity.TOP | Gravity.LEFT); 76 | ta.recycle(); 77 | } 78 | 79 | public void setTextColor(int textColor) { 80 | mTextColor = textColor; 81 | invalidate(); 82 | } 83 | 84 | public void setText(String text) { 85 | mTextContent = text; 86 | invalidate(); 87 | } 88 | 89 | public void setTextSize(float textSize) { 90 | mTextSize = sp2px(textSize); 91 | invalidate(); 92 | } 93 | 94 | public void setTextBold(boolean textBold) { 95 | mTextBold = textBold; 96 | invalidate(); 97 | } 98 | 99 | public void setFillTriangle(boolean fillTriangle) { 100 | mFillTriangle = fillTriangle; 101 | invalidate(); 102 | } 103 | 104 | public void setTextAllCaps(boolean textAllCaps) { 105 | mTextAllCaps = textAllCaps; 106 | invalidate(); 107 | } 108 | 109 | public void setBgColor(int backgroundColor) { 110 | mBackgroundColor = backgroundColor; 111 | invalidate(); 112 | } 113 | 114 | public void setMinSize(float minSize) { 115 | mMinSize = dp2px(minSize); 116 | invalidate(); 117 | } 118 | 119 | public void setPadding(float padding) { 120 | mPadding = dp2px(padding); 121 | invalidate(); 122 | } 123 | 124 | /** 125 | * Gravity.TOP | Gravity.LEFT 126 | * Gravity.TOP | Gravity.RIGHT 127 | * Gravity.BOTTOM | Gravity.LEFT 128 | * Gravity.BOTTOM | Gravity.RIGHT 129 | */ 130 | public void setGravity(int gravity) { 131 | mGravity = gravity; 132 | } 133 | 134 | public String getText() { 135 | return mTextContent; 136 | } 137 | 138 | public int getTextColor() { 139 | return mTextColor; 140 | } 141 | 142 | public float getTextSize() { 143 | return mTextSize; 144 | } 145 | 146 | public boolean isTextBold() { 147 | return mTextBold; 148 | } 149 | 150 | public boolean isFillTriangle() { 151 | return mFillTriangle; 152 | } 153 | 154 | public boolean isTextAllCaps() { 155 | return mTextAllCaps; 156 | } 157 | 158 | public int getBgColor() { 159 | return mBackgroundColor; 160 | } 161 | 162 | public float getMinSize() { 163 | return mMinSize; 164 | } 165 | 166 | public float getPadding() { 167 | return mPadding; 168 | } 169 | 170 | public int getGravity() { 171 | return mGravity; 172 | } 173 | 174 | @Override 175 | protected void onDraw(Canvas canvas) { 176 | int size = getHeight(); 177 | 178 | mTextPaint.setColor(mTextColor); 179 | mTextPaint.setTextSize(mTextSize); 180 | mTextPaint.setFakeBoldText(mTextBold); 181 | mBackgroundPaint.setColor(mBackgroundColor); 182 | 183 | float textHeight = mTextPaint.descent() - mTextPaint.ascent(); 184 | if (mFillTriangle) { 185 | if (mGravity == (Gravity.TOP | Gravity.LEFT)) { 186 | mPath.reset(); 187 | mPath.moveTo(0, 0); 188 | mPath.lineTo(0, size); 189 | mPath.lineTo(size, 0); 190 | mPath.close(); 191 | canvas.drawPath(mPath, mBackgroundPaint); 192 | 193 | drawTextWhenFill(size, -DEFAULT_DEGREES, canvas, true); 194 | } else if (mGravity == (Gravity.TOP | Gravity.RIGHT)) { 195 | mPath.reset(); 196 | mPath.moveTo(size, 0); 197 | mPath.lineTo(0, 0); 198 | mPath.lineTo(size, size); 199 | mPath.close(); 200 | canvas.drawPath(mPath, mBackgroundPaint); 201 | 202 | drawTextWhenFill(size, DEFAULT_DEGREES, canvas, true); 203 | } else if (mGravity == (Gravity.BOTTOM | Gravity.LEFT)) { 204 | mPath.reset(); 205 | mPath.moveTo(0, size); 206 | mPath.lineTo(0, 0); 207 | mPath.lineTo(size, size); 208 | mPath.close(); 209 | canvas.drawPath(mPath, mBackgroundPaint); 210 | 211 | drawTextWhenFill(size, DEFAULT_DEGREES, canvas, false); 212 | } else if (mGravity == (Gravity.BOTTOM | Gravity.RIGHT)) { 213 | mPath.reset(); 214 | mPath.moveTo(size, size); 215 | mPath.lineTo(0, size); 216 | mPath.lineTo(size, 0); 217 | mPath.close(); 218 | canvas.drawPath(mPath, mBackgroundPaint); 219 | 220 | drawTextWhenFill(size, -DEFAULT_DEGREES, canvas, false); 221 | } 222 | } else { 223 | double delta = (textHeight + mPadding * 2) * Math.sqrt(2); 224 | if (mGravity == (Gravity.TOP | Gravity.LEFT)) { 225 | mPath.reset(); 226 | mPath.moveTo(0, (float) (size - delta)); 227 | mPath.lineTo(0, size); 228 | mPath.lineTo(size, 0); 229 | mPath.lineTo((float) (size - delta), 0); 230 | mPath.close(); 231 | canvas.drawPath(mPath, mBackgroundPaint); 232 | 233 | drawText(size, -DEFAULT_DEGREES, canvas, textHeight, true); 234 | } else if (mGravity == (Gravity.TOP | Gravity.RIGHT)) { 235 | mPath.reset(); 236 | mPath.moveTo(0, 0); 237 | mPath.lineTo((float) delta, 0); 238 | mPath.lineTo(size, (float) (size - delta)); 239 | mPath.lineTo(size, size); 240 | mPath.close(); 241 | canvas.drawPath(mPath, mBackgroundPaint); 242 | 243 | drawText(size, DEFAULT_DEGREES, canvas, textHeight, true); 244 | } else if (mGravity == (Gravity.BOTTOM | Gravity.LEFT)) { 245 | mPath.reset(); 246 | mPath.moveTo(0, 0); 247 | mPath.lineTo(0, (float) delta); 248 | mPath.lineTo((float) (size - delta), size); 249 | mPath.lineTo(size, size); 250 | mPath.close(); 251 | canvas.drawPath(mPath, mBackgroundPaint); 252 | 253 | drawText(size, DEFAULT_DEGREES, canvas, textHeight, false); 254 | } else if (mGravity == (Gravity.BOTTOM | Gravity.RIGHT)) { 255 | mPath.reset(); 256 | mPath.moveTo(0, size); 257 | mPath.lineTo((float) delta, size); 258 | mPath.lineTo(size, (float) delta); 259 | mPath.lineTo(size, 0); 260 | mPath.close(); 261 | canvas.drawPath(mPath, mBackgroundPaint); 262 | 263 | drawText(size, -DEFAULT_DEGREES, canvas, textHeight, false); 264 | } 265 | } 266 | } 267 | 268 | private void drawText(int size, float degrees, Canvas canvas, float textHeight, boolean isTop) { 269 | canvas.save(); 270 | canvas.rotate(degrees, size / 2f, size / 2f); 271 | float delta = isTop ? -(textHeight + mPadding * 2) / 2 : (textHeight + mPadding * 2) / 2; 272 | float textBaseY = size / 2 - (mTextPaint.descent() + mTextPaint.ascent()) / 2 + delta; 273 | canvas.drawText(mTextAllCaps ? mTextContent.toUpperCase() : mTextContent, 274 | getPaddingLeft() + (size - getPaddingLeft() - getPaddingRight()) / 2, textBaseY, mTextPaint); 275 | canvas.restore(); 276 | } 277 | 278 | private void drawTextWhenFill(int size, float degrees, Canvas canvas, boolean isTop) { 279 | canvas.save(); 280 | canvas.rotate(degrees, size / 2f, size / 2f); 281 | float delta = isTop ? -size / 4 : size / 4; 282 | float textBaseY = size / 2 - (mTextPaint.descent() + mTextPaint.ascent()) / 2 + delta; 283 | canvas.drawText(mTextAllCaps ? mTextContent.toUpperCase() : mTextContent, 284 | getPaddingLeft() + (size - getPaddingLeft() - getPaddingRight()) / 2, textBaseY, mTextPaint); 285 | canvas.restore(); 286 | } 287 | 288 | @Override 289 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 290 | int measuredWidth = measureWidth(widthMeasureSpec); 291 | setMeasuredDimension(measuredWidth, measuredWidth); 292 | } 293 | 294 | /** 确定View宽度大小 */ 295 | private int measureWidth(int widthMeasureSpec) { 296 | int result; 297 | int specMode = MeasureSpec.getMode(widthMeasureSpec); 298 | int specSize = MeasureSpec.getSize(widthMeasureSpec); 299 | if (specMode == MeasureSpec.EXACTLY) {//大小确定直接使用 300 | result = specSize; 301 | } else { 302 | int padding = getPaddingLeft() + getPaddingRight(); 303 | mTextPaint.setColor(mTextColor); 304 | mTextPaint.setTextSize(mTextSize); 305 | float textWidth = mTextPaint.measureText(mTextContent + ""); 306 | result = (int) ((padding + (int) textWidth) * Math.sqrt(2)); 307 | //如果父视图的测量要求为AT_MOST,即限定了一个最大值,则再从系统建议值和自己计算值中去一个较小值 308 | if (specMode == MeasureSpec.AT_MOST) { 309 | result = Math.min(result, specSize); 310 | } 311 | 312 | result = Math.max((int) mMinSize, result); 313 | } 314 | 315 | return result; 316 | } 317 | 318 | protected int dp2px(float dp) { 319 | final float scale = getResources().getDisplayMetrics().density; 320 | return (int) (dp * scale + 0.5f); 321 | } 322 | 323 | protected int sp2px(float sp) { 324 | final float scale = getResources().getDisplayMetrics().scaledDensity; 325 | return (int) (sp * scale + 0.5f); 326 | } 327 | } -------------------------------------------------------------------------------- /app/src/main/java/com/hadlink/recyclerviewhelpper/unrelated/MathUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, lyao. lomoliger@hotmail.com 4 | * 5 | * Part of the code from the open source community, 6 | * thanks stackOverflow & gitHub . 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * 19 | */ 20 | package com.hadlink.recyclerviewhelpper.unrelated; 21 | 22 | import java.math.BigDecimal; 23 | import java.util.regex.Matcher; 24 | import java.util.regex.Pattern; 25 | 26 | public class MathUtil { 27 | 28 | /** 29 | * 检查整数 30 | * 31 | * @param num 32 | * @param type "0+":非负整数 "+":正整数 "-0":非正整数 "-":负整数 "":整数 33 | * @return 34 | */ 35 | public static boolean checkNumber(String num, String type) { 36 | String eL = ""; 37 | if (type.equals("0+")) 38 | eL = "^\\d+$";// 非负整数 39 | else if (type.equals("+")) 40 | eL = "^\\d*[1-9]\\d*$";// 正整数 41 | else if (type.equals("-0")) 42 | eL = "^((-\\d+)|(0+))$";// 非正整数 43 | else if (type.equals("-")) 44 | eL = "^-\\d*[1-9]\\d*$";// 负整数 45 | else 46 | eL = "^-?\\d+$";// 整数 47 | Pattern p = Pattern.compile(eL); 48 | Matcher m = p.matcher(num); 49 | boolean b = m.matches(); 50 | return b; 51 | } 52 | 53 | /** 54 | * 将一个浮点型保留一定位数 55 | * 56 | * @param source 源 57 | * @param number 保留位数 58 | * @return 59 | */ 60 | public static float floatTransfer(Float source, int number) { 61 | float result = 0f; 62 | Integer numberTmp = (int) Math.pow(10, number); 63 | result = (float) (Math.round(source * numberTmp)) / numberTmp; 64 | return result; 65 | } 66 | 67 | /** 68 | * 将一个double型保留一定位数 69 | * 70 | * @param source 源 71 | * @param number 保留位数 72 | * @return 73 | */ 74 | public static double doubleTransfer(double source, int number) { 75 | double result = 0f; 76 | Integer numberTmp = (int) Math.pow(10, number); 77 | result = (double) (Math.round(source * numberTmp)) / numberTmp; 78 | return result; 79 | } 80 | 81 | public static float formatFloat(Float source) { 82 | return floatTransfer(source, 2); 83 | } 84 | 85 | public static double formatDouble(Double source) { 86 | return doubleTransfer(source, 2); 87 | 88 | } 89 | 90 | public static void main(String[] args) throws Exception { 91 | float a = 12.01f; 92 | float b = 12.007f; 93 | float e = 0.001f; 94 | 95 | System.out.println(a + b); 96 | addFloat(a, b); 97 | 98 | System.out.println(a - b); 99 | subFloat(a, b); 100 | 101 | System.out.println(a * b); 102 | multiFloat(a, b); 103 | 104 | System.out.println(addFloat(a, b, e)); 105 | System.out.println(subFloat(a, b, e)); 106 | 107 | double c = 12.01; 108 | double d = 0.005; 109 | System.out.println(addDouble(c, d)); 110 | } 111 | 112 | public static float addFloat(float a, float b) { 113 | BigDecimal b1 = new BigDecimal(Float.toString(a)); 114 | BigDecimal b2 = new BigDecimal(Float.toString(b)); 115 | Float result = b1.add(b2).floatValue(); 116 | return result; 117 | } 118 | 119 | public static float subFloat(float a, float b) { 120 | BigDecimal b1 = new BigDecimal(Float.toString(a)); 121 | BigDecimal b2 = new BigDecimal(Float.toString(b)); 122 | Float result = b1.subtract(b2).floatValue(); 123 | return result; 124 | } 125 | 126 | public static float multiFloat(float a, float b) { 127 | BigDecimal b1 = new BigDecimal(Float.toString(a)); 128 | BigDecimal b2 = new BigDecimal(Float.toString(b)); 129 | Float result = b1.multiply(b2).floatValue(); 130 | return result; 131 | } 132 | 133 | public static float divFloat(float a, float b) { 134 | BigDecimal b1 = new BigDecimal(Float.toString(a)); 135 | BigDecimal b2 = new BigDecimal(Float.toString(b)); 136 | Float result = b1.divide(b2).floatValue(); 137 | return result; 138 | } 139 | 140 | public static float addFloat(float a, float b, float c) { 141 | 142 | BigDecimal b1 = new BigDecimal(Float.toString(a)); 143 | BigDecimal b2 = new BigDecimal(Float.toString(b)); 144 | BigDecimal b3 = new BigDecimal(Float.toString(c)); 145 | Float result = b1.add(b2).add(b3).floatValue(); 146 | return result; 147 | } 148 | 149 | public static float subFloat(float a, float b, float c) { 150 | BigDecimal b1 = new BigDecimal(Float.toString(a)); 151 | BigDecimal b2 = new BigDecimal(Float.toString(b)); 152 | BigDecimal b3 = new BigDecimal(Float.toString(c)); 153 | Float result = b1.subtract(b2).subtract(b3).floatValue(); 154 | return result; 155 | } 156 | 157 | public static double addDouble(double a, double b) { 158 | BigDecimal b1 = new BigDecimal(Double.toString(a)); 159 | BigDecimal b2 = new BigDecimal(Double.toString(b)); 160 | Double result = b1.add(b2).doubleValue(); 161 | return result; 162 | } 163 | 164 | public static double subDouble(double a, double b) { 165 | BigDecimal b1 = new BigDecimal(Double.toString(a)); 166 | BigDecimal b2 = new BigDecimal(Double.toString(b)); 167 | Double result = b1.subtract(b2).doubleValue(); 168 | return result; 169 | } 170 | 171 | public static double multiDouble(double a, double b) { 172 | BigDecimal b1 = new BigDecimal(Double.toString(a)); 173 | BigDecimal b2 = new BigDecimal(Double.toString(b)); 174 | Double result = b1.multiply(b2).doubleValue(); 175 | return result; 176 | } 177 | 178 | } 179 | -------------------------------------------------------------------------------- /app/src/main/java/com/hadlink/recyclerviewhelpper/unrelated/MultiAdapter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Copyright (c) 2016, lyao. lomoliger@hotmail.com 4 | * 5 | * Part of the code from the open source community, 6 | * thanks stackOverflow & gitHub . 7 | * Licensed under the Apache License, Version 2.0 (the "License"); 8 | * you may not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * 19 | */ 20 | 21 | package com.hadlink.recyclerviewhelpper.unrelated; 22 | 23 | import android.support.v7.widget.RecyclerView; 24 | 25 | import com.hadlink.recyclerviewhelpper.R; 26 | import com.hadlink.rvhelpperlib.adapter.RecyclerViewAdapter; 27 | import com.hadlink.rvhelpperlib.adapter.ViewHolderHelper; 28 | 29 | /** 30 | * @author Created by lyao on 2016/1/18. 31 | * @update 32 | * @description 33 | */ 34 | public class MultiAdapter extends RecyclerViewAdapter { 35 | public final static int TYPE_1 = 1; 36 | public final static int TYPE_2 = 2; 37 | 38 | public MultiAdapter(RecyclerView recyclerView) { 39 | super(recyclerView, R.layout.item2); 40 | } 41 | 42 | @Override protected int getItemViewResId(int viewType) { 43 | switch (viewType) { 44 | case TYPE_1: 45 | return R.layout.item1; 46 | case TYPE_2: 47 | return R.layout.item2; 48 | } 49 | return super.getItemViewResId(viewType); 50 | } 51 | 52 | @Override public int getItemViewType(int position) { 53 | 54 | return (getItem(position) instanceof String) ? TYPE_1 : TYPE_2; 55 | } 56 | 57 | @Override protected void fillData(ViewHolderHelper viewHolderHelper, int position, Object model) { 58 | switch (getItemViewType(position)) { 59 | case TYPE_1: 60 | String s = (String) model; 61 | viewHolderHelper.setText(R.id.tv, s); 62 | break; 63 | case TYPE_2: 64 | FuelBean fuelBean = (FuelBean) model; 65 | LabelView_New labelViewNew = viewHolderHelper.getView(R.id.labelView); 66 | labelViewNew.setText(fuelBean.discount + "折"); 67 | viewHolderHelper.setText(R.id.faveVal, fuelBean.faceVal + ""); 68 | viewHolderHelper.setText(R.id.price, fuelBean.price + ""); 69 | viewHolderHelper.getView(R.id.mainContent).setSelected(fuelBean.isSelect); 70 | break; 71 | } 72 | 73 | } 74 | public void setSelectItem(int position) { 75 | Object item = getItem(position); 76 | if(item instanceof FuelBean){ 77 | FuelBean fuelBean =(FuelBean)item; 78 | fuelBean.isSelect = !fuelBean.isSelect; 79 | notifyItemChanged(position); 80 | } 81 | } 82 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable/selector_fuel_item.xml: -------------------------------------------------------------------------------- 1 | 2 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/selector_text_fuel_item_b.xml: -------------------------------------------------------------------------------- 1 | 2 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/selector_text_fuel_item_n.xml: -------------------------------------------------------------------------------- 1 | 2 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_gv.xml: -------------------------------------------------------------------------------- 1 | 2 | 21 | 22 | 30 | 31 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_lv.xml: -------------------------------------------------------------------------------- 1 | 2 | 21 | 22 | 30 | 31 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 21 | 22 | 30 | 31 | 35 | 36 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_rv.xml: -------------------------------------------------------------------------------- 1 | 2 | 21 | 22 | 29 | 30 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /app/src/main/res/layout/content_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 21 | 22 | 33 | 34 |