拼接PullToRefreshListView和SlideExpandableListView的功能


点击打开链接:源码地址:http://download.csdn.net/download/zhzhh7378/7076571

手里面有两个listview的源码SlideExpandableListView和PullToRefreshListView想把两根listview的功能柔和到一块。刚开始想到用一个自定义的类来继承两个listview的功能,发现行不通。

无意间发现SlideExpandableListView包里面有个SlideExpandableListAdapter可以让一个普通的listView的adapter有下拉拓展的功能,其实就是包裹了一下普通的adapter。

先看SlideExpandableListAdapter的构造方法里面有个 SlideExpandableListAdapter(wrapped, toggle_button_id, expandable_view_id);

wrapped就是要包裹的adapter;

toggle_button_id是个按钮或者其他控件id目的是让点击后让listview的item能下拉多出一个视图这个视图的id就是第三个参数expandable_view_id;

expandable_view_id是一个groupView的id里面可以放一些自定义的控件比如两根button。

其他的详细介绍请参考:

https://github.com/tjerkw/Android-SlideExpandableListView
  

首先做的第一步是找到两个listview的第三方类包
这个是slidExpandableListview的类包这个包我直接拷贝进我的项目里面了考完这个包以后会发现SlideExpandableListAdapter这个类报错,需要在项目res下values建一个ids.xml文件代码如下
<?xml version="1.0" encoding="utf-8"?>
<resources>
	<item type="id" name="expandable_toggle_button" />
	<item type="id" name="expandable" />
</resources>

这个包不用修改东西

pulltorefreshlistview包需要修改一个地方

在PullToRefresh的类库的com.handmark.pulltorefresh.library包下,打开PullToRefreshBase.java,在这个类的最后面添加如下代码:

public boolean isHeaderShown() {
		return getHeaderLayout().isShown();
	}

	public boolean isFooterShown() {
		return getFooterLayout().isShown();
	}

修改方法参考

http://blog.csdn.net/losetowin/article/details/18261389

现在开始新建项目来做实现ps两个listview的功能

新建项目以后引入两个类包。

布局文件主界面

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >


    <com.handmark.pulltorefresh.library.PullToRefreshListView
        android:id="@+id/pull_lv"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</RelativeLayout>

然后看activity的代码,有点乱

package com.example.pslistview;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.SimpleFormatter;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.tjerkw.slideexpandable.library.SlideExpandableListAdapter;

public class MainActivity extends Activity implements
		OnRefreshListener<ListView> {
	private List<String> str = new ArrayList<String>();
	private PullToRefreshListView plv;
	private ListView baselv;
	private MyAdapter adapter;
	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case 1:
				plv.onRefreshComplete();
				Toast.makeText(getApplicationContext(), "下拉刷新成功",
						Toast.LENGTH_SHORT).show();
				adapter.notifyDataSetChanged();
				break;
			case 2:
				plv.onRefreshComplete();
				Toast.makeText(getApplicationContext(), "上拉刷新成功",
						Toast.LENGTH_SHORT).show();
				adapter.notifyDataSetChanged();
				break;
			case 3:
				str.remove(msg.arg1);
				Toast.makeText(getApplicationContext(), "删除第" + msg.arg1 + "条",
						Toast.LENGTH_SHORT).show();
				adapter.notifyDataSetChanged();
				break;
			case 4:
				Toast.makeText(getApplicationContext(), "doNothing",
						Toast.LENGTH_SHORT).show();
				adapter.notifyDataSetChanged();
				break;
			}
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		init();
	}

	private void init() {
		str.add("a");
		str.add("b");
		str.add("c");
		str.add("d");
		adapter = new MyAdapter(handler);
		plv = (PullToRefreshListView) findViewById(R.id.pull_lv);
		plv.setMode(Mode.BOTH);
		plv.getLoadingLayoutProxy(false, true);
		plv.setPullLabel("下拉刷新");
		plv.getLoadingLayoutProxy(false, true).setRefreshingLabel("正在加载");
		plv.getLoadingLayoutProxy(false, true).setReleaseLabel("上啦刷新");
		plv.setOnRefreshListener(this);
		baselv = plv.getRefreshableView();
		baselv.setAdapter(new SlideExpandableListAdapter(adapter,
				R.id.expandable_toggle_button, R.id.expandable));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	private class MyAdapter extends BaseAdapter {
		private Handler h;

		public MyAdapter(Handler h) {
			super();
			this.h = h;
		}

		@Override
		public int getCount() {
			return str.size();
		}

		@Override
		public Object getItem(int arg0) {
			return str.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(final int posion, View view, ViewGroup arg2) {
			ViewHolder vh;
			if (view == null) {
				vh = new ViewHolder();
				view = LayoutInflater.from(getApplicationContext()).inflate(
						R.layout.ps_listview_item_layout, null);
				vh.tv = (TextView) view.findViewById(R.id.text);
				vh.a = (Button) view.findViewById(R.id.action_a);
				vh.b = (Button) view.findViewById(R.id.details);
				view.setTag(vh);
			} else {
				vh = (ViewHolder) view.getTag();
			}
			vh.tv.setText("第" + posion + "行" + "----" + str.get(posion));
			vh.a.setText("确定删除");
			vh.b.setText("取消");
			vh.a.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					Message msg = new Message();
					msg.what = 3;
					msg.arg1 = posion;
					h.sendMessage(msg);
				}
			});
			vh.b.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Message msg = new Message();
					msg.what = 4;
					msg.arg1 = posion;
					h.sendMessage(msg);
				}
			});
			return view;
		}

	}

	private class ViewHolder {
		TextView tv;
		Button a, b;
	}
         /**此s方法的用来响应上啦下拉的*/
	@Override
	public void onRefresh(PullToRefreshBase<ListView> refreshView) {
		new Thread() {
			@Override
			public void run() {
				try {
					sleep(2000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				if (plv.isHeaderShown()) {
					str.add(0, "新添加的top");
					handler.sendEmptyMessage(1);
				} else if (plv.isFooterShown()) {
					str.add(str.size(), "新添加的bottom");
					handler.sendEmptyMessage(2);
				}
			}

		}.start();

	}
}

注意MyAdapter里面引用的布局文件的格式如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/text"
            android:layout_width="fill_parent"
            android:layout_height="50dip"
            android:gravity="center_vertical"
            android:text="Hello World" />

        <!-- this is the button that will trigger sliding of the expandable view -->

        <Button
            android:id="@+id/expandable_toggle_button"
            android:layout_width="wrap_content"
            android:layout_height="50dip"
            android:layout_alignBottom="@+id/text"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@id/text"
            android:text="More" />
    </RelativeLayout>

    <!-- this is the expandable view that is initially hidden and will slide out when the more button is pressed -->

    <LinearLayout
        android:id="@+id/expandable"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#000000"
        android:orientation="horizontal" >

        <!-- put whatever you want in the expandable view -->

        <Button
            android:id="@+id/action_a"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="0.5"
            android:text="Action A" />

        <Button
            android:id="@+id/details"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="0.5"
            android:text="Action B" />
    </LinearLayout>

</LinearLayout>

最后看看效果图吧:

运行后主界面

点击more下拉


下拉完成多出来一行显示在上面

上啦效果上啦成功添加一条在下面

源码下载地址http://download.csdn.net/download/zhzhh7378/7076571

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值