package teamhgl.xinwensudu;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;
import java.util.ArrayList;
import java.util.List;
import teamhgl.xinwensudu.fragment.xiaocry_Fragment;
/**
* A simple {@link Fragment} subclass.
*/
@ContentView(R.layout.fragment_shipin)
public class ShiPin_Fragment extends Fragment {
@ViewInject(R.id.shipin_linearlayout)
private LinearLayout mshipin_layout;
@ViewInject(R.id.shipin_viewpager)
private ViewPager mshipin_pager;
private List<Fragment> mfragments;
private ShiPin_Viewpager_Adapter mviewpager_adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = x.view().inject(this, inflater, container);
mfragments = new ArrayList<Fragment>();
xiaocry_Fragment xiaocry_fragment = new xiaocry_Fragment();
mfragments.add(xiaocry_fragment);
xiaocry_Fragment xiaocry_fragment1 = new xiaocry_Fragment();
mfragments.add(xiaocry_fragment1);
xiaocry_Fragment xiaocry_fragment2 = new xiaocry_Fragment();
mfragments.add(xiaocry_fragment2);
xiaocry_Fragment xiaocry_fragment3 = new xiaocry_Fragment();
mfragments.add(xiaocry_fragment3);
mviewpager_adapter = new ShiPin_Viewpager_Adapter(getFragmentManager());
mshipin_pager.setAdapter(mviewpager_adapter);
intitTab();
return rootView;
}
//ViewPager+fragment+tab
private void intitTab() {
mshipin_layout.getChildAt(0).setSelected(true);
//给tab添加监听
for (int i = 0; i < 4; i++) {
final View textview = mshipin_layout.getChildAt(i);
textview.setTag(i);
textview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = (int) textview.getTag();
mshipin_pager.setCurrentItem(position);
}
});
}
//滑动viewpager的监听
mshipin_pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
for (int i = 0; i < 4; i++) {
View childview = mshipin_layout.getChildAt(i);
if (i == position) {
childview.setSelected(true);
} else {
childview.setSelected(false);
}
}
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
class ShiPin_Viewpager_Adapter extends FragmentPagerAdapter{
public ShiPin_Viewpager_Adapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return mfragments.get(position);
}
@Override
public int getCount() {
return mfragments.size();
}
}
}