求帮忙看下,我想把一个类里面的方法抽出来,放在另一个类里面,然后这个类再调用,该怎么抽取啊。

obliviousSing 2016-08-25 04:15:58
太长,麻烦大家将就看下,我想把里面点击按钮切换页面的代码放到另一个类里面,然后这个MainActivity再调用这个类的方法,请问下该怎么写啊。

public class MainActivity extends Activity implements OnClickListener,SwipeRefreshLayout.OnRefreshListener {

private SwipeRefreshLayout refreshLayout;
private View title_gone;
private View title_index;
private TextView txt_title;
private FragmentManager fragmentManager;
private Fragment[] fragments;
private ImageView[] imageButtons;
private TextView[] textViews;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化布局元素
initViews();
//把fragment添加到事务中
addFragmentToTransaction();
//第一次启动时选中第0个tab
setTabSelection(0);
//下拉刷新
setRefresh();
}
//下拉刷新
private void setRefresh(){
refreshLayout=(SwipeRefreshLayout) findViewById(R.id.refresh_header);

}
@Override
public void onRefresh(){

}
//在这里获取到每个需要用到的控件的实例,并给它们设置好必要的点击事件。
private void initViews() {
title_gone=findViewById(R.id.title_gone);
title_index=findViewById(R.id.title_index);
txt_title=(TextView) findViewById(R.id.txt_title);
IndexFragment indexFragment=new IndexFragment();
MallFragment mallFragment=new MallFragment();
HealthFragment healthFragment=new HealthFragment();
MineFragment mineFragment=new MineFragment();
fragments=new Fragment[]{indexFragment,mallFragment,healthFragment,mineFragment};
ImageView indexImage = (ImageView) findViewById(R.id.tab_index_img);
ImageView mallImage = (ImageView) findViewById(R.id.tab_mall_img);
ImageView healthImage = (ImageView) findViewById(R.id.tab_health_img);
ImageView mineImage = (ImageView) findViewById(R.id.tab_mine_img);
imageButtons=new ImageView[]{indexImage,mallImage,healthImage,mineImage};
TextView indexText = (TextView) findViewById(R.id.tab_index_text);
TextView mallText = (TextView) findViewById(R.id.tab_mall_text);
TextView healthText = (TextView) findViewById(R.id.tab_health_text);
TextView mineText = (TextView) findViewById(R.id.tab_mine_text);
textViews=new TextView[]{indexText,mallText,healthText,mineText};
View indexLayout = findViewById(R.id.layout_index);
View mallLayout = findViewById(R.id.layout_mall);
View healthLayout = findViewById(R.id.layout_health);
View mineLayout = findViewById(R.id.layout_mine);
indexLayout.setOnClickListener(this);
mallLayout.setOnClickListener(this);
healthLayout.setOnClickListener(this);
mineLayout.setOnClickListener(this);
}
//把fragment添加到事务中.
public void addFragmentToTransaction(){
fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.content, fragments[0]);
transaction.add(R.id.content, fragments[1]);
transaction.add(R.id.content, fragments[2]);
transaction.add(R.id.content, fragments[3]);
transaction.commit();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.layout_index:
setTabSelection(0);
break;
case R.id.layout_mall:
setTabSelection(1);
break;
case R.id.layout_health:
setTabSelection(2);
break;
case R.id.layout_mine:
setTabSelection(3);
break;
default:
break;
}
}
//每个tab页对应的下标。0表示消息,1表示联系人,2表示动态,3表示设置。
private void setTabSelection(int index) {
//每次选中之前先清楚掉上次的选中状态
clearSelection();
//开启一个Fragment事务
FragmentTransaction transaction = fragmentManager.beginTransaction();
//先隐藏掉所有的Fragment,以防止有多个Fragment显示在界面上的情况
hideFragments(transaction);
//显示顶部标题
title_gone.setVisibility(View.VISIBLE);
switch (index) {
case 0:
imageButtons[0].setImageResource(R.drawable.tab_index_a);
textViews[0].setTextColor(0xFF3498db);
txt_title.setVisibility(View.GONE);
title_index.setVisibility(View.VISIBLE);
transaction.show(fragments[0]);
break;
case 1:
imageButtons[1].setImageResource(R.drawable.tab_mall_a);
textViews[1].setTextColor(0xFF3498db);
txt_title.setText("商城");
txt_title.setVisibility(View.VISIBLE);
title_index.setVisibility(View.GONE);
transaction.show(fragments[1]);
break;
case 2:
imageButtons[2].setImageResource(R.drawable.tab_health_a);
textViews[2].setTextColor(0xFF3498db);
txt_title.setText("健康");
txt_title.setVisibility(View.VISIBLE);
title_index.setVisibility(View.GONE);
transaction.show(fragments[2]);
break;
case 3:
imageButtons[3].setImageResource(R.drawable.tab_mine_a);
textViews[3].setTextColor(0xFF3498db);
title_gone.setVisibility(View.GONE);
transaction.show(fragments[3]);
break;
}
transaction.commit();
}
//清除掉所有的选中状态。
private void clearSelection() {
for (int i=0;i<4;i++){
textViews[i].setTextColor(0xFF999999);
}
imageButtons[0].setImageResource(R.drawable.tab_index);
imageButtons[1].setImageResource(R.drawable.tab_mall);
imageButtons[2].setImageResource(R.drawable.tab_health);
imageButtons[3].setImageResource(R.drawable.tab_mine);
}
//将所有的Fragment都置为隐藏状态。
private void hideFragments(FragmentTransaction transaction) {
for(int i=0;i<4;i++){
transaction.hide(fragments[i]);
}
}
}


...全文
250 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
网易云捕 2016-08-25
  • 打赏
  • 举报
回复
轮播图。可以用ViewPager自定义了一个轮播图效果的View,具体实现方式,网上太多了吧……
obliviousSing 2016-08-25
  • 打赏
  • 举报
回复
引用 3 楼 crash163 的回复:
[quote=引用 2 楼 obliviousSing 的回复:] [quote=引用 1 楼 crash163 的回复:] 在代码里面你获取点击事件用的是 @Override public void onClick(View v) { } 按你的想法是想单独操作,可以不用上面代码,在Oncreate函数里面里面直接: xxxxx.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ………………//操作 } }); xxxxx为title_index按钮或者某个view等对应Onclick函数里面的。 想放到另一个类里面,可以直接封装一层。
不好意思啊,最近刚学android,没怎么看懂,我现在想要的就是把MainActivity里面的所有的方法都移到另一个类里面,然后MainActivity再调用这个类,现在因为申明了很多变量,请问下,这些申明的变量也必须要在我新建的这个类里面申明才行么。[/quote] 看你怎么调用了,有些可以直接当做函数的参数传进去,譬如你有一个针对按钮的操作,Button,在你自己的类里面不用声明,只要在函数里面传这个Button参数就好了。 特别要说的是,这里面监控点击事件操作Onclick是最好不要移到另一个类里面的,因为每一个Activity页面对应自身页面的点击事件,便于管理,如果你放到其他类里面,做是可能可以做,可以封装一层自己的监控操作,但是感觉没有什么意义……[/quote] 谢了,请问下做轮播图用什么比较好啊,我用ViewFlipper 做轮播图,调用startFlipping()的时候,程序就显示 xxx已停止运行,这怎么回事啊,也没有报错。
网易云捕 2016-08-25
  • 打赏
  • 举报
回复
引用 2 楼 obliviousSing 的回复:
[quote=引用 1 楼 crash163 的回复:] 在代码里面你获取点击事件用的是 @Override public void onClick(View v) { } 按你的想法是想单独操作,可以不用上面代码,在Oncreate函数里面里面直接: xxxxx.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ………………//操作 } }); xxxxx为title_index按钮或者某个view等对应Onclick函数里面的。 想放到另一个类里面,可以直接封装一层。
不好意思啊,最近刚学android,没怎么看懂,我现在想要的就是把MainActivity里面的所有的方法都移到另一个类里面,然后MainActivity再调用这个类,现在因为申明了很多变量,请问下,这些申明的变量也必须要在我新建的这个类里面申明才行么。[/quote] 看你怎么调用了,有些可以直接当做函数的参数传进去,譬如你有一个针对按钮的操作,Button,在你自己的类里面不用声明,只要在函数里面传这个Button参数就好了。 特别要说的是,这里面监控点击事件操作Onclick是最好不要移到另一个类里面的,因为每一个Activity页面对应自身页面的点击事件,便于管理,如果你放到其他类里面,做是可能可以做,可以封装一层自己的监控操作,但是感觉没有什么意义……
obliviousSing 2016-08-25
  • 打赏
  • 举报
回复
引用 1 楼 crash163 的回复:
在代码里面你获取点击事件用的是 @Override public void onClick(View v) { } 按你的想法是想单独操作,可以不用上面代码,在Oncreate函数里面里面直接: xxxxx.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ………………//操作 } }); xxxxx为title_index按钮或者某个view等对应Onclick函数里面的。 想放到另一个类里面,可以直接封装一层。
不好意思啊,最近刚学android,没怎么看懂,我现在想要的就是把MainActivity里面的所有的方法都移到另一个类里面,然后MainActivity再调用这个类,现在因为申明了很多变量,请问下,这些申明的变量也必须要在我新建的这个类里面申明才行么。
网易云捕 2016-08-25
  • 打赏
  • 举报
回复
在代码里面你获取点击事件用的是 @Override public void onClick(View v) { } 按你的想法是想单独操作,可以不用上面代码,在Oncreate函数里面里面直接: xxxxx.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ………………//操作 } }); xxxxx为title_index按钮或者某个view等对应Onclick函数里面的。 想放到另一个类里面,可以直接封装一层。

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧