0

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.

mehrdad khosravi
  • 2,228
  • 9
  • 29
  • 34
Rodriquez
  • 981
  • 1
  • 7
  • 21

4 Answers4

2

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()

Community
  • 1
  • 1
tsiro
  • 2,323
  • 3
  • 24
  • 46
  • Your answer work perfectly, But i have one question. What is best practice for fragment? Initialize them when activity is create like "add" and then on button just show and hide it? – Rodriquez Oct 05 '16 at 09:43
  • your are welcome...you could also accept my answer...it depends on what you would like to achieve...if you would like to show your fragment only when you click a button, you do not have to add your fragment on activity creation. You should add it only on button click.. – tsiro Oct 05 '16 at 09:47
0

I think your code wrong in here please check this..

View view = inflater.inflate(R.layout.listview_fragment, container, false);
        CustomList adapter = new
                CustomList(getActivity(), web, imageId);
        list =  (ListView) **container**.findViewById(R.id.list);

to

View view = inflater.inflate(R.layout.listview_fragment, container, false);
        CustomList adapter = new
                CustomList(getActivity(), web, imageId);
        list =  (ListView) **view** .findViewById(R.id.list);
Dileep Patel
  • 1,988
  • 2
  • 12
  • 26
0

Try to use add or replace method for fragment transaction.

    FragmentManager fragmentManager = getFragmentManager(); 
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.setCustomAnimations(R.anim.slide_from_right, R.anim.slide_out_left, android.R.anim.slide_in_left, android.R.anim.slide_out_right);
    fragmentTransaction.add(container, fragment, fragment.getClass().getSimpleName());
    fragmentTransaction.commit();

Also change "container" to view in your fragment class for initializing view.

Sanjeet
  • 2,385
  • 1
  • 13
  • 22
0

Try below code inside button click event

   BackgroundSelectFragment bFragment = new BackgroundSelectFragment();
            getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.slide_from_right, R.anim.slide_out_left, android.R.anim.slide_in_left, android.R.anim.slide_out_right).replace(R.id.fragmentContainer, bFragment).commit();

In here, R.id.fragmentContainer is the id of layout in which BackgroundSelectFragment is needed to be inflated.

Rissmon Suresh
  • 13,173
  • 5
  • 29
  • 38