这几天项目中涉及到了很多关于viewpager和fragment的技术,虽然知道是使用viewpager+viewpagerindicator的方式,但是由于有很长时间没用了,所以写了一个demo,总结一下,希望以后可以随时复习。
Activity布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.viewpagerindicator.TabPageIndicator
android:id="@+id/tab"
android:layout_width="fill_parent"
android:background="#FFFF00"
android:layout_height="wrap_content"/>
<android.support.v4.view.ViewPager
android:id="@+id/info_pager"
android:layout_width="fill_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
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/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Activity中的代码:
package com.example.chentao.viewpagerdemo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import com.viewpagerindicator.TabPageIndicator;
public class MainActivity extends FragmentActivity {
private ViewPager viewpager;
private TabPageIndicator tabpageindicator;
public static String[] contents={"分组","编号","排序"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewpager= (ViewPager) findViewById(R.id.info_pager);
tabpageindicator= (TabPageIndicator) findViewById(R.id.tab);
MyAdapter adapter=new MyAdapter(getSupportFragmentManager());
viewpager.setAdapter(adapter);
viewpager.setOffscreenPageLimit(0);//取消预加载
tabpageindicator.setViewPager(viewpager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
class MyAdapter extends FragmentPagerAdapter
{
public MyAdapter(FragmentManager fm)
{
super(fm);
}
@Override
public Fragment getItem(int position) {
FragmentDemo fd=new FragmentDemo(position);
return fd;
}
@Override
public int getCount() {
return contents.length;
}
@Override
public CharSequence getPageTitle(int position) {
return contents[position];
}
}
}
Fragment中的代码:
package com.example.chentao.viewpagerdemo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* Created by chentao on 2015/3/4.
*/
public class FragmentDemo extends Fragment {
private TextView text;
public int position;
public FragmentDemo(int position)
{
this.position=position;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=View.inflate(this.getActivity(),R.layout.fragment_demo1,null);
text= (TextView) view.findViewById(R.id.text);
text.setText(MainActivity.contents[position]);
return view;
}
@Override
public void onPause() {
super.onPause();
}
}