我希望在TextView
的帮助下控制SeekBar
的大小,即当TextView
向右拖动时SeekBar
尺寸增加,在拖动时减小TextView
尺寸向左移动。我可以在简单的活动UI中显示TextView
时执行此操作。但问题是ViewPager
必须显示在public class MainActivity extends FragmentActivity {
TextView tv;
SeekBar seek;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seek = (SeekBar) findViewById(R.id.seekBar1);
/** Getting a reference to the ViewPager defined the layout file */
ViewPager pager = (ViewPager) findViewById(R.id.pager);
/** Getting fragment manager */
FragmentManager fm = getSupportFragmentManager();
/** Instantiating FragmentPagerAdapter */
MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(fm);
/** Setting the pagerAdapter to the pager object */
pager.setAdapter(pagerAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 5;
/** Constructor of the class */
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
/** This method will be invoked when a page is requested to create */
@Override
public Fragment getItem(int arg0) {
MyFragment myFragment = new MyFragment();
Bundle data = new Bundle();
data.putInt("current_page", arg0 + 1);
myFragment.setArguments(data);
return myFragment;
}
/** Returns the number of pages */
@Override
public int getCount() {
return PAGE_COUNT;
}
public CharSequence getPageTitle(int position) {
return "Page #" + (position + 1);
}
}
public class MyFragment extends Fragment {
MainActivity m;
int mCurrentPage;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/** Getting the arguments to the Bundle object */
Bundle data = getArguments();
/** Getting integer data of the key current_page from the bundle */
mCurrentPage = data.getInt("current_page", 0);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.myfragment_layout, container,
false);
tv = (TextView) v.findViewById(R.id.tv);
tv.setText("You are viewing the page #" + mCurrentPage + "\n\n"
+ "Swipe Horizontally left / right");
return tv;
}
}
}
内。请帮帮我。< / p>
以下是我的代码,
{{1}}
答案 0 :(得分:1)
我猜测你的SeekBar
在ViewPager
之外。首先在OnSeekBarChangeListener
上设置SeekBar
:
private OnSeekBarChangeListener mListener = new OnSeekBarChangeListener() {
// ...
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
if (fromUser) {
MyFragment mf = (MyFragment) getSupportFragmentManager()
.findFragmentByTag("android:switcher:" + R.id.pager + ":" + mCurrentItem);
if (mf != null && mf.getView() != null) {
mf.updateText(progress);
}
mf = (MyFragment) getSupportFragmentManager()
.findFragmentByTag("android:switcher:" + R.id.pager + ":" + mCurrentItem - 1);
if (mf != null && mf.getView != null) {
mf.updateText(progress);
}
mf = (MyFragment) getSupportFragmentManager()
.findFragmentByTag("android:switcher:" + R.id.pager + ":" + mCurrentItem + 1);
if (mf != null && mf.getView != null) {
mf.updateText(progress);
}
}
}
};
其中mCurrentItem
是int
字段,用于存储ViewPager
的当前页面:
private int mCurrentItem = 0;
// ... in the onCreate method
seek.setMax(100); // the maximum for the TextView size
seek.setProgress(50); // the starting textSize for the TextView
seek.setOnSeekBarChangeListener(mListener);
您还需要ViewPager.OnPageChangeListener
上的ViewPager
来更新mCurrentItem
字段:
pager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
mCurrentItem = position;
MyFragment mf = (MyFragment) getSupportFragmentManager()
.findFragmentByTag("android:switcher:" + R.id.pager + ":" + position);
if (mf != null && mf.getView() != null) {
mSeekBar.setOnSeekBarChangeListener(null);
mSeekBar.setProgress((int) (mf.getTextSize()));
mSeekBar.setOnSeekBarChangeListener(mListener);
}
}
// ...
}
不要忘记将此方法添加到您的片段类中:
public void updateText(int progress) {
textSize = progress;
((TextView)getView().findViewById(R.id.the_textview_id)).setTextSize(textSize);
}
和
public float getTextSize() {
return textSize;
}
其中mTextsize
是您的片段类中的一个字段,它将保留文字大小:
private float textSize = 50;
刚才意识到我可能误解了这个问题。如果您要更新TextView
尺寸,宽度/高度,那么不要像我的回答中那样设置文字,只需从LayoutParams
中检索TextView
并根据需要进行更新。< / p>