这是在我写的新闻App中实现的界面切换
贴出切换界面的主要代码:
xml代码:
<span style="font-size:14px;"> <android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="8"
android:flipInterval="30"
android:persistentDrawingCache="animation" /> </span>
Activity类代码:
<span style="font-size:14px;">public class MainActivity extends FragmentActivity implements OnClickListener{
private Button bottom_button_one = null;
private Button bottom_button_two = null;
private Button bottom_button_three = null;
private Button top_break = null;
private Button top_menu = null;
private Button menu_home = null;
private Button menu_refresh = null;
private Button menu_search = null;
private LinearLayout bottom_back = null;
private XinwenFragment xinwen = null;
private TupianFragment tupian = null;
private ShipinFragment shipin = null;
private LayoutInflater inflater = null;
private View view =null;
private PopupWindow pop = null;
//构造适配器
private List<Fragment> fragments = null;
//设定适配器
private ViewPager vp = null;
private FragmentManager fm = null;
private FragmentAdapter adapter = null;
@SuppressLint("InflateParams")
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//让进度条显示在标题栏上
requestWindowFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.activity_main);
xinwen = new XinwenFragment();
tupian = new TupianFragment();
shipin = new ShipinFragment();
fragments = new ArrayList<Fragment>();
fragments.add(xinwen);
fragments.add(tupian);
fragments.add(shipin);
fm = this.getSupportFragmentManager();
adapter = new FragmentAdapter(fm, fragments);
vp = (ViewPager) findViewById(R.id.viewpager);
vp.setAdapter(adapter);
bottom_button_one = (Button) findViewById(R.id.bottom_button_one);
bottom_button_two = (Button) findViewById(R.id.bottom_button_two);
bottom_button_three = (Button) findViewById(R.id.bottom_button_three);
top_break = (Button) findViewById(R.id.top_break);
top_menu = (Button) findViewById(R.id.top_menu);
bottom_back = (LinearLayout) findViewById(R.id.bottom_back);
bottom_button_one.setOnClickListener(this);
bottom_button_two.setOnClickListener(this);
bottom_button_three.setOnClickListener(this);
top_break.setOnClickListener(this);
top_menu.setOnClickListener(this);
inflater = LayoutInflater.from(this);
//引入窗口配置文件
view = inflater.inflate(R.layout.menu_layout, null);
menu_home = (Button) view.findViewById(R.id.menu_home);
menu_refresh = (Button) view.findViewById(R.id.menu_refresh);
menu_search = (Button) view.findViewById(R.id.menu_search);
menu_home.setOnClickListener(this);
menu_refresh.setOnClickListener(this);
menu_search.setOnClickListener(this);
//创建PopupWindow对象
pop= new PopupWindow(view, LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, false);
//需要设置一下此参数,点击外边可消息
pop.setBackgroundDrawable(new BitmapDrawable());
//设置点击窗口外边窗口消失
pop.setFocusable(true);
//设置默认的Fragment
setDefaultFragment();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
private void setDefaultFragment(){
//注意:remove会销毁Fragment,hide不销毁只是隐藏,所有add方法要重新new
vp.setCurrentItem(0,false); //false表示取消滑动效果,true则有滑动效果
}
//弹出带输入框的对话框
@SuppressLint("InflateParams")
private void inputTitleDialog(){
/*
Android中得到布局文件对象有两种方式
第一种,在Activity所在类中
this.getLayoutInflater().inflater(R.layout.布局文件名,null);
第二种,在非Activity所在类中
Context.getSystemService(Context.LAYOUT_INFLATER_SERVICE).inflater(R.layout.布局文件名,null);
*/
View view = this.getLayoutInflater().inflate(R.layout.dialog_layout, null);
final EditText text = (EditText) view.findViewById(R.id.edit_search);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("对话框标题")
.setIcon(R.drawable.ic_launcher)
.setView(view)
.setNegativeButton("取消", null)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String str = text.getText().toString();
if(!str.equals("")){
if(str.indexOf("http://")==0){
xinwen.wv.loadUrl(str);
}else{
xinwen.wv.loadUrl("http://www.baidu.com/s?wd="+str);
}
}
}
});
builder.show();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bottom_button_one:
bottom_back.setBackgroundResource(R.drawable.bottom_one);
vp.setCurrentItem(0, false);
break;
case R.id.bottom_button_two:
bottom_back.setBackgroundResource(R.drawable.bottom_two);
vp.setCurrentItem(1, false);
break;
case R.id.bottom_button_three:
bottom_back.setBackgroundResource(R.drawable.bottom_three);
vp.setCurrentItem(2, false);
break;
case R.id.top_break:
if(xinwen!=null)
xinwen.wv.goBack(); //后退
break;
case R.id.top_menu:
if(pop.isShowing()){
//隐藏窗口,如果设置了点击窗口外小时即不需要此方法隐藏
pop.dismiss();
}else{
//显示窗口
pop.showAsDropDown(v);
}
// openOptionsMenu(); //显示menu菜单项
break;
case R.id.menu_home:
pop.dismiss();
xinwen.wv.loadUrl("http://news.sina.com.cn");
break;
case R.id.menu_refresh:
pop.dismiss();
xinwen.wv.reload(); //刷新
break;
case R.id.menu_search:
pop.dismiss();
inputTitleDialog();
break;
default:
break;
}
/*
mWebView.goBack(); //后退
mWebView.goForward();//前进
mWebView.reload(); //刷新
*/
}
}
</span>
adapter代码:
<span style="font-size:14px;">public class FragmentAdapter extends FragmentPagerAdapter{
private List<Fragment> mFragments;
public Fragment currentFragment;
public FragmentAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
mFragments = fragments;
}
@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
@Override
public int getCount() {
return mFragments.size();
}
//重载FragmentPagerAdapter.setPrimaryItem方法
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object){
currentFragment = (Fragment)object;
super.setPrimaryItem(container, position, object);
}
}</span>
setCurrentItem()方法在这里要说一下,设置为false取消滑动效果,true显示滑动效果,准确来说是通过控制滑动时间来实现,当设置滑动时间为0时,滑动效果就没有了。
我要实现点击底部按钮实现没有滑动效果的切换,刚开始以为是其它问题,没有从setCurrentItem()入手,在哪里啥弄了半天。