Rodriquez Rodriquez - 1 month ago 7
Android Question

My fragment not showing on OnClickListener by button

I have a problem with fragment, what i create:

Here is a custom List

class CustomList extends ArrayAdapter<String> {

private final Activity activity;
private final String[] web;
private final Integer[] imageID;

CustomList(Activity activity, String[] web, Integer[] imageID){
super(activity, R.layout.list_single, web);
this.activity = activity;
this.web = web;
this.imageID = imageID;
}

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = activity.getLayoutInflater();
View rowView = inflater.inflate(R.layout.list_single, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
ImageView img = (ImageView) rowView.findViewById(R.id.img);
txtTitle.setText(web[position]);
img.setImageResource(imageID[position]);
return rowView;
}
}


Here is a fragment:

public class BackgroundSelectFragment extends Fragment {
ListView list;
String[] web = {
"Clearing",
"Scroll",
"Desert",
"World",
"Old City",
"Clip Board",
} ;
Integer[] imageId = {
R.drawable.clearing,
R.drawable.scroll,
R.drawable.desert,
R.drawable.world,
R.drawable.city,
R.drawable.clipboard,
};

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.listview_fragment, container, false);
CustomList adapter = new
CustomList(getActivity(), web, imageId);
list = (ListView) container.findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getActivity(), "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
}
});
return view;
}
}


and the last i try to open a fragment:

backgroundBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fm = getFragmentManager();
fm.beginTransaction()
.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
.show(backgroundSelectFragment)
.commit();
}
});


Im not got any error, when i click a button nothing appear, where is my fragment? Of course i initialize it in MainActivity OnCreate like:

backgroundSelectFragment = new BackgroundSelectFragment();


Please help. I dont know what i do wrong.

Answer

You have to either add or replace your fragment into your basic layout view

fm.beginTransaction()
                .setCustomAnimations(android.R.animator.fade_in,android.R.animator.fade_out)
                .replace(containerViewId, fragment, tag) //either use replace
                .add(containerViewId, fragment, tag) //or add
                .show(backgroundSelectFragment)
                .commit();

where containerViewId is the view where your fragment should be added You can find the differences between replace() and add here: Difference between add(), replace(), and addToBackStack()

Comments