首页 > 代码库 > Android实现网易新闻客户端效果
Android实现网易新闻客户端效果
tabhost_tabwidget.xml
[html] view plaincopyprint?
- <?xml version="1.0" encoding="utf-8"?>
- <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@android:id/tabhost"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <TabWidget
- android:id="@android:id/tabs"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal" />
- <FrameLayout
- android:id="@android:id/tabcontent"
- android:layout_width="match_parent"
- android:layout_height="match_parent"/>
- <!-- <FrameLayout
- android:id="@+id/real_tabcontent"
- android:layout_width="match_parent"
- android:layout_height="0dp"
- android:layout_weight="0"/> -->
- </LinearLayout>
- </TabHost>
[java] view plaincopyprint?
- package com.jackie;
- import android.app.Activity;
- import android.app.Fragment;
- import android.app.FragmentTransaction;
- import android.content.Context;
- import android.os.Bundle;
- import android.text.TextUtils;
- import android.view.View;
- import android.widget.ImageView;
- import android.widget.TabHost;
- import android.widget.TabHost.OnTabChangeListener;
- import android.widget.TabWidget;
- import com.jackie.R;
- /*
- * ____________
- * | |
- * | |
- * |tabcontent|
- * |显示标签页 |
- * |__________| 区域是由tabhost控制, 显示内容的区域为tabcontent
- * tab标签 组合 fragment
- */
- public class TestTabWidget extends Activity {
- TabHost tabHost;
- Fragment fragment;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.tabhost_tabwidget);
- tabHost = (TabHost) findViewById(android.R.id.tabhost);
- tabHost.setup();
- tabHost.setOnTabChangedListener(new OnTabChangeListener() {
- @Override
- public void onTabChanged(String tabId) {
- System.out.println("current tabid=" + tabId);
- FragmentTransaction ft = getFragmentManager().beginTransaction();
- if (TextUtils.equals("first", tabId)) {
- //add/replace fragment first
- fragment= new Fragment1();
- System.out.println("load frament1");
- } else if (TextUtils.equals("second", tabId)) {
- //add/replace fragment second
- fragment = new Fragment2();
- System.out.println("load frament2");
- } else if (TextUtils.equals("third", tabId)) {
- //add/replace fragment third
- fragment = new Fragment3();
- System.out.println("load frament3");
- }
- ft.replace(android.R.id.tabcontent, fragment, "fragment");
- ft.commit();
- }
- });
- tabHost.addTab(tabHost.newTabSpec("first").setIndicator("First")
- .setContent(new DummyTabFactory(this)));
- tabHost.addTab(tabHost.newTabSpec("second").setIndicator("Second")//setIndicator 设置标签样式
- .setContent(new DummyTabFactory(this))); //setContent 点击标签后触发
- tabHost.addTab(tabHost.newTabSpec("third").setIndicator("Third")
- .setContent(new DummyTabFactory(this)));
- }
- static class DummyTabFactory implements TabHost.TabContentFactory {
- private Context context;
- public DummyTabFactory(Context ctx) {
- this.context = ctx;
- }
- @Override
- public View createTabContent(String tag) {//创建宽高均为0的view
- View v = new ImageView(context);
- v.setMinimumWidth(0);
- v.setMinimumHeight(0);
- return v;
- }
- }
- }
效果
当然,现在还只能通过点击上面的Tab来切换相应的Fragment,真正的网易菜单还可以左右滑动来切换的,这时候只需要为每个Fragment添加滑动事件实现相应的逻辑即可。
Android实现网易新闻客户端效果
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。