horizontalscrollview+textview簡單版

/Hor/res/layout/activity_main.xml  佈局java

<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">

<HorizontalScrollView
android:id="@+id/hor"
android:layout_width="match_parent"
android:layout_height="50dp"

>
<LinearLayout
android:id="@+id/lin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></LinearLayout>

</HorizontalScrollView>android

<android.support.v4.view.ViewPager
android:id="@+id/viewp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</android.support.v4.view.ViewPager>app

</LinearLayout>ide

 
   

/Hor/res/layout/f1.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/t"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sdsss"
/>this

</LinearLayout>xml

   

com.bawei.hor.MainActivity  主頁utf-8

package com.bawei.hor;get

import java.util.ArrayList;
import java.util.List;it

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class MainActivity extends FragmentActivity {
private String[] name=new String[]{"1","2","3","4","5","6","7"};
private List<Fragment> list;
private List<TextView> t_list;
private HorizontalScrollView horizontalScrollView;
private int width;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

horizontalScrollView = (HorizontalScrollView) findViewById(R.id.hor);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.lin);
final ViewPager viewPager= (ViewPager) findViewById(R.id.viewp);
//獲取屏幕寬度
width = getWindowManager().getDefaultDisplay().getWidth();
//加載viewpager頁面
steData();
//viewpager適配
FragmentManager fm = getSupportFragmentManager();
viewPager.setAdapter(new MyFragment(fm,list));
//viewpager監聽
viewPager.setOnPageChangeListener(new OnPageChangeListener() {

@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
//判斷滑到那頁
getpage(arg0);
}

@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

}
});
//最上面導航
t_list = new ArrayList<TextView>();
for(int i=0;i<name.length;i++){
//每一個textview的寬度
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(width/3,LayoutParams.WRAP_CONTENT);

TextView textView=new TextView(this);
//距離上下左右
textView.setPadding(20, 20, 20, 20);
//賦值
textView.setText(name[i]);
//添加集合
t_list.add(textView);
//綁定
textView.setTag(i);
//將textview放到佈局裏
linearLayout.addView(textView, i,params);
//textview 監聽 點哪一個滑到那頁
textView.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int m = (Integer) v.getTag();
viewPager.setCurrentItem(m);
}
});
}
}
//viewpager監聽裏的
private void getpage(int arg0) {
// TODO Auto-generated method stub
//當前顯示textview頁
TextView textView1 = t_list.get(arg0);
for (int i = 0; i < t_list.size(); i++) {
//將當前顯示的textview放到中間
TextView text=t_list.get(arg0);
int k = text.getMeasuredWidth();
int l = text.getLeft();
int s = k / 2 + l - width / 2;
horizontalScrollView.smoothScrollTo(s, 0);
//獲得全部textview
TextView t = t_list.get(i);
//給當前顯示頁面的textview頁變色
if (!t.equals(textView1)) {

t.setTextColor(Color.BLACK);
} else {
t.setTextColor(Color.RED);

}


}

}
//new page頁
private void steData() {
list = new ArrayList<Fragment>();
for(int i=0;i<name.length;i++){
//傳值第幾頁
Fragment1 f1=new Fragment1(name[i]);
list.add(f1);
}
}



}

 
   

com.bawei.hor.MyFragment  viewpager適配

 

package com.bawei.hor;

import java.util.List;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.view.View;

public class MyFragment extends FragmentPagerAdapter {
List<Fragment> list;
public MyFragment(FragmentManager fm, List<Fragment> list) {
super(fm);
// TODO Auto-generated constructor stub
this.list=list;
}

@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();
}


}

   

com.bawei.hor.Fragment1   fragment頁

package com.bawei.hor;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class Fragment1 extends Fragment{
String name;
public Fragment1(String name) {
// TODO Auto-generated constructor stub
this.name=name;
}

@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View view=inflater.inflate(R.layout.f1, null); TextView textView= (TextView) view.findViewById(R.id.t); textView.setText(name); return view;}}

相關文章
相關標籤/搜索