点击按钮,先自动进行下拉刷新,也可以手动刷新,刷新完后,最后就多一行数据。有四个选项卡。
前两天导师要求做一个给本科学生预定机房座位的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个tabsetTabSelection(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;}@OverrIDepublic voID onVIEwCreated(VIEw vIEw,Bundle savedInstanceState) {// Todo auto-generated method stubsuper.onVIEwCreated(vIEw,savedInstanceState);initDatas();// mAdapter1 = new MyAdapter(getActivity(),data);mAdapter = new MyAdapter(getActivity(),data,room,mpullRefreshListVIEw);mpullRefreshListVIEw.setAdapter(mAdapter);mpullRefreshListVIEw.setonRefreshListener(new OnRefreshListener2<ListVIEw>() {@OverrIDepublic voID onPullDownToRefresh(PullToRefreshBase<ListVIEw> refreshVIEw) {Log.e("TAG","onPullDownToRefresh");// 这里写下拉刷新的任务new GetDataTask().execute();}@OverrIDepublic 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","3","1"));}}protected voID autoRefresh() {mpullRefreshListVIEw.setRefreshing(true);}// 请求网络接口,这里是做的假数据private class GetDataTask extends AsyncTask<VoID,VoID,String> {@OverrIDeprotected String doInBackground(VoID... params) {try {// Todo 解析JsonThread.sleep(1000);} catch (InterruptedException e) {}return "" + (mItemCount++);}@OverrIDeprotected voID onPostExecute(String result) {// data.add(new// ComputerRoomStatus(1,result,result));data.add(new ComputerRoomStatus(Integer.parseInt(result),"","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);}@OverrIDepublic int getCount() {return data.size();}@OverrIDepublic Object getItem(int position) {return data.get(position);}@OverrIDepublic long getItemID(int position) {return position;}@OverrIDepublic 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;}@OverrIDepublic 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和witchToastUtils.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 仿今日头条简单的刷新效果实例代码的相关知识,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的,再此也非常感谢大家对编程小技巧网站的支持!
总结以上是内存溢出为你收集整理的Android 仿今日头条简单的刷新效果实例代码全部内容,希望文章能够帮你解决Android 仿今日头条简单的刷新效果实例代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)