0

I have a fragment in which I want to have a main ImageButton in the center and 2 FAB's on either side that would go to the previous or next Image respectively. I have this set up in XML but can't quite figure out how to make it all fit in Java.

public class NewCard extends Fragment {

public static int cardNumber;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_new_card, container, false);
    final ImageButton button = view.findViewById(R.id.card1);

    FloatingActionButton previous = view.findViewById(R.id.previous);
    FloatingActionButton next = view.findViewById(R.id.next);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            cardNumber = 1;
            FinalSubmit fragment = new FinalSubmit();

            getContext().getSharedPreferences("pref", 0).edit().putInt("int", cardNumber).apply();

            address fragment4 = new address();
            FragmentTransaction ft4 = getFragmentManager().beginTransaction();
            ft4.replace(R.id.frameLayout, fragment4, "FragmentName");
            ft4.commit();


        }});

    return view;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);



}
}
4

1 回答 1

0

那么您想要的是背景中的按钮图像,以及通过图片序列/循环来更改背景中按钮图像的图像的两个 FAB?

我是从正常活动中得到的,而不是片段,但我相信它应该是一样的。

ImageButton button;
int nImages=5; //whatever number of images youre cycle goes through
int currentImageId=3; //for example   

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

button= (ImageButton)findViewById(R.id.imgButton);
button.setOnClickListener(imgButtonHandler);
changeBackground(currentImageId);

fab1= (ImageButton)findViewById(R.id.fab1);
fab1.setOnClickListener(imgUp);

fab2= (ImageButton)findViewById(R.id.fab2);
fab2.setOnClickListener(imgDown);
}

//so now we are cycling through 1 to 5 

View.OnClickListener imgUp = new View.OnClickListener() {

public void onClick(View v) {
    if (currentImageId==5)  //if we hit 5, and user is still pressing same fab we set 
// it back to the start, 1
{
currentImageId=1;
changeBackground(currentImageId);
return;
}
currentImageId+=1     //Otherwise, just add 1 to the counter
changeBackground(currentImageId);
}
};

View.OnClickListener imgDown = new View.OnClickListener() {

public void onClick(View v) {
if (currentImageId==1) //if we hit 1, and user is still pressing same fab we set 
// it back to the top, 5
{
currentImageId=5;
changeBackground(currentImageId);
return;
}
currentImageId-=1 //Otherwise, just subtract 1 to the counter
changeBackground(currentImageId);

}
};

public void changeBackground(int currentImageId) {
if (currentImageId==1) {
button.setBackgroundResource(image1 path or drawable);

}
if (currentImageId==2) {
button.setBackgroundResource(image2 path or drawable);

}
// and so on and so on... just add all the files 
}

它可能不干净,如果图片太多,可能会有更好的选择,但我认为这很简单,我就是这样做的,不是专家或任何东西,只是想帮忙!

于 2018-06-22T00:15:33.497 回答