Holder-oriented programming, code refreshing, simpler management, and holder-Oriented Programming

Source: Internet
Author: User

Holder-oriented programming, code refreshing, simpler management, and holder-Oriented Programming

Please refer to this Article without limit: http://blog.csdn.net/wanghao200906/article/details/46819101
Listview is believed to have been used, and ViewHolder has written a lot of times. Every time you use listview, you need to write a bunch of viewholder files, and getview files. This is especially troublesome. In fact, the workload is not large, but it is physical work. Ah

There is another hard nut to crack. If you are a project manager or a special dish, the Code he wrote is written in your activity. When the bug was fixed, he decided not to change it, you can only work overtime and change it by yourself. I have been pitted for countless times. For this reason, I wanted to write a class for a long time, so that all the code division is clear and mutually independent, so as to better manage it.

In order to have time to complete, it must be encapsulated. Many Android gods have encapsulated listview, and the dark horse training institutions have also mentioned viewholder encapsulation. In order to prevent more people from working overtime, more group leaders with color pens are not working overtime on other people's bugs.
Let's talk about the animation effect first.

Let's briefly describe this animation:
1. The above is a HorizontalScrollView that can slide left and right. The network gets the image display.
2. Below is a text display. The first few lines of the text are displayed. Click to slide to the bottom of the text, and then click to slide. This has a rebound effect, it doesn't matter if the gif looks clear.
What is used here is
Property animation,
Listen,
Obtain the height and total height of the first few lines of text.
The rebound effect of the property animation.

My previous experience was to assign it to two younger siblings who wrote them to an activity. As a result, a bug occurs. Of course, I will modify it later. Younger brother or manager cousin, you know.

It seems simple. In fact, the total amount of code is also a small amount of one thousand lines, there is no comment, really want to pass away.

So the force is coming:

Package com. example. holder_design.holder; import android. view. view;/*** @ author wang * @ version Creation Time: 5:15:41, January 1, July 2, 2015 * class description: All interfaces are developed by holder. ** all pages are inseparable: the Network request data and display pages have the same purposes, but each page has its own characteristics. Therefore, let the subclass implement * initialize the default control first, * When the network requests data, write the obtained data to the control. ** in short, all code is written to the same interface because of the complex interface, which makes it difficult to maintain, pit yourself into the trap */public abstract class BaseHolder <T> {private View contentView; // interface private T data; // data type // initialize the space first during initialization, write the controls to the view (similar to the view of listview. setTag (holder) public BaseHolder () {contentView = initView (); contentView. setTag (this) ;}// read data: note that the data here is already obtained after network loading and json parsing. public void setData (T data) {this. data = data; refreshView (data); // display interface} // return the current view to the parent class public View getView () {return contentView ;} /*** initialize various controls ** @ return view */protected abstract View initView ();/*** add various data to the control, time consuming hypes * @ param data2 */protected abstract void refreshView (T data );}

How to use it.
Direct inheritance

Package com. example. holder_design.holder; import android. view. view; import android. widget. imageView; import com. example. holder_design.R; import com.wang.cn. utils. UIUtils;/*** @ author wang * @ version Creation Time: 12:06:48, January 1, July 9, 2015 class description */public class Horizontalscreen extends BaseHolder <Integer []> {private ImageView [] ivs; @ Override public View initView () {View view = UIUtils. inflate (R. layout. screen); ivs = new ImageView [5]; ivs [0] = (ImageView) view. findViewById (R. id. screen_1); ivs [1] = (ImageView) view. findViewById (R. id. screen_2); ivs [2] = (ImageView) view. findViewById (R. id. screen_3); ivs [3] = (ImageView) view. findViewById (R. id. screen_4); ivs [4] = (ImageView) view. findViewById (R. id. screen_5); return view;} @ Override protected void refreshView (Integer [] data) {for (int I = 0; I <5; I ++) {ivs [I]. setVisibility (View. VISIBLE); ivs [I]. setImageDrawable (UIUtils. getDrawable (data [I]) ;}}

Then the point is, you only need to add their sub-spaces to the activity.

Package com. example. holder_design; import android. app. activity; import android. OS. bundle; import android. view. view; import android. widget. frameLayout; import android. widget. horizontalScrollView; import android. widget. relativeLayout; import com. example. holder_design.holder.Horizontalscreen; import com. example. holder_design.holder.TextHolder; import com.wang.cn. utils. UIUtils; import com.wang.cn. utils. viewUtils; Public class MainActivity extends Activity {String url = "http://image.baidu.com/search/detail? Struct & bdtype = 0 & gsm = undefined "; Integer [] pic = {R. drawable. a, R. drawable. b, R. drawable. c, R. drawable. d, R. drawable. e}; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // The textview text drop-down code FrameLayout fl = (FrameLayout) findViewById (R. id. detail_des); TextHolder textHolder = new TextHolder (); textHolder. setData (url); fl. addView (textHolder. getView (); // The image sliding code HorizontalScrollView horizontalscrollview = (HorizontalScrollView) findViewById (R. id. detail_screen); Horizontalscreen horizontalholder = new Horizontalscreen (); horizontalholder. setData (pic); horizontalscrollview. addView (horizontalholder. getView ());}}

Is it refreshing. The layout of each interface does not need to be changed in the activity.
Activity is like a photo wall. We only need to keep posting photos to the activity. The activity will not take care of the specific photos.
Another advantage is that there are more nails when there are more photos. He restricted the size and layout of the photo frame, and it was not so easy to pull the nails, but this method can solve this problem well. It will no longer interfere with your modification of your code because the code written by others is too spam.
Everyone is responsible for protecting the eyes

Http://download.csdn.net/detail/wanghao200906/8884391

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.