Android实现网易新闻客户端效果


下面来简单实现一下网易新闻客户端左右切换的效果,当然实际项目上肯定不能这样写,还有很多需要优化的地方。

activity_main.xml

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@android:id/tabhost"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent" >  
  6.   
  7.     <LinearLayout  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="match_parent"  
  10.         android:orientation="vertical" >  
  11.   
  12.         <TabWidget  
  13.             android:id="@android:id/tabs"  
  14.             android:layout_width="match_parent"  
  15.             android:layout_height="wrap_content"  
  16.             android:orientation="horizontal" />  
  17.   
  18.         <FrameLayout  
  19.             android:id="@android:id/tabcontent"  
  20.             android:layout_width="match_parent"  
  21.             android:layout_height="match_parent"/>  
  22.       <!--   <FrameLayout   
  23.             android:id="@+id/real_tabcontent"  
  24.             android:layout_width="match_parent"  
  25.             android:layout_height="0dp"  
  26.             android:layout_weight="0"/> -->  
  27.     </LinearLayout>  
  28.   
  29. </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;

@Override
protected 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() {

@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
mFragment = new Fragment1();
System.out.println("Load Frament1");
} else if (TextUtils.equals("second", tabId)) {
// add/replace fragment second
mFragment = new Fragment2();
System.out.println("Load Frament2");
} else if (TextUtils.equals("third", tabId)) {
// add/replace fragment third
mFragment = 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,能够根据不同分辨率适配而改变
}

@Override
public 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;
}

@Override
public View createTabContent(String tag) {// 创建宽高均为0的view
View v = new ImageView(context);
v.setMinimumWidth(0);
v.setMinimumHeight(0);
return v;
}
}
}


效果如下:


 
 



注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com