pager和fragment绑定使用的完整代码。
Fragment的代码,没什么特别就只是一个界面而已。
但是这里要实现的方法和activity有点不同是实现OnCreateView(activity是实现OnCreate)
package com.zsz.develop.wisdomcommunity.main;
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 com.zsz.develop.wisdomcommunity.R;
/**
* Created by shengzhong on 2015/8/28.
*/
public class FragmentDoor extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.door_view_pager,null);
return view;
}
}
pager页面
- 1.定义好两个list一个装view、一个装fragment
- 2.对于pager这种需要后期添加的都需要对他设置adapter
- 3.pager是针对fragment设置的adapter所以他就要使用fragment的适配器。中间要加个管理器。
package com.zsz.develop.wisdomcommunity.main;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.zsz.develop.wisdomcommunity.R;
import com.zsz.develop.wisdomcommunity.function.StairsMouthActivity;
import com.zsz.develop.wisdomcommunity.myOneLine.MyBillActivity;
import com.zsz.develop.wisdomcommunity.myOneLine.MyHomeActivity;
import com.zsz.develop.wisdomcommunity.myOneLine.MyInfoActivity;
import com.zsz.develop.wisdomcommunity.myOneLine.MyMonitoringActivity;
import com.zsz.develop.wisdomcommunity.myTwoLine.MyExpressActivity;
import com.zsz.develop.wisdomcommunity.myTwoLine.MyInterflowMainActivity;
import com.zsz.develop.wisdomcommunity.myTwoLine.MyNeighbourActivity;
import java.util.ArrayList;
import java.util.List;
public class MainViewPagerActivity extends AppCompatActivity implements View.OnClickListener {
private ViewPager pager = null;
private List<Fragment> fragmentlist=new ArrayList<>();
ArrayList<View> viewContainter = new ArrayList<View>();
private LinearLayout llCloud,llDoor,llMy;
private View myView;
public String TAG = "tag";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_view_pager);
pager= (ViewPager) findViewById(R.id.viewPager);
llCloud= (LinearLayout) findViewById(R.id.llCloud);
llDoor= (LinearLayout) findViewById(R.id.llDoor);
llMy= (LinearLayout) findViewById(R.id.llMy);
llMy.setOnClickListener(this);
llDoor.setOnClickListener(this);
llCloud.setOnClickListener(this);
fragmentlist.add(new FragmentCloud());
fragmentlist.add(new FragmentDoor());
fragmentlist.add(new FragmentMy());
myView= LayoutInflater.from(this).inflate(R.layout.my_view_pager,null);
View doorView= LayoutInflater.from(this).inflate(R.layout.door_view_pager,null);
View cloudView= LayoutInflater.from(this).inflate(R.layout.cloud_view_pager,null);
viewContainter.add(cloudView);
viewContainter.add(doorView);
viewContainter.add(myView);
pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public int getCount() {
return fragmentlist.size();
}
@Override
public Fragment getItem(int position) {
return fragmentlist.get(position);
}
});
pager.setCurrentItem(1);
pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrollStateChanged(int arg0) {
Log.d(TAG, "--------changed:" + arg0);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
Log.d(TAG, "-------scrolled arg0:" + arg0);
Log.d(TAG, "-------scrolled arg1:" + arg1);
Log.d(TAG, "-------scrolled arg2:" + arg2);
}
//选择的位置
@Override
public void onPageSelected(int position) {
Log.d(TAG, "------selected:" + position);
}
});
}
@Override
public void onClick(View view) {
int id=view.getId();
switch (id){
case R.id.llCloud:
pager.setCurrentItem(0);
break;
case R.id.llDoor:
pager.setCurrentItem(1);
break;
case R.id.llMy:
pager.setCurrentItem(2);
break;
}
}
}
对于viewpager的layout
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
>
</android.support.v4.view.ViewPager>
最简单的pager和fragment的结合使用
package com.zsz.develop.exercisedemo;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import java.util.ArrayList;
public class ViewPagerActivity extends AppCompatActivity {
private ViewPager viewPager;
private ArrayList<Fragment> fragmentArrayList=new ArrayList<>();
private ArrayList<View> viewArrayList=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pager);
viewPager= (ViewPager) findViewById(R.id.vp);
fragmentArrayList.add(new MyFragment());
fragmentArrayList.add(new MyFragment02());
fragmentArrayList.add(new MyFragment());
// viewArrayList.add()
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int i) {
return fragmentArrayList.get(i);
}
@Override
public int getCount() {
return fragmentArrayList.size();
}
});
}
}