21 viewPager--- hzScrollView ----llContainer

时间:2021-07-30 09:04:01

结构:

21 viewPager--- hzScrollView ----llContainer

MainActivity.java

package com.qf.day21_hsviewpagerfragment_demo5;

import java.util.ArrayList;
import java.util.List; import android.graphics.Color;
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.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView; public class MainActivity extends FragmentActivity { private ViewPager viewPager;
private HorizontalScrollView hzScrollView;
private LinearLayout llContainer; private String[] titles ; private List<Fragment> list = new ArrayList<Fragment>(); private TextView[] tvs; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //初始化View
initView();
//初始化VIewPager
initViewPager();
//初始化导航书签
initTab();
} public void initTab(){
tvs = new TextView[titles.length];
for(int i=0;i<titles.length;i++){ TextView tv = new TextView(getApplicationContext());
tv.setText(" "+titles[i]);
tv.setTextSize(20);
tv.setTextColor(Color.BLUE);
tv.setGravity(Gravity.CENTER);
tv.setEnabled(true); tv.setTag(i);
tv.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
viewPager.setCurrentItem((Integer)v.getTag());
}
});
tvs[i] = tv;
llContainer.addView(tv); tvs[0].setEnabled(false);
tvs[0].setTextColor(Color.RED); } } public void initViewPager(){
//获取标题数据
titles = getResources().getStringArray(R.array.titles); for(int i=0;i<titles.length;i++){
MyFragment myFragment = MyFragment.getInstance(i+1);
list.add(myFragment);
} viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
viewPager.setOnPageChangeListener(new OnPageChangeListener() { @Override
public void onPageSelected(int pos) {
//ViewPager 绑定 书签
setCurrentView(pos);
} @Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub } @Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub }
});
} //滑动ViewPager时 书签也到相应的位置 屏幕中心位置
public void setCurrentView(int pos){ for(int i=0;i<titles.length;i++){
tvs[i].setEnabled(true);
tvs[i].setTextColor(Color.BLUE);
}
tvs[pos].setEnabled(false);
tvs[pos].setTextColor(Color.RED); int screenWidth = getResources().getDisplayMetrics().widthPixels; //假设当前 pos书签 在中间位置
int leftlength = (screenWidth-tvs[pos].getWidth())/2; //获取偏移量
int offsetLength = tvs[pos].getLeft()-leftlength; //hzScrollView 进行移动 参数1:水平偏移量 参数2:垂直偏移量
hzScrollView.smoothScrollTo(offsetLength, 0); } public void initView() {
viewPager = (ViewPager) findViewById(R.id.viewPager);
hzScrollView = (HorizontalScrollView) findViewById(R.id.hzScrollView);
llContainer = (LinearLayout) findViewById(R.id.ll_container);
} public class MyAdapter extends FragmentPagerAdapter{ public MyAdapter(FragmentManager fm) {
super(fm);
} @Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
} @Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
} } }

MyFragment.java

package com.qf.day21_hsviewpagerfragment_demo5;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleAdapter;
import android.widget.TextView; public class MyFragment extends ListFragment { private TextView tvShow; private int index =0; public static MyFragment getInstance(int index){
MyFragment myFragment = new MyFragment();
Bundle args = new Bundle();
args.putInt("index", index);
myFragment.setArguments(args);
return myFragment; } @Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity); Bundle bundle = getArguments();
if(bundle!=null){
index = bundle.getInt("index");
}
} @Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.fragment_layout, container, false); tvShow = (TextView) v.findViewById(R.id.tv_show);
return v;
} @Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
switch (index) {
case 1:
tvShow.setText("您点击了书签1"); break;
case 2:
tvShow.setText("您点击了书签2");
break;
case 3:
tvShow.setText("您点击了书签3");
break;
case 4:
tvShow.setText("您点击了书签4");
break;
default:
break;
} SimpleAdapter adapter = new SimpleAdapter(
getActivity(),
loadNetWorkData(),
R.layout.item,
new String[]{"icon","title","content"},
new int[]{R.id.iv_item,R.id.title_item,R.id.content_item}); setListAdapter(adapter); } /**
* 假设从网络获取数据
* @return
*/
private List<Map<String,Object>> loadNetWorkData(){ List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
for(int i=0;i<20;i++){
Map<String, Object> map = new HashMap<String, Object>();
map.put("icon", R.drawable.ic_launcher);
map.put("title", "郭XX大战曹XXX"+i+"tab"+index);
map.put("content", "降龙十八掌赢"+i+"tab"+index);
list.add(map); } return list; } @Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
} @Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
} @Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
} @Override
public void onStop() {
// TODO Auto-generated method stub
super.onStop();
} @Override
public void onDestroyView() {
// TODO Auto-generated method stub
super.onDestroyView();
Log.e("AAA", "==onDestroyView=>");
} @Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.e("AAA", "==onDestroy=>");
} @Override
public void onDetach() {
// TODO Auto-generated method stub
super.onDetach();
Log.e("AAA", "==onDetach=>");
} //show() hide() 走当前的方法
@Override
public void onHiddenChanged(boolean hidden) {
// TODO Auto-generated method stub
super.onHiddenChanged(hidden); Log.e("AAA", "==onHiddenChanged=>");
} }

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" > <HorizontalScrollView
android:id="@+id/hzScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
>
<LinearLayout
android:id="@+id/ll_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
></LinearLayout> </HorizontalScrollView>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#f00"
/>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
></android.support.v4.view.ViewPager> </LinearLayout>

fragment_layout.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:orientation="vertical" > <TextView
android:id="@+id/tv_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#f00"
android:text="AAA"
/>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ListView> </LinearLayout>

item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="@+id/iv_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
/>
<TextView
android:id="@+id/title_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/iv_item"
android:text="name"
/>
<TextView
android:id="@+id/content_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/iv_item"
android:text="aaa"
android:layout_alignBottom="@id/iv_item"
/> </RelativeLayout>