我现在正在进行包含片段的活动
[1],[2],[3],[4]
启动应用程序[1]和[2]时一次运行
滑动到[2]时运行[3]并滑动到[3]运行[4]。
任何人都可以解决这个问题
我希望[1]中的功能[1],[2]中的[2]和[3]中的[3],选项卡滑动中的[4]中的[3]。
低于我的代码 片段[1]:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.meaningview, container, false);
word = (EditText) view.findViewById(R.id.word);
data = (WebView) view.findViewById(R.id.web);
if(first == 1)
{
try {
dbHelper = new SqlLiteDbHelper(getActivity());
dbHelper.CopyDataBaseFromAsset();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
first++;
}
find.setOnClickListener(new OnClickListener() {
@SuppressWarnings("static-access")
@SuppressLint({ "NewApi", "DefaultLocale" })
@Override
public void onClick(View v) {
url2 = word.getText().toString().toLowerCase();
urlword = url2;
InputMethodManager inputManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
if(url2.isEmpty())
{
Toast.makeText(getActivity(), "Word is missing! ", Toast.LENGTH_LONG).show();
}
else
{
try{
urdu = dbHelper.getDetail(word.getText().toString());
urdu_meaning.setText(urdu.Meaning.toString());
}catch(Exception e){
e.printStackTrace();
urdu_meaning.setText("Urdu word not found");
}
if (mWifi.isConnected())
{
data.loadUrl(url1 + url2);
}
else
{
Toast.makeText(getActivity(), "Network Problem", Toast.LENGTH_LONG).show();
//wifiManager.setWifiEnabled(true);
}
}
}
});
return view;
}
片段[2]
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.image, container, false);
imageView = (ImageView) view.findViewById(R.id.ImageView);
imageURL1 = imageURL + MeaningFragment.urlword + ".jpg";
// Execute the task
GetXMLTask task = new GetXMLTask();
task.execute(imageURL1);
return view;
}
片段[3]
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.audio, container, false);
audioURL2 = MeaningFragment.urlword + ".mp3";
try{
if (isPlayed) { // if media player had played, this function is called
start();
}else {
try {
//pDialog.show();
mPlayer =new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.setDataSource(audioURL+audioURL2);
ac = new MediaController(getActivity());
ac.setAnchorView(view.findViewById(R.id.reference));
ac.setMediaPlayer(AudioFragment.this);
mPlayer.setOnCompletionListener(AudioFragment.this);
mPlayer.setOnPreparedListener(AudioFragment.this);
} catch (IOException e) {
//e.printStackTrace();
Toast.makeText(getActivity(), "Audio URL Not Found! ", Toast.LENGTH_LONG).show();
}
try {
mPlayer.prepare();
mPlayer.start();
}
catch (Exception e)
{
e.printStackTrace();
//urdu_meaning.setText("Audio Not Found");
Toast.makeText(getActivity(), "Audio not Found!", Toast.LENGTH_LONG).show();
}
}
}
和片段[4]采用相同的方法
答案 0 :(得分:1)
FragmentPagerAdapter
的工作方式是,它会加载您滚动到的Fragment
前。因此,当您启动应用程序时,片段1加载并显示,片段2已加载并准备好显示。然后,当您滑动时,将显示预加载的片段2并加载片段3。
因此,如果您希望在视图寻呼机中显示片段时执行代码,则无法将此逻辑放在onCreateView()
中。
如果您希望在显示片段时执行逻辑,则需要为监听器创建ViewPager.OnPageChangeListener
并为视图寻呼机调用ViewPager.setOnPageChangeListener()
。在OnPageChangeListener
中,您可以实施方法onPageSelected
。这将告诉您何时通过滑动显示片段。 onPageSelected
的参数是显示的片段的索引,因此您可以判断哪个片段现在可见。
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
public void onPageSelected(int page) {
switch (page) {
case 0:
// code for first page displayed
break;
case 1:
// code for second page displayed
break;
case 2:
// code for third page displayed
break;
case 3:
// code for fourth page displayed
break;
}
}
// you also have to create empty impls for the other two methods
.
.
.
});
请注意:如果我在使用此功能时记得正确,onPageSelected
仅在滑动时调用,因此在第一次显示查看寻呼机时可能不会调用它。对于该事件,您可能必须从创建/显示视图寻呼机的方法中调用某些内容。
答案 1 :(得分:1)
我有setOnPageChangeListener的主要活动和getItem ....的另一个活动(ViewpageAdapter),如下所示
<强> ViewpagerAdpater 强>
@Override
public Fragment getItem(int position) {
switch (position) {
// Open FragmentTab1.java
case 0:
MeaningFragment meaningtab = new MeaningFragment();
return meaningtab;
// Open FragmentTab2.java
case 1:
ImageFragment imagetab = new ImageFragment();
return imagetab;
case 2:
AudioFragment audiotab= new AudioFragment();
return audiotab;
case 3:
VideoFragment videotab = new VideoFragment();
return videotab;
}
return null;
}
<强> MainActivity 强>
// Adding Tabs
for (String tab_name : tabtitles) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
MainActivity.position = position;
Log.i("Position", "Meaning =" + position);
Log.i("Main Position", "Meaning =" + MainActivity.position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
仍有问题。我在滑动标签时为每个片段设置日志,然后在onpagelistner中显示每个标签的准确位置,但在[1]和[2]中显示位置0和[3]在位置1和[4]在位置2。