您的位置:首页 > 移动开发

非常简单的viewpager+fragmen实现类似商城app结构

2015-03-31 14:38 183 查看
先说下思路在一个activity中的主布局加入supportv4架包中的viewpager,viewpager相当于一个listview,在viewpager中我们要放的fragment,对viewpager做监听,实现想要的效果,代码很简单一看就懂,一直上代码。

主类

package com.example.fragtest;

import java.util.ArrayList;

import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import android.os.Build;

public class MainActivity extends FragmentActivity {
private ArrayList<Fragment> list;
private ViewPager viewPager;
private TextView [] tvs=new TextView[4];
private int pos=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager=(ViewPager) findViewById(R.id.viewPager);
tvs[0]=(TextView) findViewById(R.id.tv_f);
tvs[0].setBackgroundColor(0x99999999);
tvs[1]=(TextView) findViewById(R.id.tv_s);
tvs[2]=(TextView) findViewById(R.id.tv_t);
tvs[3]=(TextView) findViewById(R.id.tv_tf);
First mFirst=new First();
Second msSecond=new Second();
third mThird=new third();
fourth mFourth=new fourth();
list=new ArrayList<Fragment>();
list.add(mFirst);
list.add(msSecond);
list.add(mThird);
list.add(mFourth);
viewPager.setAdapter(new MyFragAdapter(getSupportFragmentManager(), this, list));
viewPager.setOnPageChangeListener(new OnPageChangeListener() {

@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
for(int i=0;i<4;i++){
if(i==arg0){
tvs[i].setBackgroundColor(0x99999999);
}else{
tvs[i].setBackgroundColor(0xffffffff);
}
}
}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub

}

@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub

}
});

for(TextView tv:tvs){
tv.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View view) {
// TODO Auto-generated method stub
switch (view.getId()) {
case R.id.tv_f:
pos=0;
break;
case R.id.tv_s:
pos=1;
break;
case R.id.tv_t:
pos=2;
break;
case R.id.tv_tf:
pos=3;
break;
default:
break;
}

for(int i=0;i<4;i++){
if(i==pos){
tvs[i].setBackgroundColor(0x99999999);
}else{
tvs[i].setBackgroundColor(0xffffffff);
}
}
viewPager.setCurrentItem(pos);
}

});
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

}
adapter

package com.example.fragtest;

import java.util.ArrayList;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class MyFragAdapter extends FragmentPagerAdapter{
private Context mContext;
private ArrayList<Fragment> mlist;
public MyFragAdapter(FragmentManager fm,Context context,ArrayList<Fragment> list) {
super(fm);
mContext=context;
if(list!=null){
mlist=list;
}else{
mlist=new ArrayList<Fragment>();
}
// TODO Auto-generated constructor stub
}

@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return mlist.get(arg0);
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return mlist.size();
}

}


四个fragment

package com.example.fragtest;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class First extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view=inflater.inflate(R.layout.view, container, false);
TextView tv=(TextView) view.findViewById(R.id.tv_name);
tv.setText("第一个");
return view;

}

}


package com.example.fragtest;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class Second extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view=inflater.inflate(R.layout.view, container, false);
TextView tv=(TextView) view.findViewById(R.id.tv_name);
tv.setText("第二个");
return view;

}
}


package com.example.fragtest;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class third extends Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view=inflater.inflate(R.layout.view, container, false);
TextView tv=(TextView) view.findViewById(R.id.tv_name);
tv.setText("第三个");
return view;

}
}


package com.example.fragtest;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class fourth extends Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view=inflater.inflate(R.layout.view, container, false);
TextView tv=(TextView) view.findViewById(R.id.tv_name);
tv.setText("第四个");
return view;

}
}


主布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent" >

<android.support.v4.view.ViewPager

android:layout_above="@+id/ll_bottom"

android:id="@+id/viewPager"

android:layout_width="match_parent"

android:layout_height="match_parent"

/>

<LinearLayout

android:id="@+id/ll_bottom"

android:layout_alignParentBottom="true"

android:layout_width="match_parent"

android:orientation="vertical"

android:layout_height="50dp">

<TextView

android:layout_width="match_parent"

android:layout_height="2dp"

android:background="#999999" />

<LinearLayout android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal">

<TextView

android:id="@+id/tv_f"

android:layout_width="0dp"

android:layout_height="match_parent"

android:layout_weight="1"

android:gravity="center"

android:text="首页"

android:textSize="16sp" />

<TextView

android:layout_width="2dp"

android:layout_height="match_parent"

android:background="#999999" />

<TextView

android:id="@+id/tv_s"

android:layout_width="0dp"

android:layout_height="match_parent"

android:layout_weight="1"

android:gravity="center"

android:text="热帖"

android:textSize="16sp" />

<TextView

android:layout_width="2dp"

android:layout_height="match_parent"

android:background="#999999" />

<TextView

android:id="@+id/tv_t"

android:layout_width="0dp"

android:layout_height="match_parent"

android:layout_weight="1"

android:gravity="center"

android:text="活动"

android:textSize="16sp" />

<TextView

android:layout_width="2dp"

android:layout_height="match_parent"

android:background="#999999" />

<TextView

android:id="@+id/tv_tf"

android:layout_width="0dp"

android:layout_height="match_parent"

android:layout_weight="1"

android:gravity="center"

android:text="我"

android:textSize="16sp" />

</LinearLayout>

</LinearLayout>

</RelativeLayout>

fragment布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="25sp"
android:text="第一个"/>
</LinearLayout>
下载地址    http://download.csdn.net/detail/u012303938/8550513[/code] 
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: