HorizontalScrollView
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HorizontalScrollView相关的知识,希望对你有一定的参考价值。
package com.example.yuekao1;
import java.util.ArrayList;
import java.util.List;
import com.baidu.farment.F1;
import com.baidu.farment.Fra;
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.view.Window;
import android.view.WindowManager;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends FragmentActivity implements OnClickListener {
private HorizontalScrollView hsv;
private LinearLayout lin;
String[] str = new String[] { "资讯", "热点", "博客", "推荐" };
private ViewPager vp;
private List<TextView> text_lv;
private int win;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WindowManager wn = getWindowManager();
win = wn.getDefaultDisplay().getWidth();
hsv = (HorizontalScrollView) findViewById(R.id.hsv);
lin = (LinearLayout) findViewById(R.id.lin);
vp = (ViewPager) findViewById(R.id.vp);
getfra();
FragmentManager fm = getSupportFragmentManager();
Fra f = new Fra(fm, getfra());
vp.setAdapter(f);
vp.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
}
});
text_lv = new ArrayList<TextView>();
for (int i = 0; i < str.length; i++) {
TextView text = new TextView(MainActivity.this);
text.setPadding(30, 10, 30, 10);
lin.addView(text);
text.setText(str[i]);
text_lv.add(text);
text.setTag(i);
text.setOnClickListener(this);
}
}
private List<Fragment> getfra() {
List<Fragment> list = new ArrayList<Fragment>();
for (int i = 0; i < str.length; i++) {
list.add(new F1());
}
return list;
}
public void onClick(View v) {
int i = (Integer) v.getTag();
for (int j = 0; j < str.length; j++) {
vp.setCurrentItem(i);
}
}
private void getpage(int p) {
TextView textview = text_lv.get(p);
for (int i = 0; i < text_lv.size(); i++) {
TextView t = text_lv.get(p);
int l = t.getMeasuredWidth();
int k = t.getLeft();
int s = l / 2 + k / 2 - win;
hsv.smoothScrollTo(s, 0);
TextView te = text_lv.get(i);
if (!te.equals(textview)) {
te.setTextColor(Color.BLACK);
} else {
te.setTextColor(Color.GREEN);
}
}
}
}
以上是关于HorizontalScrollView的主要内容,如果未能解决你的问题,请参考以下文章