With the refactoring of the above two articles, it is very easy to refactor the ListView grouping list into a custom control simply by placing the initialization action inside the constructor of the custom control. Refactoring custom Controls the annotation refactoring for the above article is based on.
Basic structure
Here is a first post on the refactoring of the code structure of the activity, the relevant methods implemented in the previous two articles are posted.
Re-post a reconstructed view structure. Both structures are visible and similar. The difference is that there is an abstract method Getdatalist () on the top of the activity, but the bottom is not, but there is a iloadrulerdata<t> iloaddata type interface. Used to load data.
And the custom view changes the InitData () method in activity to LoadData () to start loading the data after the active call.
After refactoring, the basic code is as follows, omitting part of the code that was previously duplicated:
The first is the data loading interface:
Public interface Iloadrulerdata<t> {public list<t> getdatalist (); }
Then the basic code for the Custom view:
/** * @Description: You need to call Setiloaddata first, set the interface to get the data, and then call the LoadData method */public class Rulerview<t> extends Linearlayou t{private View Baseview; Private TextView Nodataview; Private TextView Rulertag; Private Progressbarwithtext progress; Private ListView ListView; Private Rulerwidget ruler; Private iloadrulerdata<t> Iloaddata; Private list<t> originallist; Public list<rulerlistdatawrapper<t>> dealedlist; Private hashmap<string, integer> taglocation = new hashmap<string, integer> (); Private ruleradapter<t> Ruleradapter; Public Rulerview (Context context) {super (context); Init (context); } public Rulerview (context context, AttributeSet Attrs) {Super (context, attrs); Init (context); } public Rulerview (context context, AttributeSet attrs, int defstyle) {Super (context, attrs); Init (context); }/** * @Description: Set Data Loading interface * @param * @return void */public void Setiloaddata (iloadrulerdata<t> iloaddata) {this.iloaddata = Iloaddata; } private void Init (context context) {Baseview = Layoutinflater.from (context). Inflate (R.layout.g_ruler, NULL); Nodataview = (TextView) Baseview.findviewbyid (r.id.g_base_list_nodata); Rulertag = (TextView) Baseview.findviewbyid (R.id.g_ruler_tag); Progress = (Progressbarwithtext) Baseview.findviewbyid (R.id.g_base_progressbar_withtext); ListView = (ListView) Baseview.findviewbyid (R.id.g_base_list); Ruler = (rulerwidget) Baseview.findviewbyid (R.id.g_ruler); Progress.setvisibility (view.visible); Rulertag.setvisibility (View.gone); Nodataview.setvisibility (View.gone); Listview.setvisibility (View.gone); Ruler.setvisibility (View.gone); AddView (Baseview, layoutparams.match_parent, layoutparams.wrap_content); } public void LoadData () {new Getdataasytask (). Execute (); } 。。。。。 }
Test layout
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" > <textview android:layout_margintop= "10DP" android:layout_width= " Wrap_content " android:layout_height=" wrap_content " android:text=" Wbxtest View " android:textcolor=" @ Color/g_black " /> <******.ruler.view.rulerview android:id=" @+id/test_ruler_view_id " Android:layout_width= "Match_parent" android:layout_height= "wrap_content" /></linearlayout>
Activity
public class Testviewactivity extends activity{ private rulerview<testannotationbo> Rulerview; @Override protected void onCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (R.layout.test_ruler_view); Rulerview = (rulerview<testannotationbo>) Findviewbyid (r.id.test_ruler_view_id); Rulerview.setiloaddata (New iloadrulerdata<testannotationbo> () { @Override public list< Testannotationbo> getdatalist () { return new Testannnodao (). Getbolist (); } ); Rulerview.loaddata (); }}
This series is complete.
Custom controls for Android list grouping and letter navigation