Android实现网易新闻客户端效果
来源:互联网 发布:淘宝内衣真人秀怎么看 编辑:程序博客网 时间:2024/05/26 20:23
下面来简单实现一下网易新闻客户端左右切换的效果,当然实际项目上肯定不能这样写,还有很多需要优化的地方。
activity_main.xml
- <?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实现逻辑
package com.jackie.neteasedemo;import android.os.Bundle;import android.app.Activity;import android.app.Fragment;import android.app.FragmentTransaction;import android.content.Context;import android.text.TextUtils;import android.view.MotionEvent;import android.view.View;import android.view.ViewConfiguration;import android.widget.ImageView;import android.widget.TabHost;import android.widget.TabHost.OnTabChangeListener;public class MainActivity extends Activity {private TabHost mTabHost;private Fragment mFragment;private float startX = 0;private float endX = 0;private int touchSlop;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mTabHost = (TabHost) findViewById(android.R.id.tabhost);mTabHost.setup();mTabHost.setOnTabChangedListener(new OnTabChangeListener() {@Overridepublic void onTabChanged(String tabId) {System.out.println("current tabid=" + tabId);FragmentTransaction ft = getFragmentManager().beginTransaction();if (TextUtils.equals("first", tabId)) {// add/replace fragment firstmFragment = new Fragment1();System.out.println("Load Frament1");} else if (TextUtils.equals("second", tabId)) {// add/replace fragment secondmFragment = new Fragment2();System.out.println("Load Frament2");} else if (TextUtils.equals("third", tabId)) {// add/replace fragment thirdmFragment = new Fragment3();System.out.println("Load Frament3");}ft.replace(android.R.id.tabcontent, mFragment);ft.commit();}});mTabHost.addTab(mTabHost.newTabSpec("first").setIndicator("First").setContent(new DummyTabFactory(this)));mTabHost.addTab(mTabHost.newTabSpec("second").setIndicator("Second").setContent(new DummyTabFactory(this)));mTabHost.addTab(mTabHost.newTabSpec("third").setIndicator("Third").setContent(new DummyTabFactory(this)));touchSlop = ViewConfiguration.get(this).getScaledTouchSlop(); //默认是8,能够根据不同分辨率适配而改变}@Overridepublic boolean onTouchEvent(MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:startX = event.getX();break;case MotionEvent.ACTION_UP:endX = event.getX();break;default:break;}if (endX - startX > touchSlop) { // 向右滑动if (TextUtils.equals(mTabHost.getCurrentTabTag(), "third")) {mFragment = new Fragment2();mTabHost.setCurrentTabByTag("second");} else if (TextUtils.equals(mTabHost.getCurrentTabTag(), "second")) {mFragment = new Fragment1();mTabHost.setCurrentTabByTag("first");}FragmentTransaction ft = getFragmentManager().beginTransaction();ft.replace(android.R.id.tabcontent, mFragment);ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);ft.commit();} else if (Math.abs(endX - startX) > touchSlop) { // 向左滑动if (TextUtils.equals(mTabHost.getCurrentTabTag(), "first")) {mFragment = new Fragment2();mTabHost.setCurrentTabByTag("second");} else if (TextUtils.equals(mTabHost.getCurrentTabTag(), "second")) {mFragment = new Fragment3();mTabHost.setCurrentTabByTag("third");}FragmentTransaction ft = getFragmentManager().beginTransaction();ft.replace(android.R.id.tabcontent, mFragment);ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);ft.commit();}return super.onTouchEvent(event);}private class DummyTabFactory implements TabHost.TabContentFactory {private Context context;public DummyTabFactory(Context ctx) {this.context = ctx;}@Overridepublic View createTabContent(String tag) {// 创建宽高均为0的viewView v = new ImageView(context);v.setMinimumWidth(0);v.setMinimumHeight(0);return v;}}}
效果如下:
0 2
- Android实现网易新闻客户端效果
- 模仿android网易新闻客户端左右动画效果
- 模拟实现网易新闻客户端
- 模拟实现网易新闻客户端
- android 仿网易新闻客户端
- Android实现网易新闻客户端侧滑菜单(一)
- Android实现网易新闻客户端侧滑菜单(二)
- Android实现网易新闻客户端侧滑菜单
- Android网易新闻评论盖楼效果的实现
- Android网易新闻评论盖楼效果的实现
- 【Android】最新主流新闻app功能实现。仿网易,搜狐等新闻客户端实现展示
- Android之类似网易新闻客户端首页新闻列表实现思路
- 使用ViewPagerIndicator结合ViewPager实现网易新闻客户端的Tab标签效果
- ActionBar+DrawerLayout实现网易新闻客户端首页
- DrawerLayout实现网易新闻抽屉效果
- PagerSlidingTabStrip实现网易新闻导航栏效果
- 实现网易新闻的tab切换效果
- Viewpager实现类似网易新闻的效果
- Linux Socket编程
- mfc 获取局域网Ip
- wireshark抓包图解 TCP三次握手/四次挥手详解
- DoTween(HOTween V2) 教程
- MongoDB之Replica Sets + Sharding
- Android实现网易新闻客户端效果
- NeoAxis学习笔记(2)
- web架构师必看:一步一步教你如何做架构
- 高效频繁模式挖掘算法PrePost和FIN的C++源码
- WindowManager.LayoutParams详解
- BZOJ 1800 AHOI2009 fly 飞行棋 暴力
- 单片机小白学步系列(二十一) IO口:基本的LED和按键操作
- 反编译工具Depends---dll和exe反编译工具
- 修改const指针所指向的值