Android 仿今日頭條簡(jiǎn)單的刷新效果實(shí)例代碼
點(diǎn)擊按鈕,先自動(dòng)進(jìn)行下拉刷新,也可以手動(dòng)刷新,刷新完后,最后就多一行數(shù)據(jù)。有四個(gè)選項(xiàng)卡。
前兩天導(dǎo)師要求做一個(gè)給本科學(xué)生預(yù)定機(jī)房座位的app,出發(fā)點(diǎn)來自這里。做著做著遇到很多問題,都解決了。這個(gè)效果感覺還不錯(cuò),整理一下。
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; /** * 用于對(duì)Fragment進(jìn)行管理 */ FragmentManager fragementManager; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); app = (CustomApplication) getApplication(); // 獲得CustomApplication對(duì)象 // 必須繼承FragmentActivity才能用getSupportFragmentManager();最好使用v4.app,已經(jīng)沒怎么有人使用app中的了 fragementManager = getSupportFragmentManager(); init(); // 第一次啟動(dòng)時(shí)選中第0個(gè)tab setTabSelection(0);// 不能左右滑動(dòng)的默認(rèn)值 } 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();// 每次選中之前先清楚掉上次的選中狀態(tài) // 開啟一個(gè)Fragment事務(wù) FragmentTransaction transaction = fragementManager.beginTransaction(); // 先隱藏掉所有的Fragment,以防止有多個(gè)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(); } /** * 清除掉所有的選中狀態(tài)。 */ 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都置為隱藏狀態(tài)。 * * @param transaction * 用于對(duì)Fragment執(zhí)行操作的事務(wù) */ 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); } } }
四個(gè)Fragment都差不多,這里是第一個(gè):
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"); // 這里寫下拉刷新的任務(wù) new GetDataTask().execute(); } @Override public void onPullUpToRefresh( PullToRefreshBase<ListView> refreshView) { Log.e("TAG", "onPullUpToRefresh"); // 這里寫上拉加載更多的任務(wù) new GetDataTask().execute(); } }); mPullRefreshListView.setRefreshing(true); } private void initDatas() { // 初始化數(shù)據(jù)和數(shù)據(jù)源 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); } // 請(qǐng)求網(wǎng)絡(luò)接口,這里是做的假數(shù)據(jù) 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(); } // 獲得集合中實(shí)體類對(duì)象 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("預(yù)約上機(jī)"); builder.setPositiveButton("預(yù)約", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); // 提交的時(shí)候提交application的room和position和witch ToastUtils.MyToast(context, room + ":" + id + ":" + witch); //TODO 拼接字符串 GET到指定的接口 //TODO 并且刷新執(zhí)行下拉刷新 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 仿今日頭條簡(jiǎn)單的刷新效果實(shí)例代碼的相關(guān)知識(shí),希望對(duì)大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的,再此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- android自定義view仿今日頭條加載文字變色效果
- Android仿今日頭條頂部導(dǎo)航欄效果的實(shí)例代碼
- Android仿今日頭條多個(gè)fragment懶加載的實(shí)現(xiàn)
- Android使用RecyclerView實(shí)現(xiàn)今日頭條頻道管理功能
- Android 仿今日頭條評(píng)論時(shí)鍵盤自動(dòng)彈出的效果(推薦)
- Android仿今日頭條APP實(shí)現(xiàn)下拉導(dǎo)航選擇菜單效果
- Android應(yīng)用中仿今日頭條App制作ViewPager指示器
- Android實(shí)現(xiàn)仿網(wǎng)易今日頭條等自定義頻道listview 或者grideview等item上移到另一個(gè)view中
- Android仿今日頭條滑動(dòng)頁面導(dǎo)航效果
- Android實(shí)現(xiàn)今日頭條訂閱頻道效果
相關(guān)文章
詳解Android中Service服務(wù)的基礎(chǔ)知識(shí)及編寫方法
這篇文章主要介紹了詳解Android中Service服務(wù)的基礎(chǔ)知識(shí)及編寫方法,包括Service的啟動(dòng)流程及生命周期等基本內(nèi)容,需要的朋友可以參考下2016-04-04Android中捕獲TTextView文本中的鏈接點(diǎn)擊事件方法
這篇文章主要介紹了Android中捕獲TTextView文本中的鏈接點(diǎn)擊事件方法,本文給出了實(shí)現(xiàn)代碼和使用方法以及實(shí)現(xiàn)自己控制的方法,需要的朋友可以參考下2015-01-01Android編程實(shí)現(xiàn)TextView部分顏色變動(dòng)的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)TextView部分顏色變動(dòng)的方法,涉及Android針對(duì)TextView樣式操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11Android利用Java優(yōu)雅消除復(fù)雜條件表達(dá)式的方法
這篇文章主要介紹了Android利用Java優(yōu)雅消除復(fù)雜條件表達(dá)式,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值。感興趣的小伙伴可以參考一下2022-06-06Android基于Toolbar實(shí)現(xiàn)頂部標(biāo)題欄及后退鍵
這篇文章主要介紹了Android基于Toolbar實(shí)現(xiàn)頂部標(biāo)題欄及后退鍵,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09關(guān)于Android高德地圖的簡(jiǎn)單開發(fā)實(shí)例代碼(DEMO)
高德地圖在日常生活中經(jīng)常會(huì)用到,那么基于代碼如何實(shí)現(xiàn)高德地圖呢?下面小編給大家分享一個(gè)demo幫助大家學(xué)習(xí)android高德地圖的簡(jiǎn)單開發(fā),需要的朋友參考下2016-11-11Android 系統(tǒng)相機(jī)拍照后相片無法在相冊(cè)中顯示解決辦法
這篇文章主要介紹了Android 系統(tǒng)相機(jī)拍照后相片無法在相冊(cè)中顯示解決辦法的相關(guān)資料,需要的朋友可以參考下2016-12-12