Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 仿今日頭條簡單的刷新效果實例代碼

Android 仿今日頭條簡單的刷新效果實例代碼

編輯:關於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 仿今日頭條簡單的刷新效果實例代碼的相關知識,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的,再此也非常感謝大家對本站網站的支持!

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved