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 仿今日头条简单的刷新效果实例代码的相关知识,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的,再此也非常感谢大家对网站的支持!

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索android
今日头条刷新
今日头条无法刷新、今日头条刷新不了、今日头条下拉刷新、今日头条wifi不能刷新、今日头条不能刷新,以便于您获取更多的相关知识。

时间: 2024-11-01 11:17:37

Android 仿今日头条简单的刷新效果实例代码_Android的相关文章

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

点击按钮,先自动进行下拉刷新,也可以手动刷新,刷新完后,最后就多一行数据.有四个选项卡. 前两天导师要求做一个给本科学生预定机房座位的app,出发点来自这里.做着做着遇到很多问题,都解决了.这个效果感觉还不错,整理一下. MainActivity package com.example.fragmentmytest; import android.content.DialogInterface; import android.graphics.Color; import android.os.B

Android仿今日头条滑动页面导航效果_Android

最近项目中用到了滑动页面,也就是和目前市场上很火的"今日头条"页面滑动类似,在网上找了一下,大部分都是用ViewPager来实现的,刚开始我用的是ViewPager+ViewGroup,上面的标题按钮用的是HorizontalScrollView,写完之后感觉效果比较生硬,果断换掉,发现了一个效果比较好的第三方,也就是今天的主题:PagerSlidingTabStrip.好了,下面来具体介绍一下PagerSlidingTabStrip,进行一下源码解析. 一.看一下demo的样子吧 二

Android仿美团下拉菜单(商品选购)实例代码_Android

美团电商应用平台大家使用非常频繁,下面小编通过本文给大家介绍电商应用平台中常用的选择类别下拉列表的实现.先给大家展示下效果图: 一.下拉列表的实现 其实实现方法有很多,这时实现的也没有什么技术含量,只是总结下自己在项目中的做法,也提供一个思路. 首先是列表的数据,一般数据都是从后台读过来,这里因为没有后台,所以写死在客户端: private void initMenuData() { menuData = new ArrayList<map<string, string=""

Android中的Button自定义点击效果实例代码_Android

方法一1.放在drawable下的selector.xml文件 复制代码 代码如下: <android="http://schemas.android.com/apk/res/Android">    android:drawable="@drawable/temp2" /> 2.布局文件main.xml 复制代码 代码如下: <http://schemas.android.com/apk/res/android"    andro

Android仿今日头条APP实现下拉导航选择菜单效果_Android

本文实例为大家分享了在Android中如何实现下拉导航选择菜单效果的全过程,供大家参考,具体内容如下 关于下拉导航选择菜单效果在新闻客户端中用的比较多,当然也可以用在其他的项目中,这样可以很方便的选择更多的菜单.我们可以让我们的应用顶部有左右滑动或进行切换的导航菜单,也可以为了增强用户体验在应用中添加这样的下拉导航选择菜单效果. 关于它的实现原理,其实也是挺简单的,就是使用PopupWindow来进行展现,在显示时控制其高度并配置以相应的动画效果.在PopupWindow中我使用GridView

Android 仿今日头条评论时键盘自动弹出的效果(推荐)

Android 仿今日头条评论时键盘自动弹出的效果:当点击评论时,弹出对话框,同时弹出软键盘,当点击返回键时,将对话框关闭,不只是关闭软键盘. 效果图: 对这个对话框设置一个style效果: <style name="inputDialog" parent="@android:style/Theme.Holo.Light.Dialog"> <item name="android:windowBackground">@col

Android界面上拉下拉的回弹效果实例代码

废话不多说,具体代码如下所示: public class MyScrollView extends ScrollView { private View childView; public MyScrollView(Context context) { super(context); } public MyScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public MyScrollView(Co

Android使用PullToRefresh实现上拉加载和下拉刷新效果的代码_Android

在没给大家介绍正文之前,先给大家介绍展示下运行图,如果大家感觉还不错,请继续往下阅读: 相关阅读:分享Android中pullToRefresh的使用心得 项目已同步至:https://github.com/nanchen2251/pullToRefreshDemo 简单使用详情: 1)studio可以直接在app的module设置中直接进行搜索,但是有-的必须添上,而不能用空格代替,为了更加了解这个东西,我还是推荐大家去这里看看,奉上网址: https://github.com/chrisba

Android5.0多种侧滑栏效果实例代码_Android

1.普通侧滑 效果图:   思路:通过自定义View继承HorizontalScrollView,然后重写onMeasure(),onLayout(),onTouchEvent() 方法并设置menu(通过动画使menu开始时处于隐藏状态)布局和content布局.(注意:使用ViewHelper类需要导入nineoldandroids-2.4.0.jar包) menu(left_menu)布局代码: <?xml version="1.0" encoding="utf-