[Android interface implementation] ZListView, the most powerful ListView Control for refreshing, loading, and sliding deletion (I), androidzlistview
Reprinted please indicate the source: http://blog.csdn.net/zhaokaiqiang1992
Two months ago, I wrote an article [Android interface implementation] integrating the XListview function of refreshing, loading more, and sliding deletion, and introduced how to integrate two open-source projects to implement this function, however, some problems occur in actual use. For example, the item click event is called multiple times, the position of the item is inaccurate, and the amount of code is too large and too complicated, A buddy used this project in his work, and these bugs troubled him. Therefore, after three days of understanding the project, I decided to rewrite the project, and then I got this article today, we will introduce you to the use and implementation of a more powerful ListView. In order to commemorate the XListView and facilitate the differentiation, I decided to name this control ZListVIew.
In addition, this article involves a lot of content. Please take care of your tea and melon seeds and prepare them in advance ~
This article will be divided into two parts for introduction. The first half is the introduction of ZListView, and the second half is the introduction of implementation principles.
ZListView has the following features:
(1) use exactly the same as XListView
(2) It can be used as a normal ListView for refreshing, loading, or disabling.
(3) supports slide from four directions
(4) added powerful event listening to fully control the trigger process of various events.
(5) fixed various bugs in previous projects and made them more stable.
(6) For ListView, the sliding Item is optimized and deleted, and the code is annotated to make it easier to understand and use.
(7) If any problem occurs, call me at any time. Powerful back-to-aid support
I. Introduction to ZListView
Before you start to introduce the use, first let's see the running effect, because I do not know why, I uploaded gif has become a static map, so please click this URL to see the effect: http://ww4.sinaimg.cn/large/005yyi5Jjw1eo1yrys3a6g30ar0h61kx.gif
The directory structure of the entire project is as follows:
BaseSwipeAdapter is an adapter. If you need to add a sliding effect to your ListView, You need to inherit this class.
The enums package contains some enumerated values, such as sliding mode, sliding direction, and display mode.
The listener package contains listeners. OnSwipeLayoutListener is used to listen to the layout change events of ZSwipeItem, while SwipeListener is used to listen to open, close, startOpen, startClose, update, and other events of ZSwipeItem, but when we use it, we only need to inherit SimpleSwipeListener, and then rewrite the events we care about.
The widget package contains the core class, which starts with ZListView and is the modified ListIVew core class. It is used exactly the same as XListView. ZSwipeItem is the core class of a sliding Item, where we process various sliding operations of items.
The following describes the basic directory structure.
MainActivity is one of our test activities. below is the code
public class MainActivity extends Activity {protected static final String TAG = "MainActivity";private ZListView listView;private Handler handler = new Handler();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);listView = (ZListView) findViewById(R.id.listview);listView.setXListViewListener(new IXListViewListener() {@Overridepublic void onRefresh() {handler.postDelayed(new Runnable() {@Overridepublic void run() {listView.stopRefresh();}}, 1000);}@Overridepublic void onLoadMore() {handler.postDelayed(new Runnable() {@Overridepublic void run() {listView.stopLoadMore();}}, 1000);}});listView.setPullLoadEnable(true);listView.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view,int position, long id) {Toast.makeText(MainActivity.this, "onItemClick=" + position,Toast.LENGTH_SHORT).show();}});listView.setOnItemLongClickListener(new OnItemLongClickListener() {@Overridepublic boolean onItemLongClick(AdapterView<?> parent, View view,int position, long id) {Toast.makeText(MainActivity.this,"onItemLongClick=" + position, Toast.LENGTH_SHORT).show();return true;}});listView.setAdapter(new ListViewAdapter(this));}}
As you can see from the above, it is exactly the same as using XListView. below is the layout file code, which is exactly the same as XListView, that is, you can replace the control name.
<?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:background="#f0f0f0" > <com.socks.zlistview.widget.ZListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent" android:cacheColorHint="@android:color/transparent" /></LinearLayout>
The main difference is that the adapter is used. As mentioned above, if we want to implement the items of the existing slide function, we need to inherit the BaseSwipeAdapter. Therefore, we need to do this here. But before providing the adapter code, we need to first look at the layout file Writing of the Item, because there is a mandatory requirement.
<?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="wrap_content" > <com.socks.zlistview.widget.ZSwipeItem android:id="@+id/swipe_item" android:layout_width="match_parent" android:layout_height="wrap_content" > <LinearLayout android:id="@+id/ll" android:layout_width="180dp" android:layout_height="match_parent" android:background="@android:color/holo_red_light" android:gravity="center" > <ImageView android:layout_width="30dp" android:layout_height="wrap_content" android:src="@drawable/trash" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/item_selector" android:orientation="vertical" android:padding="15dp" > <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@android:color/black" android:textSize="14sp" /> </LinearLayout> </com.socks.zlistview.widget.ZSwipeItem></LinearLayout>
If we need the slide feature, ZSwipeItem must be included in the item. In fact, ZSwipeItem is the inherited FrameLayout, which will be explained in more detail in the Principles phase. In the ZSwipeItem layout, there must be two ViewGroup la S. The first ViewGroup contains the content displayed after sliding, and the second ViewGroup is the layout displayed in front. Remember, the two viewgroups are required! Otherwise, an error is reported!
After writing the item layout file, let's take a look at the Adapter code.
Public class ListViewAdapter extends BaseSwipeAdapter {protected static final String TAG = "ListViewAdapter"; private Activity context; public ListViewAdapter (Activity context) {this. context = context ;}@ Overridepublic int getCount () {return 20 ;}@ Overridepublic Object getItem (int position) {return position ;}@ Overridepublic long getItemId (int position) {return position ;}@ Overridepublic int getSwipeLayoutResourceId (int position) {return R. id. swipe_item;} @ Overridepublic View generateView (int position, ViewGroup parent) {return context. getLayoutInflater (). inflate (R. layout. item_listview, parent, false) ;}@ Overridepublic void fillValues (final int position, View convertView) {final ZSwipeItem swipeItem = (ZSwipeItem) convertView. findViewById (R. id. swipe_item); LinearLayout ll = (LinearLayout) convertView. findViewById (R. id. ll); TextView TV = (TextView) convertView. findViewById (R. id. TV); TV. setText (position + ". "+" test text "); if (position % 4 = 1) {swipeItem. setShowMode (ShowMode. pullOut); swipeItem. setDragEdge (DragEdge. right);} else if (position % 4 = 2) {swipeItem. setShowMode (ShowMode. layDown); swipeItem. setDragEdge (DragEdge. right);} else if (position % 4 = 3) {swipeItem. setShowMode (ShowMode. pullOut); swipeItem. setDragEdge (DragEdge. left);} else if (position % 4 = 0) {swipeItem. setShowMode (ShowMode. layDown); swipeItem. setDragEdge (DragEdge. left);} swipeItem. addSwipeListener (new SimpleSwipeListener () {@ Overridepublic void onOpen (ZSwipeItem layout) {Log. d (TAG, "Open:" + position) ;}@ Overridepublic void onClose (ZSwipeItem layout) {Log. d (TAG, "close:" + position) ;}@ Overridepublic void onStartOpen (ZSwipeItem layout) {Log. d (TAG, "ready to open:" + position) ;}@ Overridepublic void onStartClose (ZSwipeItem layout) {Log. d (TAG, "prepare to close:" + position) ;}@ Overridepublic void onHandRelease (ZSwipeItem layout, float xvel, float yvel) {Log. d (TAG, "gesture release") ;}@ Overridepublic void onUpdate (ZSwipeItem layout, int leftOffset, int topOffset) {Log. d (TAG, "location update") ;}}); ll. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {Toast. makeText (context, "delete:" + position, Toast. LENGTH_SHORT ). show (); swipeItem. close ();}});}}
First, we must inherit the BaseSwipeAdapter. Do not abandon me. After the inheritance, there are several special methods that need to be implemented. The following describes the usage of these methods:
(1) The getSwipeLayoutResourceId (int position) method is used to return the id of ZSwipeItem in the position layout file. The reason why the position is required is to have multiple la S. In most cases, we do not need to care about the position parameter.
(2) generateView (int posiion, ViewGroup parent) in this method, we need to load the layout file resources, just use it like above.
(3) fillVlues (int position, View convertIView) is used to populate the data in our layout with the listener. In this case, you do not need to worry about whether convertView is null, it has been instantiated and can be called as much as possible.
(4) swipeItem. setShowMode () is used to set the display mode. There are two display modes: PullOut and LayDown, which are defined in the ShowMode Enumeration type. PullOut is the launch mode. After slide, the LayOut in front will be pushed out of the screen, and the LayOut in the back will be moved out of the screen, while LayOut will display the LayOut in the back, that is, the master is overwritten.
(5) swipeItem. setDragEdge () is used to set the direction of the side slide action. Right indicates that the side slide from the Right to the left, and the layout will be displayed later. This is also an enumeration type and has four values: top, bottom, and left, it is generally recommended that the left and right sides conflict with the sliding of ListView, and the effect is not good.
(6) Use swipeItem. addSwipeListener () can be used to add event listening to sliding items. We recommend that you use the SimpleSwipeListener Anonymous class. In this way, you can only rewrite the events you are concerned about. onOpen and onClose are called when the events are closed, onStartXX is called at the beginning of the action. Therefore, if you need to change the subsequent layout status, call onHandRelease () at onStartXX () it is called when the user's finger leaves the screen. The layout parameter is the ZSwipeItem object, xvel, and yvel of the event. It is the acceleration in the x and y directions when the gesture is opened instantly. OnUpdate () is always called when sliding. leftOffset and topOffset are the distance from the coordinates in the upper left corner.
(7) after SWipeItem is used, you can add various events to the controls in the subsequent layout as usual. For example, the above Code directly adds click events to the linear layout, click it to close the sliding layout.
The introduction is written here first. Please download the project from github because the implementation principle is a little complicated. Therefore, I would like to introduce it in the next article.
Project address: https://github.com/ZhaoKaiQiang/ZListVIew