一、
@Override
public Object getItem(int position){
Log.i("lzw","get Item "+position+" ");
return null;
}
BaseAdapter中的getItem函数总是不被调用。
打开Activity并没有输出调用getItem的相关信息。
BaseAdapter示例:
BaseAdapter adapter=new BaseAdapter(){
@Override
public int getCount(){
Log.i("lzw","getCount");
return 5;
}
@Override
public Object getItem(int position){
Log.i("lzw","get Item "+position+" ");
return null;
}
@Override
public long getItemId(int position){
Log.i("lzw","ItemId "+ position+" ");
return position;
}
public View getView(int position,View convertView,
ViewGroup parent){
Log.i("lzw",position+" "+convertView+" "+parent);
TextView tv=new TextView(MainActivity.this);
tv.setText(""+position);
tv.setTextSize(100);
return tv;
}
};
代码中添加布局控件示例:
// 创建一个LinearLayout,并向其中添加2个组件
LinearLayout line = new LinearLayout(BaseAdapterTest.this);
line.setOrientation(0);
ImageView image = new ImageView(BaseAdapterTest.this);
image.setImageResource(R.drawable.ic_launcher);
TextView text = new TextView(BaseAdapterTest.this);
text.setText("第" + (position +1 ) + "个列表项");
text.setTextSize(20);
text.setTextColor(Color.RED);
line.addView(image);
line.addView(text);
复用ConvertView:
BaseAdapter adapter = new BaseAdapter() {
@Override
public int getCount() {
Log.i("lzw", "getCount");
return 5;
}
@Override
public Object getItem(int position) {
Log.i("lzw", "get Item " + position + " ");
return null;
}
@Override
public long getItemId(int position) {
Log.i("lzw", "ItemId " + position + " ");
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
Log.i("lzw", position + " " + convertView + " " + parent);
ViewHolder holder = new ViewHolder();
if (convertView != null) {
holder = (ViewHolder) convertView.getTag();
} else {
TextView tv;
tv = new TextView(MainActivity.this);
tv.setTextSize(100);
holder.tv = tv;
convertView = (View) tv;
convertView.setTag(holder);
}
holder.tv.setText("" + position);
return convertView;
}
class ViewHolder {
TextView tv;
}
};
在代码中新建TextView,并且设置长宽:
tv=new TextView(this);
tv.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
root.addView(tv);
获取当前屏幕的宽高:
DisplayMetrics dm=new DisplayMetrics();
super.getWindowManager().getDefaultDisplay().
getMetrics(dm);
draw.setWidthAndHeight(dm.widthPixels,dm.heightPixels);
列表项的视图不相同时的BaseAdapter示例:
BaseAdapter adapter = new BaseAdapter() {
@Override
public int getCount() {
return 20;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
//翻页时也调用了很多次
public int getItemViewType(int position){
Log.i("lzw","getItemViewType "+position);
return position%2;
}
//发现只是在打开Activity的时候调用了一次
public int getViewTypeCount(){
Log.i("lzw","getViewTypeCount");
return 2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder=null;
if (convertView != null) {
holder = (ViewHolder) convertView.getTag();
} else {
if(position%2==0){
convertView=inflater.inflate(R.layout.row_layout, null);
}else{
convertView=inflater.inflate(R.layout.right_layout, null);
}
holder=new ViewHolder();
holder.tv=(TextView)convertView.findViewById(R.id.row_tv);
convertView.setTag(holder);
}
holder.tv.setText("" + position);
return convertView;
}
class ViewHolder {
TextView tv;
}
};
不自动弹出软键盘:
// 启动activity时不自动弹出软键盘
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
空格和换行text:
<string name="loveWord"> text\n\n</string>
在这样一行@drawable附近按住Ctrl键,直接可以打开引用的图片,看源代码的时候非常方便!
android:background="@drawable/confirm_dialog_bg2"
这个也可以,
android:onClick="exitbutton0"
延迟执行一个任务,
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(Appstart.this, Welcome.class);
startActivity(intent);
Appstart.this.finish();
}
}, 1000);
设置Dialog的图标:
setIcon(getResources().getDrawable(R.drawable.login_error_icon))
打开网页的Intent:
Uri uri = Uri.parse("http://3g.qq.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
也可以为整个布局添加监听器,
layout = (LinearLayout) findViewById(R.id.main_dialog_layout);
layout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "提示:点击窗口外部关闭窗口!",
Toast.LENGTH_SHORT).show();
}
});
所以,不仅是按钮,一切都可以添加点击监听器。
当弹出一个像Dialog的Activity时,按Dialog之外的地方应该要退出这个Activity,所以在class可以复写触摸事件,
@Override
public boolean onTouchEvent(MotionEvent event) {
finish();
return true;
}
关掉另外一个Activity,另外一个Activity中,
public static MainWeixin instance = null;
instance = this;
此Activity中,
MainWeixin.instance.finish();//关闭Main 这个Activity
页卡切换监听器,高亮显示下面的图标,像,
找朋友那里高亮显示了,在左右切换页卡的时候,换了找朋友上面的图片以及让下面发绿的灯光图片动画转移到当前页,感觉下面的图标被照亮了。
页卡切换监听器,
/*
* 页卡切换监听(原作者:D.Winter)
*/
public class MyOnPageChangeListener implements OnPageChangeListener {
@Override
public void onPageSelected(int arg0) {
Animation animation = null;
switch (arg0) {
case 0 :
mTab1.setImageDrawable(getResources().getDrawable(
R.drawable.tab_weixin_pressed));
if (currIndex == 1) {
animation = new TranslateAnimation(one, 0, 0, 0);
mTab2.setImageDrawable(getResources().getDrawable(
R.drawable.tab_address_normal));
} else if (currIndex == 2) {
animation = new TranslateAnimation(two, 0, 0, 0);
mTab3.setImageDrawable(getResources().getDrawable(
R.drawable.tab_find_frd_normal));
} else if (currIndex == 3) {
animation = new TranslateAnimation(three, 0, 0, 0);
mTab4.setImageDrawable(getResources().getDrawable(
R.drawable.tab_settings_normal));
}
break;
case 1 :
mTab2.setImageDrawable(getResources().getDrawable(
R.drawable.tab_address_pressed));
if (currIndex == 0) {
animation = new TranslateAnimation(zero, one, 0, 0);
mTab1.setImageDrawable(getResources().getDrawable(
R.drawable.tab_weixin_normal));
} else if (currIndex == 2) {
animation = new TranslateAnimation(two, one, 0, 0);
mTab3.setImageDrawable(getResources().getDrawable(
R.drawable.tab_find_frd_normal));
} else if (currIndex == 3) {
animation = new TranslateAnimation(three, one, 0, 0);
mTab4.setImageDrawable(getResources().getDrawable(
R.drawable.tab_settings_normal));
}
break;
case 2 :
mTab3.setImageDrawable(getResources().getDrawable(
R.drawable.tab_find_frd_pressed));
if (currIndex == 0) {
animation = new TranslateAnimation(zero, two, 0, 0);
mTab1.setImageDrawable(getResources().getDrawable(
R.drawable.tab_weixin_normal));
} else if (currIndex == 1) {
animation = new TranslateAnimation(one, two, 0, 0);
mTab2.setImageDrawable(getResources().getDrawable(
R.drawable.tab_address_normal));
} else if (currIndex == 3) {
animation = new TranslateAnimation(three, two, 0, 0);
mTab4.setImageDrawable(getResources().getDrawable(
R.drawable.tab_settings_normal));
}
break;
case 3 :
mTab4.setImageDrawable(getResources().getDrawable(
R.drawable.tab_settings_pressed));
if (currIndex == 0) {
animation = new TranslateAnimation(zero, three, 0, 0);
mTab1.setImageDrawable(getResources().getDrawable(
R.drawable.tab_weixin_normal));
} else if (currIndex == 1) {
animation = new TranslateAnimation(one, three, 0, 0);
mTab2.setImageDrawable(getResources().getDrawable(
R.drawable.tab_address_normal));
} else if (currIndex == 2) {
animation = new TranslateAnimation(two, three, 0, 0);
mTab3.setImageDrawable(getResources().getDrawable(
R.drawable.tab_find_frd_normal));
}
break;
}
currIndex = arg0;
animation.setFillAfter(true);// True:图片停在动画结束位置
animation.setDuration(150);
mTabImg.startAnimation(animation);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
}
mTabPager = (ViewPager) findViewById(R.id.tabpager);
mTabPager.setOnPageChangeListener(new MyOnPageChangeListener());
mTab1 = (ImageView) findViewById(R.id.img_weixin);
mTab2 = (ImageView) findViewById(R.id.img_address);
mTab3 = (ImageView) findViewById(R.id.img_friends);
mTab4 = (ImageView) findViewById(R.id.img_settings);
mTabImg = (ImageView) findViewById(R.id.img_tab_now);
mTab1.setOnClickListener(new MyOnClickListener(0));
mTab2.setOnClickListener(new MyOnClickListener(1));
mTab3.setOnClickListener(new MyOnClickListener(2));
mTab4.setOnClickListener(new MyOnClickListener(3));
Display currDisplay = getWindowManager().getDefaultDisplay();// 获取屏幕当前分辨率
int displayWidth = currDisplay.getWidth();
int displayHeight = currDisplay.getHeight();
one = displayWidth / 4; // 设置水平动画平移大小
two = one * 2;
three = one * 3;
获取layout实例,
// 获取LayoutInflater实例
inflater = (LayoutInflater) this
.getSystemService(LAYOUT_INFLATER_SERVICE);
// 这里的main布局是在inflate中加入的哦,以前都是直接this.setContentView()的吧?呵呵
// 该方法返回的是一个View的对象,是布局中的根
layout = inflater.inflate(R.layout.main_menu, null);
自定义popup window,
// 下面我们要考虑了,我怎样将我的layout加入到PopupWindow中呢???很简单
menuWindow = new PopupWindow(layout, LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT); // 后两个参数是width和height
menuWindow.showAtLocation(this.findViewById(R.id.mainweixin),
Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); // 设置layout在PopupWindow中显示的位置
// 如何获取我们main中的控件呢?也很简单
mClose = (LinearLayout) layout.findViewById(R.id.menu_close);
mCloseBtn = (LinearLayout) layout.findViewById(R.id.menu_close_btn);
获取振动器,
mVibrator = (Vibrator)getApplication().getSystemService(VIBRATOR_SERVICE);
SlidingDrawer布局文件,
<SlidingDrawer
android:id="@+id/slidingDrawer1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:content="@+id/content"
android:handle="@+id/handle" >
<Button
android:id="@+id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/shake_report_dragger_up" />
<LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f9f9f9" >
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@drawable/shake_line_up" />
</LinearLayout>
</SlidingDrawer>
Shake监听器示例。微信上摇一摇的效果实现,
mShakeListener.setOnShakeListener(new OnShakeListener() {
public void onShake() {
startAnim(); // 开始 摇一摇手掌动画
mShakeListener.stop();
startVibrato(); // 开始 震动
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Toast mtoast;
mtoast = Toast.makeText(getApplicationContext(),
"抱歉,暂时没有找到\n在同一时刻摇一摇的人。\n再试一次吧!", 10);
// mtoast.setGravity(Gravity.CENTER, 0, 0);
mtoast.show();
mVibrator.cancel();
mShakeListener.start();
}
}, 2000);
}
});
public void startAnim() { // 定义摇一摇动画动画
AnimationSet animup = new AnimationSet(true);
TranslateAnimation mytranslateanimup0 = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, -0.5f);
mytranslateanimup0.setDuration(1000);
TranslateAnimation mytranslateanimup1 = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, +0.5f);
mytranslateanimup1.setDuration(1000);
mytranslateanimup1.setStartOffset(1000);
animup.addAnimation(mytranslateanimup0);
animup.addAnimation(mytranslateanimup1);
mImgUp.startAnimation(animup);
AnimationSet animdn = new AnimationSet(true);
TranslateAnimation mytranslateanimdn0 = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, +0.5f);
mytranslateanimdn0.setDuration(1000);
TranslateAnimation mytranslateanimdn1 = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, -0.5f);
mytranslateanimdn1.setDuration(1000);
mytranslateanimdn1.setStartOffset(1000);
animdn.addAnimation(mytranslateanimdn0);
animdn.addAnimation(mytranslateanimdn1);
mImgDn.startAnimation(animdn);
}
振动,
public void startVibrato() { // 定义震动
mVibrator.vibrate(new long[]{500, 200, 500, 200}, -1); // 第一个{}里面是节奏数组,
// 第二个参数是重复次数,-1为不重复,非-1从pattern的指定下标开始重复
}
要在onDestroy方法中停止监听,否则该Activity结束了还在监听。
protected void onDestroy() {
super.onDestroy();
if (mShakeListener != null) {
mShakeListener.stop();
}
}
ViewPager在xml中写法示例,
<android.support.v4.view.ViewPager
android:id="@+id/whatsnew_viewpager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<android.support.v4.view.PagerTitleStrip
android:id="@+id/pagertitle"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="bottom"
android:background="#0000"
android:textColor="#ffff" />
</android.support.v4.view.ViewPager>
通过所在位置得到R文件的资源id,
getResources().getIdentifier(name,"layout",getPackageName()),null)