Integrates the xlistview function of refreshing, loading more, and sliding deletion.

Source: Internet
Author: User

Reprint please explain the source: http://blog.csdn.net/bz419927089


If you want to do a listview with refresh and load more functions, you can use xlistview. (Maintenance has been stopped, GitHub address: https://github.com/Maxwin-z/XListView-Android)

To create a listview with the sliding deletion function, we can use swipelistview. (GitHub: https://github.com/47deg/android-swipelistview)

What if you want to refresh, load more, and slide the deleted listview? Sorry, I have not found listview with these features at the same time. However, we can integrate multiple open-source projects to achieve this effect.

To implement this function, we need the cooperation of two open-source projects: xlistview to implement the refresh and load functions, while sliding and delete functions, we need another great open-source project, androidswipelayout (GitHub: https://github.com/daimajia/AndroidSwipeLayout ).

Let's take a look at the effect.



I think the results are very good. For general development requirements, it is enough. Next, let's take a look at the Project integration process.

The following is the structure of the entire project.



Daimajia. Swipe is the androidswipelayout code, and me. maxwin. view is the xlistview code. You do not need to import other lib packages.

The xlistview_footer and xlistview_header under the layout folder are layout files required by the xlistview project. The attrs under the values folder is

Swipelayout Some preset parameters. colors is the two color values used, and strings is the string constant used by xlistview.

After introducing some important folders, let's take a look at the code implementation of listviewadapter.

import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.TextView;import android.widget.Toast;import com.daimajia.swipe.SimpleSwipeListener;import com.daimajia.swipe.SwipeLayout;import com.daimajia.swipe.adapters.BaseSwipeAdapter;/** *  * @author zhaokaiqiang *  */public class ListViewAdapter extends BaseSwipeAdapter {// 上下文对象private Context mContext;// 构造函数public ListViewAdapter(Context mContext) {this.mContext = mContext;}// SwipeLayout的布局id@Overridepublic int getSwipeLayoutResourceId(int position) {return R.id.swipe;}@Overridepublic View generateView(int position, ViewGroup parent) {View v = LayoutInflater.from(mContext).inflate(R.layout.listview_item,parent, false);final SwipeLayout swipeLayout = (SwipeLayout) v.findViewById(getSwipeLayoutResourceId(position));// 当隐藏的删除menu被打开的时候的回调函数swipeLayout.addSwipeListener(new SimpleSwipeListener() {@Overridepublic void onOpen(SwipeLayout layout) {Toast.makeText(mContext, "Open", Toast.LENGTH_SHORT).show();}});// 双击的回调函数swipeLayout.setOnDoubleClickListener(new SwipeLayout.DoubleClickListener() {@Overridepublic void onDoubleClick(SwipeLayout layout,boolean surface) {Toast.makeText(mContext, "DoubleClick",Toast.LENGTH_SHORT).show();}});// 添加删除布局的点击事件v.findViewById(R.id.ll_menu).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {Toast.makeText(mContext, "delete", Toast.LENGTH_SHORT).show();//点击完成之后,关闭删除menuswipeLayout.close();}});return v;}//对控件的填值操作独立出来了,我们可以在这个方法里面进行item的数据赋值@Overridepublic void fillValues(int position, View convertView) {TextView t = (TextView) convertView.findViewById(R.id.position);t.setText((position + 1) + "."<p class="p1"><span class="s1"> + </span>"我就是一行很长很长很长很长很长很长很长很长很长很长很长很长很长的测试文本"</p>);}@Overridepublic int getCount() {return 20;}@Overridepublic Object getItem(int position) {return position;}@Overridepublic long getItemId(int position) {return position;}}

If we want to use listvew with a sliding layout, We need to inherit the baseswipeadapter and implement the important methods mentioned in the code above.

In addition to the adapter, we also need to modify the layout file of the listview item.

The following is the code implementation of listview_item.xml.

<?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="80dp" >    <com.daimajia.swipe.SwipeLayout        android:id="@+id/swipe"        android:layout_width="match_parent"        android:layout_height="match_parent" >        <LinearLayout            android:id="@+id/ll_menu"            android:layout_width="100dp"            android:layout_height="match_parent"            android:background="@android:color/holo_red_light"            android:gravity="center" >            <ImageView                android:id="@+id/trash"                android:layout_width="25dp"                android:layout_height="25dp"                android:src="@drawable/trash" />            <TextView                android:id="@+id/delete"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="删除"                android:textColor="#ffffff"                android:textSize="15sp" />        </LinearLayout>        <LinearLayout            android:layout_width="match_parent"            android:layout_height="match_parent"            android:background="@drawable/item_selector"            android:padding="6dp" >            <TextView                android:id="@+id/position"                android:layout_width="wrap_content"                android:layout_height="wrap_content" />        </LinearLayout>    </com.daimajia.swipe.SwipeLayout></LinearLayout>

The layout of the entire item needs to be wrapped in swipelayout, and then an ID is added. We need to use this ID in the adapter

The return value of getswipelayoutresourceid.

After setting the adapter, we can add an adapter for our xlistview in mainactivity. The following is a simple example.

package com.example.swiperefreshloadlistview;import me.maxwin.view.XListView;import me.maxwin.view.XListView.IXListViewListener;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.widget.Toast;/** *  * @author zhaokaiqiang *  */public class MainActivity extends Activity {private XListView mListView;// 只是用来模拟异步获取数据private Handler handler;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);handler = new Handler();mListView = (XListView) findViewById(R.id.xListView);// 设置xlistview可以加载、刷新mListView.setPullLoadEnable(true);mListView.setPullRefreshEnable(true);// 设置回调函数mListView.setXListViewListener(new IXListViewListener() {@Overridepublic void onRefresh() {// 模拟刷新数据,1s之后停止刷新handler.postDelayed(new Runnable() {@Overridepublic void run() {mListView.stopRefresh();Toast.makeText(MainActivity.this, "refresh",Toast.LENGTH_SHORT).show();}}, 1000);}@Overridepublic void onLoadMore() {handler.postDelayed(new Runnable() {// 模拟加载数据,1s之后停止加载@Overridepublic void run() {mListView.stopLoadMore();Toast.makeText(MainActivity.this, "loadMore",Toast.LENGTH_SHORT).show();}}, 1000);}});// 设置适配器mListView.setAdapter(new ListViewAdapter(this));}}

Okay. After that, we have implemented the multi-function lixtview for refreshing, loading, and sliding deletion.

The source code of the project can be downloaded from my GitHub.

Https://github.com/ZhaoKaiQiang/SwipeRefreshLoadListview

Integrates the xlistview function of refreshing, loading more, and sliding deletion.

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.