首先是JiaoyuzixunActivity.java代码部分:
package com.gaoxiaotong.ctone.jiaoyuzixun; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import org.json.JSONArray; import org.json.JSONObject; import com.gaoxiaotong.ctone.entity.Jiaoyuzixun_ViewPage_Adapter; import com.gaoxiaotong.ctone.entity.jianzhizhaopinAdapter; import com.gaoxiaotong.ctone.jianzhizhaopin.JianzhizhaopinActivity; import com.gaoxiaotong.info.Webserinfo; import com.gaoxiaotongctone.MainActivity; import com.gaoxiaotongctone.R; import android.app.Activity; import android.app.ProgressDialog; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; import android.os.Parcelable; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.view.ViewGroup.LayoutParams; import android.view.animation.AnimationSet; import android.view.animation.TranslateAnimation; import android.widget.AdapterViewFlipper; import android.widget.HorizontalScrollView; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.ProgressBar; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; import android.widget.RadioGroup.OnCheckedChangeListener; /** * HorizontalScrollView和ViewPager联动效果 上面为HorizontalScrollView,下面为ViewPager **/ public class JiaoyuzixunActivity extends Activity implements OnCheckedChangeListener { private ProgressDialog dialog; private ProgressBar progressBar; private HashMap<Integer, Integer> hMap; private int pageIndex; private boolean load_pagesize; private int thePage; /** * 设置布局显示属性 */ private LayoutParams mLayoutParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); /** * 设置布局显示目标最大化属性 */ private LayoutParams FFlayoutParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); private LinearLayout loadingLayout; private TextView texview_back; private RadioGroup mRadioGroup; private RadioButton mRadioButton1; private RadioButton mRadioButton2; private RadioButton mRadioButton3; private RadioButton mRadioButton4; private RadioButton mRadioButton5; private RadioButton mRadioButton6; private ImageView mImageView; private float mCurrentCheckedRadioLeft;// 当前被选中的RadioButton距离左侧的距离 private HorizontalScrollView mHorizontalScrollView;// 上面的水平滚动控件 private ViewPager mViewPager; // 下方的可横向拖动的控件 private ArrayList<View> mViews;// 用来存放下方滚动的layout(layout_1,layout_2,layout_3) @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.title_bar_education); iniController(); iniListener(); iniVariable(); mRadioButton1.setChecked(true); mViewPager.setCurrentItem(1); mCurrentCheckedRadioLeft = getCurrentCheckedRadioLeft(); // 返回 texview_back.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(JiaoyuzixunActivity.this, MainActivity.class); startActivity(intent); JiaoyuzixunActivity.this.finish(); } }); // 绑定考研,专升本等等部分 } /** * AsyncTask异步加载部分操作 */ public class MyTaskJianzhi1 extends AsyncTask<String, Void, List<Map<String, Object>>> { private ListView lvCont; private jianzhizhaopinAdapter adapter; private int index; // 判断适配器是否被置空部分 public MyTaskJianzhi1(ListView lvCont, jianzhizhaopinAdapter jzzpAdapter) { super(); this.lvCont = lvCont; if (jzzpAdapter == null) { adapter = new jianzhizhaopinAdapter(JiaoyuzixunActivity.this); } else { adapter = jzzpAdapter; } } // 下载提示框 @Override protected void onPreExecute() { // TODO Auto-generated method stub super.onPreExecute(); dialog = new ProgressDialog(JiaoyuzixunActivity.this); dialog.setTitle("提示"); dialog.setMessage("正在下载,请稍后...."); dialog.show(); } // 操作结束部分 @Override protected void onPostExecute(List<Map<String, Object>> result) { // TODO Auto-generated method stub super.onPostExecute(result); // 绑定考研,专升本部分 String aa = ""; try { if (thePage == 1) { adapter.setData(result); lvCont.setAdapter(adapter); } if (result.size() == 0) { loadingLayout.setVisibility(View.GONE); } adapter.notifyDataSetChanged(); } catch (Exception e) { // TODO: handle exception e.getMessage(); e.printStackTrace(); } dialog.dismiss(); } // 获取数据操作部分 @Override protected List<Map<String, Object>> doInBackground(String... params) { // TODO Auto-generated method stub List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); // 链接网络,获取json,解析数据 try { Webserinfo wsWebserinfo = new Webserinfo(); JSONArray data = new JSONArray(); if (pageIndex == 0) { data = wsWebserinfo.jobsByType("" + Webserinfo.pageSize, "2", "" + thePage); } else if (pageIndex == 1) { data = wsWebserinfo.jobsByType("" + Webserinfo.pageSize, "1", "" + thePage); } else if (pageIndex == 2) { data = wsWebserinfo.jobsByType("" + Webserinfo.pageSize, "3", "" + thePage); } if (data.length() < 10) { load_pagesize = false; } for (int i = 0; i < data.length(); i++) { JSONObject jsonObj = data.getJSONObject(i); HashMap<String, Object> map = new HashMap<String, Object>(); // 迭代输出json的key作为map的key Iterator<String> iterator = jsonObj.keys(); while (iterator.hasNext()) { String key = iterator.next(); Object value = jsonObj.get(key); map.put(key, value); } if (thePage > 1) { adapter.addItem(map); } list.add(map); } } catch (Exception e) { // TODO: handle exception } return list; } } private void iniVariable() { // TODO Auto-generated method stub mViews = new ArrayList<View>(); mViews.add(getLayoutInflater().inflate(R.layout.layout_0, null)); mViews.add(getLayoutInflater().inflate(R.layout.layout_1, null)); mViews.add(getLayoutInflater().inflate(R.layout.layout_2, null)); mViews.add(getLayoutInflater().inflate(R.layout.layout_3, null)); mViews.add(getLayoutInflater().inflate(R.layout.layout_4, null)); mViews.add(getLayoutInflater().inflate(R.layout.layout_5, null)); mViews.add(getLayoutInflater().inflate(R.layout.layout_6, null)); mViews.add(getLayoutInflater().inflate(R.layout.layout_0, null)); mViewPager.setAdapter(new Jiaoyuzixun_ViewPage_Adapter(mViews));// 设置ViewPager的适配器 } /** * RadioGroup点击CheckedChanged监听 */ @Override public void onCheckedChanged(RadioGroup group, int checkedId) { AnimationSet _AnimationSet = new AnimationSet(true); TranslateAnimation _TranslateAnimation; Log.i("zj", "checkedid=" + checkedId); if (checkedId == R.id.btn1) { _TranslateAnimation = new TranslateAnimation( mCurrentCheckedRadioLeft, getResources().getDimension( R.dimen.rdo1), 0f, 0f); _AnimationSet.addAnimation(_TranslateAnimation); _AnimationSet.setFillBefore(false); _AnimationSet.setFillAfter(true); _AnimationSet.setDuration(100); mImageView.startAnimation(_AnimationSet);// 开始上面蓝色横条图片的动画切换 // mImageView.setLayoutParams(_LayoutParams1); mViewPager.setCurrentItem(1);// 让下方ViewPager跟随上面的HorizontalScrollView切换 } else if (checkedId == R.id.btn2) { _TranslateAnimation = new TranslateAnimation( mCurrentCheckedRadioLeft, getResources().getDimension( R.dimen.rdo2), 0f, 0f); _AnimationSet.addAnimation(_TranslateAnimation); _AnimationSet.setFillBefore(false); _AnimationSet.setFillAfter(true); _AnimationSet.setDuration(100); mImageView.startAnimation(_AnimationSet); mViewPager.setCurrentItem(2); } else if (checkedId == R.id.btn3) { _TranslateAnimation = new TranslateAnimation( mCurrentCheckedRadioLeft, getResources().getDimension( R.dimen.rdo3), 0f, 0f); _AnimationSet.addAnimation(_TranslateAnimation); _AnimationSet.setFillBefore(false); _AnimationSet.setFillAfter(true); _AnimationSet.setDuration(100); mImageView.startAnimation(_AnimationSet); mViewPager.setCurrentItem(3); } else if (checkedId == R.id.btn4) { _TranslateAnimation = new TranslateAnimation( mCurrentCheckedRadioLeft, getResources().getDimension( R.dimen.rdo4), 0f, 0f); _AnimationSet.addAnimation(_TranslateAnimation); _AnimationSet.setFillBefore(false); _AnimationSet.setFillAfter(true); _AnimationSet.setDuration(100); mImageView.startAnimation(_AnimationSet); mViewPager.setCurrentItem(4); } else if (checkedId == R.id.btn5) { _TranslateAnimation = new TranslateAnimation( mCurrentCheckedRadioLeft, getResources().getDimension( R.dimen.rdo5), 0f, 0f); _AnimationSet.addAnimation(_TranslateAnimation); _AnimationSet.setFillBefore(false); _AnimationSet.setFillAfter(true); _AnimationSet.setDuration(100); mImageView.startAnimation(_AnimationSet); mViewPager.setCurrentItem(5); } else if (checkedId == R.id.btn6) { _TranslateAnimation = new TranslateAnimation( mCurrentCheckedRadioLeft, getResources().getDimension( R.dimen.rdo6), 0f, 0f); _AnimationSet.addAnimation(_TranslateAnimation); _AnimationSet.setFillBefore(false); _AnimationSet.setFillAfter(true); _AnimationSet.setDuration(100); mImageView.startAnimation(_AnimationSet); mViewPager.setCurrentItem(6); } mCurrentCheckedRadioLeft = getCurrentCheckedRadioLeft();// 更新当前蓝色横条距离左边的距离 mHorizontalScrollView.smoothScrollTo((int) mCurrentCheckedRadioLeft - (int) getResources().getDimension(R.dimen.rdo2), 0); } /** * 获得当前被选中的RadioButton距离左侧的距离 */ private float getCurrentCheckedRadioLeft() { // TODO Auto-generated method stub if (mRadioButton1.isChecked()) { return getResources().getDimension(R.dimen.rdo1); } else if (mRadioButton2.isChecked()) { return getResources().getDimension(R.dimen.rdo2); } else if (mRadioButton3.isChecked()) { return getResources().getDimension(R.dimen.rdo3); } else if (mRadioButton4.isChecked()) { return getResources().getDimension(R.dimen.rdo4); } else if (mRadioButton5.isChecked()) { return getResources().getDimension(R.dimen.rdo5); } else if (mRadioButton6.isChecked()) { return getResources().getDimension(R.dimen.rdo6); } return 0f; } /** * 监听部分操作 */ private void iniListener() { // TODO Auto-generated method stub mRadioGroup.setOnCheckedChangeListener(this); mViewPager.setOnPageChangeListener(new MyPagerOnPageChangeListener()); } private void iniController() { // TODO Auto-generated method stub mRadioGroup = (RadioGroup) findViewById(R.id.radioGroup); mRadioButton1 = (RadioButton) findViewById(R.id.btn1); mRadioButton2 = (RadioButton) findViewById(R.id.btn2); mRadioButton3 = (RadioButton) findViewById(R.id.btn3); mRadioButton4 = (RadioButton) findViewById(R.id.btn4); mRadioButton5 = (RadioButton) findViewById(R.id.btn5); mRadioButton6 = (RadioButton) findViewById(R.id.btn6); mImageView = (ImageView) findViewById(R.id.img1); texview_back = (TextView) findViewById(R.id.back_activity_button); mHorizontalScrollView = (HorizontalScrollView) findViewById(R.id.horizontalScrollView); mViewPager = (ViewPager) findViewById(R.id.pager); } /** * ViewPager的PageChangeListener(页面改变的监听器) */ private class MyPagerOnPageChangeListener implements OnPageChangeListener { @Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO Auto-generated method stub } /** * 滑动ViewPager的时候,让上方的HorizontalScrollView自动切换 */ @Override public void onPageSelected(int position) { // TODO Auto-generated method stub if (position == 0) { mViewPager.setCurrentItem(1); } else if (position == 1) { mRadioButton1.performClick(); } else if (position == 2) { mRadioButton2.performClick(); } else if (position == 3) { mRadioButton3.performClick(); } else if (position == 4) { mRadioButton4.performClick(); } else if (position == 5) { mRadioButton5.performClick(); } else if (position == 6) { mRadioButton6.performClick(); } else if (position == 7) { mViewPager.setCurrentItem(6); } } } // back键 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { Intent intent = new Intent(); intent.setClass(JiaoyuzixunActivity.this, MainActivity.class); JiaoyuzixunActivity.this.startActivity(intent); JiaoyuzixunActivity.this.finish(); } else { return false; } return super.onKeyDown(keyCode, event); } }
dimens.xml部分,控制菜单栏显示条目效果:
<resources> <dimen name="padding_small">8dp</dimen> <dimen name="padding_medium">8dp</dimen> <dimen name="padding_large">16dp</dimen> <dimen name="rdo1">0dp</dimen> <dimen name="rdo2">100dp</dimen> <dimen name="rdo3">200dp</dimen> <dimen name="rdo4">300dp</dimen> <dimen name="rdo5">400dp</dimen> <dimen name="rdo6">500dp</dimen> </resources>
过度页面layout_0.xml部分:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/darker_gray"> </LinearLayout>
显示页面其中一个样式layout_1.xml部分:
<?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:background="@android:color/darker_gray" android:orientation="vertical" > <TextView android:id="@+id/textView_showMessages" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:text="A" android:textColor="#33b5e5" android:textSize="240sp" /> </LinearLayout>
效果图:
Android第六期 - ViewPage与菜单栏本地页面监听滑动效果,布布扣,bubuko.com
时间: 2024-10-07 06:31:16