package com.jiuqi.njt.ui;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import com.jiuqi.mobile.nigo.comeclose.bean.base.AdminAreaBean;
import com.jiuqi.njt.R;
import com.jiuqi.njt.adapter.WelcomePagerAdapter;
import com.jiuqi.njt.data.MyApp;
import com.jiuqi.njt.data.OptsharepreInterface;
import com.jiuqi.njt.util.Constants;
import com.jiuqi.njt.util.TitleBarUtil;
import com.jiuqi.njt.util.UIUtil;
import com.jiuqi.njt.widget.NjtTitleBar;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
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.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver.OnPreDrawListener;
import android.view.ViewStub;
import android.view.View.OnClickListener;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
/**
* 资讯
*
* @author joe
*
*/
public class NjtNewsTest extends FragmentActivity {
private Context context = this;
private String TAG = this.getClass().getName();
private LinearLayout addStaffLayout;
private ViewPager mPager;
private NjtTitleBar titleBar;
private RadioButton dotRadioButton;
private FrameLayout frameLayout;
private Drawable d ;
private MyApp application;
private OptsharepreInterface sharePre;
public static AdminAreaBean xzqh = null;
private int margin;
private int selectFregment;
private ArrayList<MyItem> collectType = new ArrayList<MyItem>();
private int offset; // 偏移位置
private int curPos = 0;// 记录当前的位置
private List<View> guides = new ArrayList<View>();
private List<Fragment> fragments = new ArrayList<Fragment>();
private final MyItem[] allSeecherType = { new MyItem("头条", false, 6),
new MyItem("政策", false, 3), new MyItem("动态", false, 7),new MyItem("补贴", false, 2)};
private String title = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
doinit();
}
private void doinit() {
initParam();
initWidgets();
initListeners();
initUI();
}
private void initParam() {
application = (MyApp) getApplication();
sharePre = new OptsharepreInterface(getApplicationContext());
for (MyItem item : allSeecherType) {
fragments.add(new NjtNewsList());
collectType.add(item);
}
if (application.getProvince() != null) {
xzqh = new AdminAreaBean();
xzqh.setProvinceName(application.getProvince());
xzqh.setCode(application.getCode());
Log.e(TAG, xzqh.toString());
}
/**
* 默认第一个被选择
*/
upDateUi(0);
}
private void initWidgets() {
setContentView(R.layout.activity_news);
ViewStub rlTitleBar = (ViewStub) findViewById(R.id.titleBarStub);
String rightTitle = "".equals(application.getProvince()) ? "全国"
: application.getProvince();
titleBar = TitleBarUtil.createTitleBar(this, rlTitleBar,allSeecherType[0].getName(),
new OnClickListener() {
@Override
public void onClick(View v) {
NjtNewsTest.this.finish();
}
}, rightTitle, new OnClickListener() {
@Override
public void onClick(View v) {
UIUtil.requestNewXzqh(NjtNewsTest.this);
}
});
addStaffLayout = (LinearLayout) findViewById(R.id.addStaffLayout);
dotRadioButton = (RadioButton) findViewById(R.id.cur_dot);
frameLayout = (FrameLayout)findViewById(R.id.framelayout);
mPager = (ViewPager) findViewById(R.id.contentPager);
// dotRadioButton.setVisibility(View.GONE);
d = context.getResources().getDrawable(
R.drawable.xuanzhong);
}
private void initListeners() {
}
private void initUI() {
createTypeLayout();
WelcomePagerAdapter adapter = new WelcomePagerAdapter(guides);
mPager.setAdapter(adapter);
mPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
titleBar.getTvTitle().setText(allSeecherType[arg0].getName());
moveCursorTo(arg0);
upDateUi(arg0);
addStaffLayout.removeAllViews();
createTypeLayout();
curPos = arg0;
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
Log.e(TAG, arg0 + "===---===" + arg1 + "===--=====" + arg2);
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
dotRadioButton.getViewTreeObserver().addOnPreDrawListener(
new OnPreDrawListener() {
public boolean onPreDraw() {
offset = dotRadioButton.getWidth();
return true;
}
});
final FragmentManager fragmentManager = getSupportFragmentManager();
mPager.setAdapter(new FragmentPagerAdapter(fragmentManager) {
@Override
public int getCount() {
return fragments.size();
}
@Override
public Fragment getItem(int position) {
selectFregment = position;
FragmentTransaction ft = fragmentManager.beginTransaction();
Fragment f = fragments.get(position);
Bundle b = new Bundle();
b.putInt("TYPE", allSeecherType[position].getType());
f.setArguments(b);
if (f.isAdded()) {
f.onResume();
}
ft.commit();
return f;
}
});
}
/**
* 生成标题下面可滑动的导航栏 RadioGroup 加 RadioButton
*/
@SuppressLint({ "ResourceAsColor", "NewApi" })
private void createTypeLayout() {
int width = com.jiuqi.njt.util.UIUtil.getScreenWidth(NjtNewsTest.this);
int heigh = com.jiuqi.njt.util.UIUtil.getScreenHeight(NjtNewsTest.this);
margin = (int) (width * 0.02);
frameLayout.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,(int)(heigh*(0.08))));
RadioGroup group = new RadioGroup(NjtNewsTest.this);
group.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
group.setGravity(Gravity.CENTER_HORIZONTAL);
group.setOrientation(Gravity.HORIZONTAL_GRAVITY_MASK);
RadioGroup.LayoutParams params = new RadioGroup.LayoutParams((width/collectType.size()),LayoutParams.MATCH_PARENT);
params.setMargins(0, margin, 0, margin/2);
FrameLayout.LayoutParams newParams = new FrameLayout.LayoutParams((width/collectType.size()),LayoutParams.MATCH_PARENT);
// newParams.gravity = Gravity.LEFT;
newParams.setMargins(0, margin, 0, margin);
dotRadioButton.setLayoutParams(newParams);
// dotRadioButton.setPadding(margin, 0, margin, 0);
dotRadioButton.setGravity(Gravity.CENTER);
dotRadioButton.setBackgroundDrawable(d);
for (int i = 0; i < collectType.size(); i++) {
Button radioButton = new Button(NjtNewsTest.this);
// radioButton.setText(" " + collectType.get(i).getName() + " ");
radioButton.setText(collectType.get(i).getName());
radioButton.setSelected(collectType.get(i).isSelected());
// radioButton.setButtonDrawable(R.color.more_tv_normal_color);
// radioButton.setButtonDrawable(android.R.color.transparent);
radioButton.setBackground(null);
radioButton.setLayoutParams(params);
radioButton.setGravity(Gravity.CENTER);
radioButton.setTextSize(android.util.TypedValue.COMPLEX_UNIT_PX,
(int) (width * 0.05));
radioButton.setTextColor(R.color.white);
radioButton.setOnClickListener(new TypeButtonOnClickLiner(i));
group.addView(radioButton);
}
addStaffLayout.addView(group);
}
/**
*
* @Description: 移动指针到相邻的位置
* @Title: moveCursorTo
* @param position
* 指针的索引值
*/
private void moveCursorTo(int position) {
// 使用绝对位置
TranslateAnimation anim = new TranslateAnimation(offset * curPos,
offset * position, 0, 0);
anim.setDuration(300);
anim.setFillAfter(true);
dotRadioButton.startAnimation(anim);
dotRadioButton.setBackgroundDrawable(d);
}
/**
* 收藏类型点击事件
*
* @author joe
*
*/
private class TypeButtonOnClickLiner implements OnClickListener {
private int index;
public TypeButtonOnClickLiner(int index) {
super();
this.index = index;
}
@Override
public void onClick(View v) {
upDateUi(index);
// 刷新页面类型数据
addStaffLayout.removeAllViews();
createTypeLayout();
mPager.setCurrentItem(index);
}
}
/**
* 更新页面
*
* @param index
* 选中的下标
*/
private void upDateUi(int index) {
collectType.clear();
collectType = new ArrayList<NjtNewsTest.MyItem>();
for (MyItem iterable_element : allSeecherType) {
if (iterable_element.equals(allSeecherType[index])) {
collectType.add(new MyItem(iterable_element.getName(), true));
} else {
collectType.add(new MyItem(iterable_element.getName(), false));
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.e(TAG,"onActivityResult");
if (requestCode == Constants.REQUEST_XZQH_CODE) {
if (resultCode == RESULT_OK) {
AdminAreaBean xzqh = (AdminAreaBean) data
.getSerializableExtra(Constants.PARAM_XZQH);
this.xzqh = xzqh;
titleBar.getBtnRight().setText(xzqh.getFullName());
}
}
}
/**
* 定义的类型对象
*
* @author joe
*
*/
class MyItem implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
/*
* 类型名称
*/
private String name;
/**
* 是否被选择
*/
private boolean selected;
/**
* 备用标识
*/
private String flag;
/**
* 类型标示
*/
private int type;
/**
*
* @param name
* 类型名称
* @param selected
*/
public MyItem(String name, boolean selected) {
super();
this.name = name;
this.selected = selected;
}
/**
*
* @param name
* 类型名称
* @param selected
* 是否选中状态
* @param type
* 类型标示("头条", 6)); ("政策", 3)); ("动态", 7));
*/
public MyItem(String name, boolean selected, int type) {
super();
this.name = name;
this.selected = selected;
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
public String getFlag() {
return flag;
}
public void setFlag(String flag) {
this.flag = flag;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
@Override
public String toString() {
return "MyItem [name=" + name + ", selected=" + selected
+ ", flag=" + flag + "]";
}
}
}