所以我的MainActivity中有两个重要元素,一个按钮和一个edittext字段。我想让我的按钮只在第三个片段页面上可见,怎么做?这是我的代码。
DeviceFragment.java
package com.danmarker.decideforme;
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.ImageView;
import android.widget.TextView;
/**
* Created by Laptop on 2015.03.17..
*/
public class DeviceFragment extends Fragment {
public static final String ImageIDKey = "imagekey";
public static final String DescriptionKey = "descriptionkey";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_device, container, false);
Bundle bundle = getArguments();
if(bundle != null)
{
int imageID = bundle.getInt(ImageIDKey);
String description = bundle.getString(DescriptionKey);
setValues(view, imageID, description);
}
return view;
}
private void setValues(View view, int imageID, String description) {
ImageView imageView = (ImageView) view.findViewById(R.id.imageViewDevice);
imageView.setImageResource(imageID);
TextView textView = (TextView) view.findViewById(R.id.tvDeviceDescription);
textView.setText(description);
}
}
DevicePagerAdaptor.java
package com.danmarker.decideforme;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
/**
* Created by Laptop on 2015.03.18..
*/
public class DevicePagerAdaptor extends FragmentPagerAdapter {
String[] devices;
String[] deviceDescription;
public DevicePagerAdaptor(FragmentManager fn, Context context) {
super(fn);
Resources resources = context.getResources();
devices = resources.getStringArray(R.array.devices);
deviceDescription = resources.getStringArray(R.array.device_description);
}
@Override
public Fragment getItem(int position) {
Bundle bundle = new Bundle();
bundle.putString(DeviceFragment.DescriptionKey, deviceDescription[position]);
bundle.putInt(DeviceFragment.ImageIDKey, getDeviceImageID(position));
DeviceFragment deviceFragment = new DeviceFragment();
deviceFragment.setArguments(bundle);
return deviceFragment;
}
private int getDeviceImageID(int position) {
int id = 0;
switch (position) {
}
return id;
}
@Override
public int getCount() {
return devices.length;
}
问题是,MainActivity中的按钮覆盖在片段上,因此无论我如何滑动屏幕,它们都是可见的。当我到达最后一个片段时,我想只让它们可见。 怎么做?我也有一个fragment_device.xml,我必须声明DeviceFragment中的按钮,或者是什么?谢谢!
答案 0 :(得分:1)
如果您使用ViewPager
,则可以向OnPageChangeListener
注册ViewPager
并使用onPageSelected
方法显示Button
。
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int index) {
if (index == 2) {
button.setVisibility(View.VISIBLE);
}else{
button.setVisibility(View.GONE);
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
答案 1 :(得分:0)
传递bundle参数中的位置,并根据你得到的位置显示片段中的按钮