欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 仿今日头条简单的刷新效果实例代码

程序员文章站 2024-03-05 15:27:07
点击按钮,先自动进行下拉刷新,也可以手动刷新,刷新完后,最后就多一行数据。有四个选项卡。 前两天导师要求做一个给本科学生预定机房座位的app,出发点来自这里。做着做着...

点击按钮,先自动进行下拉刷新,也可以手动刷新,刷新完后,最后就多一行数据。有四个选项卡。

Android 仿今日头条简单的刷新效果实例代码

前两天导师要求做一个给本科学生预定机房座位的app,出发点来自这里。做着做着遇到很多问题,都解决了。这个效果感觉还不错,整理一下。

mainactivity

package com.example.fragmentmytest;
import android.content.dialoginterface;
import android.graphics.color;
import android.os.bundle;
import android.support.v4.app.fragmentactivity;
import android.support.v4.app.fragmentmanager;
import android.support.v4.app.fragmenttransaction;
import android.text.textutils;
import android.view.menu;
import android.view.menuitem;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.button;
import android.widget.textview;
import com.example.dialog.customdialog;
import com.example.dialog.customdialogchangepwd;
import com.example.dialog.customdialogsignup;
import com.example.myapplication.customapplication;
import com.example.utils.toastutils;
public class mainactivity extends fragmentactivity {
public static final string serveraddress = "http://192.168.1.101";
public static final string serverport = "8080";
onefragment onefragment;
twofragment twofragment;
threefragment threefragment;
fourfragment fourfragment;
button btn1, btn2, btn3, btn4;
onclicklistener clicklistener;
textview stu_msg;
private customapplication app;
/**
* 用于对fragment进行管理
*/
fragmentmanager fragementmanager;
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
app = (customapplication) getapplication(); // 获得customapplication对象
// 必须继承fragmentactivity才能用getsupportfragmentmanager();最好使用v4.app,已经没怎么有人使用app中的了
fragementmanager = getsupportfragmentmanager();
init();
// 第一次启动时选中第0个tab
settabselection(0);// 不能左右滑动的默认值
}
public void init() {
stu_msg = (textview) findviewbyid(r.id.stu_msg);
btn1 = (button) findviewbyid(r.id.btn1);
btn2 = (button) findviewbyid(r.id.btn2);
btn3 = (button) findviewbyid(r.id.btn3);
btn4 = (button) findviewbyid(r.id.btn4);
clicklistener = new onclicklistener() {
public void onclick(view arg0) {
int id = arg0.getid();
switch (id) {
case r.id.btn1:
settabselection(0);
break;
case r.id.btn2:
settabselection(1);
break;
case r.id.btn3:
settabselection(2);
break;
case r.id.btn4:
settabselection(3);
break;
default:
break;
}
}
};
btn1.setonclicklistener(clicklistener);
btn2.setonclicklistener(clicklistener);
btn3.setonclicklistener(clicklistener);
btn4.setonclicklistener(clicklistener);
}
private void settabselection(int index) {
clearselection();// 每次选中之前先清楚掉上次的选中状态
// 开启一个fragment事务
fragmenttransaction transaction = fragementmanager.begintransaction();
// 先隐藏掉所有的fragment,以防止有多个fragment显示在界面上的情况
hidefragements(transaction);
switch (index) {
case 0:
btn1.setbackgroundcolor(color.parsecolor("#cfefef"));
btn1.settextcolor(color.parsecolor("#ffffff"));
app.setroom(btn1.gettext().tostring());
if (onefragment == null) {
onefragment = new onefragment();
transaction.add(r.id.framelayout, onefragment);
} else {
transaction.show(onefragment);
onefragment.mpullrefreshlistview.setrefreshing(true);
}
break;
case 1:
btn2.setbackgroundcolor(color.parsecolor("#cfefef"));
btn2.settextcolor(color.parsecolor("#ffffff"));
app.setroom(btn2.gettext().tostring());
if (twofragment == null) {
twofragment = new twofragment();
transaction.add(r.id.framelayout, twofragment);
} else {
transaction.show(twofragment);
twofragment.mpullrefreshlistview.setrefreshing(true);
}
break;
case 2:
btn3.setbackgroundcolor(color.parsecolor("#cfefef"));
btn3.settextcolor(color.parsecolor("#ffffff"));
app.setroom(btn3.gettext().tostring());
if (threefragment == null) {
threefragment = new threefragment();
transaction.add(r.id.framelayout, threefragment);
} else {
transaction.show(threefragment);
threefragment.mpullrefreshlistview.setrefreshing(true);
}
break;
case 3:
btn4.setbackgroundcolor(color.parsecolor("#cfefef"));
btn4.settextcolor(color.parsecolor("#ffffff"));
app.setroom(btn4.gettext().tostring());
if (fourfragment == null) {
fourfragment = new fourfragment();
transaction.add(r.id.framelayout, fourfragment);
} else {
transaction.show(fourfragment);
fourfragment.mpullrefreshlistview.setrefreshing(true);
}
break;
default:
break;
}
transaction.commit();
}
/**
* 清除掉所有的选中状态。
*/
private void clearselection() {
btn1.setbackgroundcolor(color.parsecolor("#efefef"));
btn1.settextcolor(color.parsecolor("#234567"));
btn2.setbackgroundcolor(color.parsecolor("#efefef"));
btn2.settextcolor(color.parsecolor("#234567"));
btn3.setbackgroundcolor(color.parsecolor("#efefef"));
btn3.settextcolor(color.parsecolor("#234567"));
btn4.setbackgroundcolor(color.parsecolor("#efefef"));
btn4.settextcolor(color.parsecolor("#234567"));
}
/**
* 将所有的fragment都置为隐藏状态。
* 
* @param transaction
* 用于对fragment执行操作的事务
*/
private void hidefragements(fragmenttransaction transaction) {
if (onefragment != null) {
transaction.hide(onefragment);
}
if (twofragment != null) {
transaction.hide(twofragment);
}
if (threefragment != null) {
transaction.hide(threefragment);
}
if (fourfragment != null) {
transaction.hide(fourfragment);
}
}
}

四个fragment都差不多,这里是第一个:

package com.example.fragmentmytest;
import java.util.arraylist;
import java.util.linkedlist;
import java.util.list;
import android.app.activity;
import android.os.asynctask;
import android.os.bundle;
import android.support.v4.app.fragment;
import android.util.log;
import android.view.layoutinflater;
import android.view.view;
import android.view.viewgroup;
import android.widget.adapterview;
import android.widget.arrayadapter;
import android.widget.adapterview.onitemclicklistener;
import android.widget.listview;
import com.example.adapter.myadapter;
import com.example.dao.computerroomstatus;
import com.example.utils.toastutils;
import com.handmark.pulltorefresh.library.pulltorefreshbase;
import com.handmark.pulltorefresh.library.pulltorefreshbase.mode;
import com.handmark.pulltorefresh.library.pulltorefreshbase.onrefreshlistener2;
import com.handmark.pulltorefresh.library.pulltorefreshlistview;
public class onefragment extends fragment {
string room = "204";
public pulltorefreshlistview mpullrefreshlistview;
// private arrayadapter<string> madapter;
private int mitemcount = 9;
// private linkedlist<string> mlistitems;
private myadapter madapter;
private list<computerroomstatus> data;
public view oncreateview(layoutinflater inflater, viewgroup container,
bundle savedinstancestatus) {
view view = inflater.inflate(r.layout.twolayout, container, false);
mpullrefreshlistview = (pulltorefreshlistview) view
.findviewbyid(r.id.pull_refresh_list_2);
mpullrefreshlistview.setmode(mode.pull_from_start);
return view;
}
@override
public void onviewcreated(view view, bundle savedinstancestate) {
// todo auto-generated method stub
super.onviewcreated(view, savedinstancestate);
initdatas();
// madapter1 = new myadapter(getactivity(), data);
madapter = new myadapter(getactivity(), data, room,mpullrefreshlistview);
mpullrefreshlistview.setadapter(madapter);
mpullrefreshlistview
.setonrefreshlistener(new onrefreshlistener2<listview>() {
@override
public void onpulldowntorefresh(
pulltorefreshbase<listview> refreshview) {
log.e("tag", "onpulldowntorefresh");
// 这里写下拉刷新的任务
new getdatatask().execute();
}
@override
public void onpulluptorefresh(
pulltorefreshbase<listview> refreshview) {
log.e("tag", "onpulluptorefresh");
// 这里写上拉加载更多的任务
new getdatatask().execute();
}
});
mpullrefreshlistview.setrefreshing(true);
}
private void initdatas() {
// 初始化数据和数据源
data = new arraylist<computerroomstatus>();
for (int i = 0; i < mitemcount; i++) {
data.add(new computerroomstatus(i, "1", "1", "3", "1", "1", "1"));
}
}
protected void autorefresh() {
mpullrefreshlistview.setrefreshing(true);
}
// 请求网络接口,这里是做的假数据
private class getdatatask extends asynctask<void, void, string> {
@override
protected string doinbackground(void... params) {
try {
// todo 解析json
thread.sleep(1000);
} catch (interruptedexception e) {
}
return "" + (mitemcount++);
}
@override
protected void onpostexecute(string result) {
// data.add(new
// computerroomstatus(1,result,result,result,result,result,result));
data.add(new computerroomstatus(integer.parseint(result), "", "",
"3", "1", "1", "1"));
madapter.notifydatasetchanged();
mpullrefreshlistview.onrefreshcomplete();
}
}
}

myadapter

package com.example.adapter;
import java.util.list;
import android.content.context;
import android.content.dialoginterface;
import android.content.res.colorstatelist;
import android.graphics.color;
import android.text.textutils;
import android.view.layoutinflater;
import android.view.view;
import android.view.viewgroup;
import android.widget.baseadapter;
import android.widget.button;
import android.widget.textview;
import com.example.dao.computerroomstatus;
import com.example.dialog.customdialogorder;
import com.example.fragmentmytest.r;
import com.example.utils.toastutils;
import com.handmark.pulltorefresh.library.pulltorefreshlistview;
public class myadapter extends baseadapter {
private context context;
private list<computerroomstatus> data;
private layoutinflater layoutinflater;
private string room;
private viewholder holder = null;
private pulltorefreshlistview mpullrefreshlistview;
public myadapter(context context, list<computerroomstatus> data, string room,pulltorefreshlistview mpullrefreshlistview) {
this.context = context;
this.data = data;
this.room = room;
this.mpullrefreshlistview = mpullrefreshlistview;
layoutinflater = layoutinflater.from(context);
}
@override
public int getcount() {
return data.size();
}
@override
public object getitem(int position) {
return data.get(position);
}
@override
public long getitemid(int position) {
return position;
}
@override
public view getview(int position, view convertview, viewgroup parent) {
if (convertview == null) {
holder = new viewholder();
convertview = layoutinflater.inflate(r.layout.lv_item, null);
holder.seatid = (textview) convertview.findviewbyid(r.id.seatid);
holder.time1 = (button) convertview.findviewbyid(r.id.time1);
holder.time2 = (button) convertview.findviewbyid(r.id.time2);
holder.time3 = (button) convertview.findviewbyid(r.id.time3);
holder.time4 = (button) convertview.findviewbyid(r.id.time4);
holder.time5 = (button) convertview.findviewbyid(r.id.time5);
holder.time6 = (button) convertview.findviewbyid(r.id.time6);
convertview.settag(holder);
} else {
holder = (viewholder) convertview.gettag();
}
// 获得集合中实体类对象
final computerroomstatus s = data.get(position);
holder.seatid.settext(s.getid() + "");
holder.time1.setonclicklistener(new lvbuttonlistener(position));
holder.time2.setonclicklistener(new lvbuttonlistener(position));
holder.time3.setonclicklistener(new lvbuttonlistener(position));
holder.time4.setonclicklistener(new lvbuttonlistener(position));
holder.time5.setonclicklistener(new lvbuttonlistener(position));
holder.time6.setonclicklistener(new lvbuttonlistener(position));
//略一部分不要紧的......
return convertview;
}
class lvbuttonlistener implements view.onclicklistener {
private int position;
lvbuttonlistener(int pos) {
position = pos;
}
@override
public void onclick(view v) {
switch (v.getid()) {
case r.id.time1:
showdialog(position, "1");
break;
case r.id.time2:
showdialog(position, "2");
break;
case r.id.time3:
showdialog(position, "3");
break;
case r.id.time4:
showdialog(position, "4");
break;
case r.id.time5:
showdialog(position, "5");
break;
case r.id.time6:
showdialog(position, "6");
break;
default:
break;
}
}
}
public void showdialog(final int id, final string witch) {
final customdialogorder.builder builder = new customdialogorder.builder(
context);
builder.settitle("预约上机");
builder.setpositivebutton("预约", new dialoginterface.onclicklistener() {
public void onclick(dialoginterface dialog, int which) {
dialog.dismiss();
// 提交的时候提交application的room和position和witch
toastutils.mytoast(context, room + ":" + id + ":" + witch);
//todo 拼接字符串 get到指定的接口
//todo 并且刷新执行下拉刷新
mpullrefreshlistview.setrefreshing(true);
}
});
builder.setnegativebutton("取消",
new android.content.dialoginterface.onclicklistener() {
public void onclick(dialoginterface dialog, int which) {
dialog.dismiss();
}
});
builder.create().show();
}
}
class viewholder {
textview seatid;
button time1, time2, time3, time4, time5, time6;
}

以上所述是小编给大家介绍的android 仿今日头条简单的刷新效果实例代码的相关知识,希望对大家有所帮助