您的位置:首页 > 其它

简单实现标题栏悬浮状态,使用PullToRefreshListView上拉加载下拉刷新

2016-08-20 10:18 555 查看
title_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="match_parent"

android:orientation="vertical" >

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="评论"

android:textSize="30sp"

android:id="@+id/pinglun1"

android:background="@android:color/background_dark"

android:textColor="@android:color/holo_blue_light"/>

</LinearLayout>

head_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="match_parent"

android:orientation="vertical" >

<ImageView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/af"

android:id="@+id/iv"/>

</LinearLayout>

main_xml:

<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"

>

<com.handmark.pulltorefresh.library.PullToRefreshListView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/pull_list_view" >

</com.handmark.pulltorefresh.library.PullToRefreshListView>

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="评论"

android:textSize="30sp"

android:id="@+id/pinglun1"

android:visibility="gone"

android:background="@android:color/background_dark"

android:textColor="@android:color/holo_blue_light"/>

</RelativeLayout>

适配器

public class MyAdapter extends BaseAdapter{

private Context context;

private ArrayList<String> stringList;

public MyAdapter(Context context,ArrayList<String> stringList) {

super();

this.context = context;

this.stringList = stringList;

}

@Override

public int getCount() {

return stringList.size();

}

@Override

public Object getItem(int position) {

return stringList.get(position);

}

@Override

public long getItemId(int position) {

return position;

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

TextView tv = new TextView(context);

tv.setText(stringList.get(position));

tv.setTextSize(100);

return tv;

}

}

MainActivity

public class MainActivity extends Activity {

private PullToRefreshListView pull;

private View headView;

private View headTab;

private TextView pinglun1;

private View headTabTitle;

private ArrayList<Integer> mList;

private ArrayList<String> stringList;

private MyAdapter adapter;

private int mItemCount = 9;

private Handler handler = new Handler(){

public void handleMessage(android.os.Message msg) {

switch (msg.what) {

case 0:

stringList.clear();

initAdapterData();

adapter.notifyDataSetChanged();

pull.onRefreshComplete();

break;

case 1:

mItemCount=mItemCount+10;

initAdapterData();

adapter.notifyDataSetChanged();

pull.onRefreshComplete();

break;

default:

break;

}

};

};

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

pull = (PullToRefreshListView) findViewById(R.id.pull_list_view);

stringList = new ArrayList<String>();

pinglun1 = (TextView) findViewById(R.id.pinglun1);

// listview的头部

initDetailHead();

// listview的title

initTab();

// 添加listview

initListView();

// 初始化适配器的参数

initAdapterData();

adapter = new MyAdapter(this, stringList);

pull.setAdapter(adapter);

}

// 初始化适配器的参数

private void initAdapterData() {

for (int i = 0; i < mItemCount; i++) {

stringList.add(i + "");

}

}

//异步加载

// private class GetDataTask extends AsyncTask<Void, Void, String> {

//

// @Override

// protected String doInBackground(Void... params) {

// try {

// Thread.sleep(2000);

// } catch (InterruptedException e) {

// }

// return ""+ (mItemCount++);

// }

//

// @Override

// protected void onPostExecute(String result) {

// stringList.add(result);

// adapter.notifyDataSetChanged();

// // Call onRefreshComplete when the list has been refreshed.

// pull.onRefreshComplete();

//

// }

// }

// 添加listview

private void initListView() {

ListView listView = pull.getRefreshableView();

listView.addHeaderView(headView);

listView.addHeaderView(headTabTitle);

// 两端刷新的方法

pull.setMode(Mode.BOTH);

//实现下拉刷新,上拉加载

initRefresh();

// 滚动监听

pull.setOnScrollListener(new OnScrollListener() {

@Override

public void onScrollStateChanged(AbsListView view, int scrollState) {

}

@Override

public void onScroll(AbsListView view, int firstVisibleItem,

int visibleItemCount, int totalItemCount) {

pinglun1.setVisibility(firstVisibleItem >= 2 ? View.VISIBLE

: View.GONE);

}

});

// //上拉加载

// pull.setOnRefreshListener(new OnRefreshListener<ListView>() {

//

// @Override

// public void onRefresh(PullToRefreshBase<ListView> refreshView) {

// // TODO Auto-generated method stub

// stringList.clear();

// initAdapterData();

// adapter.notifyDataSetChanged();

// }

// });

//

// //滑动到底部最后一个item监听

// pull.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {

//

// @Override

// public void onLastItemVisible() {

// initAdapterData();

// adapter.notifyDataSetChanged();

// }

// });

}

private void initRefresh() {

new Thread(){

public void run() {

pull.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {

// 下拉刷新

@Override

public void onPullDownToRefresh(PullToRefreshBase refreshView) {

// TODO Auto-generated method stub

//异步加载

//new GetDataTask().execute();

try {

Thread.sleep(2000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

handler.sendEmptyMessage(0);

}

// 上拉加载

@Override

public void onPullUpToRefresh(PullToRefreshBase refreshView) {

// TODO Auto-generated method stub

//异步加载

//new GetDataTask().execute();

try {

Thread.sleep(2000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

handler.sendEmptyMessage(1);

}

});

};

}.start();

}

// listview的title

private void initTab() {

headTabTitle = View.inflate(this, R.layout.title, null);

}

// listview的头部

public void initDetailHead() {

headView = View.inflate(this, R.layout.head, null);

ImageView iv = (ImageView) headView.findViewById(R.id.iv);

iv.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

Toast.makeText(MainActivity.this, "点击了图片", 0).show();

}

});

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: