当前位置: 编程技术>综合
本页文章导读:
▪android 类似网易首页的图片切换(viewPager) 1.activity调用
private View viewImage() {
LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
viewPager = null;
View view = getLayoutInflat.........
▪android 实现下拉刷新 1.布局文件 listview
<com.and.netease.utils.MyListView
android:id="@+id/lv_b" android:layout_width="fill_parent"
.........
▪设计模式-命令模式 命令(Command)模式属于对象的行为模式,把一个请求或者操作封装到一个对象中,允许系统使用不同的请求把客户端参数化,对请求排队或者记录到请求日志,可以提供命令的撤销和恢复功能。.........
[1]android 类似网易首页的图片切换(viewPager)
1.activity调用
private View viewImage() {
LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
viewPager = null;
View view = getLayoutInflater().inflate(R.layout.news_lstview_header,
null);
viewPager = (ViewPager) view.findViewById(R.id.news_layout_viewPager);
viewPager.setLayoutParams(new ListView.LayoutParams(
ListView.LayoutParams.FILL_PARENT, 300));
List<View> list = new ArrayList<View>();
for (int i = 0; i < 3; i++) {
ImageView imgView = new ImageView(this);
imgView.setLayoutParams(mParams);
imgView.setImageResource(pics[i]);
list.add(imgView);
}
viewPager.setAdapter(new MyPagerAdapter(list));
viewPager.setCurrentItem(0);
return viewPager;
}
2.适配器
package com.and.netease.utils;
import java.util.List;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
public class MyPagerAdapter extends PagerAdapter {
List<View> views;
public MyPagerAdapter(List<View> v) {
super();
views = v;
}
@Override
public void destroyItem(View v, int pos, Object arg2) {
((ViewPager) v).removeView(views.get(pos));
}
@Override
public void finishUpdate(View arg0) {
}
@Override
public int getCount() {
if (views != null)
{
return views.size();
}
return 0;
}
@Override
public Object instantiateItem(View v, int pos) {
((ViewPager) v).addView(views.get(pos));
return views.get(pos);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0==arg1;
}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View arg0) {
}
}
已有 0 人发表留言,猛击->>这里<<-参与讨论
ITeye推荐
private View viewImage() {
LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
viewPager = null;
View view = getLayoutInflater().inflate(R.layout.news_lstview_header,
null);
viewPager = (ViewPager) view.findViewById(R.id.news_layout_viewPager);
viewPager.setLayoutParams(new ListView.LayoutParams(
ListView.LayoutParams.FILL_PARENT, 300));
List<View> list = new ArrayList<View>();
for (int i = 0; i < 3; i++) {
ImageView imgView = new ImageView(this);
imgView.setLayoutParams(mParams);
imgView.setImageResource(pics[i]);
list.add(imgView);
}
viewPager.setAdapter(new MyPagerAdapter(list));
viewPager.setCurrentItem(0);
return viewPager;
}
2.适配器
package com.and.netease.utils;
import java.util.List;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
public class MyPagerAdapter extends PagerAdapter {
List<View> views;
public MyPagerAdapter(List<View> v) {
super();
views = v;
}
@Override
public void destroyItem(View v, int pos, Object arg2) {
((ViewPager) v).removeView(views.get(pos));
}
@Override
public void finishUpdate(View arg0) {
}
@Override
public int getCount() {
if (views != null)
{
return views.size();
}
return 0;
}
@Override
public Object instantiateItem(View v, int pos) {
((ViewPager) v).addView(views.get(pos));
return views.get(pos);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0==arg1;
}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View arg0) {
}
}
已有 0 人发表留言,猛击->>这里<<-参与讨论
ITeye推荐
- —软件人才免语言低担保 赴美带薪读研!—
[2]android 实现下拉刷新
1.布局文件 listview
<com.and.netease.utils.MyListView
android:id="@+id/lv_b" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_marginRight="10dip"
/>
2.刷新的header 布局
<?xml version="1.0" encoding="utf-8"?>
<!-- ListView的头部 -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
>
<!-- 内容 -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/head_contentLayout"
android:paddingLeft="30dp"
>
<!-- 箭头图像、进度条 -->
<FrameLayout
android:layout_width="18dip"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
>
<!-- 箭头 -->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="/blog_article/@drawable/arrow_down/index.html"
android:id="@+id/head_arrowImageView"
/>
<!-- 进度条 -->
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/head_progressBar"
android:visibility="gone"
/>
</FrameLayout>
<!-- 提示、最近更新 -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:orientation="vertical"
android:gravity="center_horizontal"
>
<!-- 提示 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下拉刷新"
android:textSize="15dp"
android:id="@+id/head_tipsTextView"
/>
<!-- 最近更新 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/head_lastUpdatedTextView"
android:text="上次更新"
android:textSize="12dp"
/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
3.核心代码
package com.and.netease.utils;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.and.netease.R;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MyListView extends ListView implements OnScrollListener {
private static final String TAG = "listview";
private final static int RELEASE_To_REFRESH = 0;
private final static int PULL_To_REFRESH = 1;
private final static int REFRESHING = 2;
private final static int DONE = 3;
private final static int LOADING = 4;
// 实际的padding的距离与界面上偏移距离的比例
private final static int RATIO = 3;
private LayoutInflater inflater;
private LinearLayout headView;
private TextView tipsTextview;
private TextView lastUpdatedTextView;
private ImageView arrowImageView;
private ProgressBar progressBar;
private RotateAnimation animation;
private RotateAnimation reverseAnimation;
// 用于保证startY的值在一个完整的touch事件中只被记录一次
private boolean isRecored;
private int headContentWidth;
private int headContentHeight;
private int startY;
private int firstItemIndex;
private int state;
private boolean isBack;
private OnRefreshListener refreshListener;
private boolean isRefreshable;
public MyListView(Context context) {
super(context);
init(context);
}
public MyListView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
System.out.println("scroll");
//setCacheColorHint(context.getResources().getColor(R.color.transparent));
inflater = LayoutInflater.from(context);
headView = (LinearLayout) inflater.inflate(R.layout.head, null);
arrowImageView = (ImageView) headView.findViewById(R.id.head_arrowImageView);
arrowImageView.setMinimumWidth(70);
arrowImageView.setMinimumHeight(50);
progressBar = (ProgressBar) headView.findViewById(R.id.head_progressBar);
tipsTextview = (TextView) headView.findViewById(R.id.head_tipsTextView);
lastUpdatedTextView = (TextView) headView.findViewById(R.id.head_lastUpdatedTextView);
measureView(headView);
headContentHeight = headView.getMeasuredHeight();
headContentWidth = headView.getMeasuredWidth();
headView.setPadding(0, -1 * headContentHeight, 0, 0);
headView.invalidate();
Log.v("size", "width:" + headContentWidth + " height:"+ headContentHeight);
addHeaderView(headView, null, false);
setOnScrollListener(this);
animation = new RotateAnimation(0, -180,RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
animation.setInterpolator(new LinearInterpolator());
animation.setDuration(250);
animation.setFillAfter(true);
reverseAnimation = new RotateAnimation(-180, 0,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
reverseAnimation.setInterpolator(new LinearInterpolator());
reverseAnimation.setDuration(200);
reverseAnimation.setFillAfter(true);
state = DONE;
isRefreshable = false;
}
public void onScroll(AbsListView arg0, int firstVisiableItem, int arg2,
int arg3) {
<com.and.netease.utils.MyListView
android:id="@+id/lv_b" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_marginRight="10dip"
/>
2.刷新的header 布局
<?xml version="1.0" encoding="utf-8"?>
<!-- ListView的头部 -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
>
<!-- 内容 -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/head_contentLayout"
android:paddingLeft="30dp"
>
<!-- 箭头图像、进度条 -->
<FrameLayout
android:layout_width="18dip"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
>
<!-- 箭头 -->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="/blog_article/@drawable/arrow_down/index.html"
android:id="@+id/head_arrowImageView"
/>
<!-- 进度条 -->
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/head_progressBar"
android:visibility="gone"
/>
</FrameLayout>
<!-- 提示、最近更新 -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:orientation="vertical"
android:gravity="center_horizontal"
>
<!-- 提示 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下拉刷新"
android:textSize="15dp"
android:id="@+id/head_tipsTextView"
/>
<!-- 最近更新 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/head_lastUpdatedTextView"
android:text="上次更新"
android:textSize="12dp"
/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
3.核心代码
package com.and.netease.utils;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.and.netease.R;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MyListView extends ListView implements OnScrollListener {
private static final String TAG = "listview";
private final static int RELEASE_To_REFRESH = 0;
private final static int PULL_To_REFRESH = 1;
private final static int REFRESHING = 2;
private final static int DONE = 3;
private final static int LOADING = 4;
// 实际的padding的距离与界面上偏移距离的比例
private final static int RATIO = 3;
private LayoutInflater inflater;
private LinearLayout headView;
private TextView tipsTextview;
private TextView lastUpdatedTextView;
private ImageView arrowImageView;
private ProgressBar progressBar;
private RotateAnimation animation;
private RotateAnimation reverseAnimation;
// 用于保证startY的值在一个完整的touch事件中只被记录一次
private boolean isRecored;
private int headContentWidth;
private int headContentHeight;
private int startY;
private int firstItemIndex;
private int state;
private boolean isBack;
private OnRefreshListener refreshListener;
private boolean isRefreshable;
public MyListView(Context context) {
super(context);
init(context);
}
public MyListView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
System.out.println("scroll");
//setCacheColorHint(context.getResources().getColor(R.color.transparent));
inflater = LayoutInflater.from(context);
headView = (LinearLayout) inflater.inflate(R.layout.head, null);
arrowImageView = (ImageView) headView.findViewById(R.id.head_arrowImageView);
arrowImageView.setMinimumWidth(70);
arrowImageView.setMinimumHeight(50);
progressBar = (ProgressBar) headView.findViewById(R.id.head_progressBar);
tipsTextview = (TextView) headView.findViewById(R.id.head_tipsTextView);
lastUpdatedTextView = (TextView) headView.findViewById(R.id.head_lastUpdatedTextView);
measureView(headView);
headContentHeight = headView.getMeasuredHeight();
headContentWidth = headView.getMeasuredWidth();
headView.setPadding(0, -1 * headContentHeight, 0, 0);
headView.invalidate();
Log.v("size", "width:" + headContentWidth + " height:"+ headContentHeight);
addHeaderView(headView, null, false);
setOnScrollListener(this);
animation = new RotateAnimation(0, -180,RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
animation.setInterpolator(new LinearInterpolator());
animation.setDuration(250);
animation.setFillAfter(true);
reverseAnimation = new RotateAnimation(-180, 0,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
reverseAnimation.setInterpolator(new LinearInterpolator());
reverseAnimation.setDuration(200);
reverseAnimation.setFillAfter(true);
state = DONE;
isRefreshable = false;
}
public void onScroll(AbsListView arg0, int firstVisiableItem, int arg2,
int arg3) {
[3]设计模式-命令模式
命令(Command)模式属于对象的行为模式,把一个请求或者操作封装到一个对象中,允许系统使用不同的请求把客户端参数化,对请求排队或者记录到请求日志,可以提供命令的撤销和恢复功能。命令模式是对命令的封装,把发出的命令的责任和执行命令的责任分开,委派给不同的对象。
设计角色:
客户(Client)角色:创建一个具体命令对象并确定接收者。
命令(Command)角色:声明一个给所有具体命令的抽象接口。
具体命令(ConcreteCommand)角色:定义一个接受者和行为之间的弱耦合;实现execute()方法,负责调用接受者的相应操作。execute方法通常叫做执行方法。
请求者(Invoker)角色:负责调用命令对象执行请求,相关方法叫做行动方法。
接收者(Reveiver)角色:负责具体实施和执行一个请求。任何一个类都可以成为接收者,实施和执行请求的方法叫做行动方法。
例子:
package cn.design.pattern.demo.command.client; import cn.design.pattern.demo.command.command.Command; import cn.design.pattern.demo.command.command.PlayCommand; import cn.design.pattern.demo.command.command.RewindCommand; import cn.design.pattern.demo.command.command.StopCommand; import cn.design.pattern.demo.command.invoker.Keypad; import cn.design.pattern.demo.command.reviever.AudioPlayer; /** * 客户端:创建命令和确定接收者 */ public class Client { private static Keypad keypad; private static AudioPlayer player = new AudioPlayer(); // 接收者 public static void main(String[] args) { test1(); test2(); } private static void test1() { // 创建三个命令并将接收者传入 Command play = new PlayCommand(player); Command stop = new StopCommand(player); Command rewind = new RewindCommand(player); keypad = new Keypad(play, rewind, stop); keypad.play(); keypad.stop(); keypad.rewind(); } private static void test2() { Command play = new Command() { @Override public void execute() { player.play(); } }; Command rewind = new Command() { @Override public void execute() { player.rewind(); } }; Command stop = new Command() { @Override public void execute() { player.stop(); } }; keypad = new Keypad(play, rewind, stop); keypad.play(); keypad.stop(); keypad.rewind(); } } package cn.design.pattern.demo.command.command; /** * 抽象命令角色 */ public interface Command { /** * 执行方法 */ public void execute(); } package cn.design.pattern.demo.command.command; import cn.design.pattern.demo.command.reviever.AudioPlayer; /** * 具体命令角色 * 定义一个接受者和行为之间的弱耦合 */ public class PlayCommand implements Command { private AudioPlayer audioPlayer; // 接收者 public PlayCommand(AudioPlayer audioPlayer) { this.audioPlayer = audioPlayer; } /** * 执行方法 */ @Override public void execute() { // 调用接收者相应方法,执行命令 audioPlayer.play(); } } package cn.design.pattern.demo.command.command; import cn.design.pattern.demo.command.reviever.AudioPlayer; /** * 具体命令角色 */ public class RewindCommand implements Command { private AudioPlayer audioPlayer; public RewindCommand(AudioPlayer audioPlayer) { this.audioPlayer = audioPlayer; } @Override public void execute() { audioPlayer.rewind(); } } package cn.design.pattern.demo.command.command; import cn.design.pattern.demo.command.reviever.AudioPlayer; /** * 具体命令角色 */ public class StopCommand implements Command { private AudioPlayer audioPlayer; public StopCommand(AudioPlayer audioPlayer) { this.audioPlayer = audioPlayer; } @Override public void execute() { audioPlayer.stop(); } } package cn.design.pattern.demo.command.invoker; import cn.design.pattern.demo.command.command.Command; /** * 请求者角色 * 负责调用命令对象执行请求 */ public class Keypad { private Command playCmd; // 命令对象 private Command rewindCmd; private Command stopCmd; public Keypad(Command play, Command rewind, Command stop) { this.playCmd = play; this.rewindCmd = rewind; this.stopCmd = stop; } /** * 行动方法 */ public void play() { // 执行命令请求 playCmd.execute(); } public void rewind() { rewindCmd.execute(); } public void stop() { stopCmd.execute(); } } package cn.design.pattern.demo.command.reviever; /** * 接收者角色 */ public class AudioPlayer { public void play() { System.out.println("播放"); } public void rewind() { System.out.println("倒带"); } public void stop() { System.out.println("停止"); } }
特点:
1.新的命令很容易地被加入到系统里.
2.允许接受请求的一方决定是否要否决请求.
3.可以方便地设置一个命令队列.
4.实现对请求的Undo和Redo.
5.比较容易地将命令计入日志.
已有 0 人发表留言,猛击->>这里<<-参与讨论
ITeye推荐
- —软件人才免语言低担保 赴美带薪读研!—
最新技术文章: