准备,分析:
分析下这个界面,主要由几个tabbar分管不同的界面,首先我们会想到TabActivity,这是我们以前的做法,安卓更新越来越好,功能也越来越完善,我们也要时刻更新自己的技术,要不就会被淘汰,TabActivity已经被google淘汰了,现在大多数的做法已经换成了TabHost加Fragment,本文是采用FragmentTabHost类
FragmentTabhost实际上是又对TabHost的封装,使我们可以更快捷的开发
FragmentTabhost的具体用法和TabHost的用法是差不多的,只不过Activity换成了Fragment,碎片在重用上给我们开发带来了很大的方便,不过让人烦恼的事每次都会重新onCreateView()让无数开发者止步于此,本人也在学习改善的过程中,目前只用fragment写过两个项目,总体还是比较喜欢这个东东的,切换效果什么的比Activity好加多了!
下面说一下这个东东遇到的问题和需要注意的问题:分辨率的适配:分辨率适配(拿来主义)
正在运行的android程序,按home键后退回桌面,再次点击程序图片又重启程序,解决方案:去掉首次进入程序Activity设置的启动模式 android:launchMode="singleTask"
Log日志工具类:可从工程里面util包下copy Logger类
本应用用的的图片均来自新浪微博客户端(如有侵权,请告之)
开始,构建
本应采用的是比较普遍的一种分包方式,主要按照组件的类型来分:构建配置类,基类,应用类(application)
构建工具类(其实就是把总结的工具类CV一下)
看到这些,肯定会有很多人会想:都用别人的安全不,好用不(如果有好用的可以分享下)----曾经自己也写过,http的一些操作类啊什么的,但总不如人家封装的完善,既然不如开源的好,那就直接套用的,不适应的地方自己在做下修改,何乐而不为呢?也有很多ui绑定点击事件的开源类库,在本应用中没有到...
//=========所有Activity继承该类,用于实现一些基本操作/**
* 应用基类
*
* @author Johnny
*
*/
public class BaseActivity extends FragmentActivity {
public MyHandler myHandler;
protected ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0);
init();
}
private void init() {
myHandler = new MyHandler(this);
progressDialog = new ProgressDialog(this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setCancelable(true);
progressDialog.setMessage("正在加载");
progressDialog.setCanceledOnTouchOutside(false);
}
public static class MyHandler extends Handler {
WeakReference baseActivitys;
MyHandler(BaseActivity baseActivity) {
baseActivitys = new WeakReference(baseActivity);
}
@Override
public void handleMessage(Message msg) {// 定义一个Handler,用于处理下载线程与UI间通讯
if (!Thread.currentThread().isInterrupted()) {
final BaseActivity baseActivity = baseActivitys.get();
switch (msg.what) {
case AppConfig.SHWO_PROGRESS:
baseActivity.progressDialog.show();// 显示进度对话框
break;
case AppConfig.HIDE_PROGRESS:
baseActivity.progressDialog.hide();// 隐藏进度对话框,不可使用dismiss()、cancel(),否则再次调用show()时,显示的对话框小圆圈不会动。(WebView)
break;
case AppConfig.DISS_PROGRESS:
baseActivity.progressDialog.dismiss();//关闭进度对话框
case _ERR_SHOW:
baseActivity.ToastShow(_ERR);// 网络异常
break;
case AppConfig.DATA_ERR_SHOW:
baseActivity.ToastShow(AppConfig.DATA_ERR);// 数据异常
break;
default:
baseActivity.getMessage(msg);
break;
}
}
super.handleMessage(msg);
}
};
public void showProgress(){
myHandler.sendEmptyMessage(AppConfig.SHWO_PROGRESS);
}
public void hideProgress(){
myHandler.sendEmptyMessage(AppConfig.HIDE_PROGRESS);
}
public void dissProgress(){
myHandler.sendEmptyMessage(AppConfig.DISS_PROGRESS);
}
public void netErrShow() {
myHandler.sendEmptyMessage(_ERR_SHOW);
}
public void dataErrShow() {
myHandler.sendEmptyMessage(AppConfig.DATA_ERR_SHOW);
}
public void sendMessage(int what){
myHandler.sendEmptyMessage(what);
}
public void sendMessage(Message msg){
myHandler.sendMessage(msg);
}
public void sendMessage(Message msg,long delayMillis){
myHandler.sendMessageDelayed(msg, delayMillis);
}
public void ToastShow(String content) {
ToastUtils.show(BaseActivity.this.getApplicationContext(), content);
}
/**
* 接受消息 实现类重写该方法接收消息
* @param msg
*/
protected void getMessage(Message msg) {
}
}
//===============主界面/**
* 主界面
* @author Johnny
*
*/
public class KKsinaActivity extends BaseActivity{
private FragmentTabHost mFth;
private LinkedList> classes;
private LayoutInflater layoutInflater;//布局加载管理器
private int mImageView[] = {R.drawable.tab_home,R.drawable.tab_message,R.drawable.tab_profile,R.drawable.tab_discover,R.drawable.tab_more};//Tab选项卡图标
private String mTextview[] = {"首页", "消息", "我","广场", "更多"};//Tab选项卡标题
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kksina);
initData();
initView();
}
private void initData() {
// TODO Auto-generated method stub
classes = new LinkedList>();
classes.add(HomeFragment.class);
classes.add(MessageFragment.class);
classes.add(ProfileFragment.class);
classes.add(DiscoverFragment.class);
classes.add(MoreFragment.class);
}
private void initView() {
// TODO Auto-generated method stub
layoutInflater = LayoutInflater.from(this);
//实例化TabHost对象,得到TabHost
mFth = (FragmentTabHost)findViewById(android.R.id.tabhost);
mFth.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
addTab(classes);
}
private void addTab(final LinkedList> classes) {
int size = classes.size();
for (int i = 0; i < size; i++) {
//为每个Tab按钮设置图标、文字和内容
TabSpec tabSpec = mFth.newTabSpec(mTextview[i]).setIndicator(getTabItemView(i));
//将Tab按钮添加进Tab选项卡中
mFth.addTab(tabSpec, classes.get(i), null);
mFth.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tabbar_bg);
}
}
/**
* 给Tab按钮设置图标和标题
*/
private View getTabItemView(int index){
View view = layoutInflater.inflate(R.layout.tab_item_view, null);
ImageView p_w_picpathView = (ImageView) view.findViewById(R.id.iv_tabbar);
p_w_picpathView.setImageResource(mImageView[index]);
TextView textView = (TextView) view.findViewById(R.id.tv_tabbar);
textView.setText(mTextview[index]);
return view;
}
}
//================AdapterBase/**
* 基本Adapter
* @author Johnny
*
* @param
*/
public abstract class AdapterBase extends BaseAdapter {
protected List mList = new LinkedList();
public List getList(){
return mList;
}
public void appendToList(List list) {
if (list == null) {
return;
}
mList.addAll(list);
notifyDataSetChanged();
}
public void appendToTopList(List list) {
if (list == null) {
return;
}
mList.addAll(0, list);
notifyDataSetChanged();
}
public void setList(List list) {
if (list == null) {
return;
}
mList=list;
notifyDataSetChanged();
}
public void clear() {
mList.clear();
notifyDataSetChanged();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mList.size();
}
@Override
public T getItem(int position) {
// TODO Auto-generated method stub
if(position > mList.size()-1){
return null;
}
return mList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (position == getCount() - 1) {
onReachBottom();
}
return getExView(position, convertView, parent);
}
protected abstract View getExView(int position, View convertView, ViewGroup parent);
protected abstract void onReachBottom();
}
结束,分享
如果觉得《android 仿微博评论编辑框_android 仿新浪微博开写篇》对你有帮助,请点赞、收藏,并留下你的观点哦!