Calling a snippet from a list adapter

I am using a sliding menu / drawer drawing in my application. So the main activity has a leftView, which is a ListFragment named themesFragment () that loads a set of theme elements. When an element / theme is clicked, it replaces the fragment on the main view by calling FeedsFragment (tag). FeedsFragment uses an arraylist adapter to load feeds that have different clickable items on each list item. I want to get another instance in the feedsFragment (tag) method when the item is clicked in the list item.

    holder.contextView= (TextView) newsView.findViewById(R.id.arcHeader);
    if (item.hasArc()) {
        holder.contextView.setVisibility(View.VISIBLE);
        String arc;
        try {
            arc=item.getarc();
            holder.contextView.setText(arc);

            holder.contextView.setOnClickListener(new View.OnClickListener() {

                //currently it loads a class
                @Override
                public void onClick(View v) { 
                   Intent i = new Intent(context, SomeClass.class); 
                   i.putExtra("tag", arc);
                   context.startActivity(i);
                }
            });
        } catch (JSONException e) {

            e.printStackTrace();
        }

    } else {
        holder.contextView.setVisibility(View.GONE);
    }

      

It is currently loading a new class. I want to define a fragment and then go to the main activity to replace with the current view, but I cannot use getSupportFragmentManager () inside the adapter class, but only in the fragment or fragment activity. What should be the alternative to sweeping the fragment out of the adapter?

+3


source to share


4 answers


Solved it with the context passed in the list adapter:

@Override
public void onClick(View v) {
    Fragment newFragment = new ListFragmentClass(tag);
    if (newFragment != null)
        switchFragment(newFragment);
}

private void switchFragment(Fragment newFragment) {
    if (context == null)
        return;
    if (context instanceof MainActivity) {
        MainActivity feeds = (MainActivity) context;
        feeds.switchContent(newFragment);
    }
}

      



Here switchContent is the method defined in your main activity to switch / replace a fragment, as pointed out in Justin W.

+3


source


What I did was create this method in my main activity and just call it from other classes to change the fragment:



public void switchContent(Fragment fragment) {
        mContent = fragment;
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragment_container, fragment).commit();

        slidemenu.showContent();

    }

      

+3


source


Pass getFragmentManager()

as a parameter in your adapter constructor and use it.

0


source


Use Interface

to connect the side box ListFragment

to the main action. For example:

public class LeftDrawer extends ListFragment{

    private DrawerCallback mCallback;

    public interface DrawerCallback{
        public void onListClick(String tag);
    }

    public void setCallback(DrawerCallback callback){
        mCallback = callback;
    }

} 

      

Since it Fragments

must have an empty constructor, use a public method in yours Fragment

to set up the callback before finalizing FragmentTransaction

by adding it to your drawer. At this point, all that's left is notifying your Fragment

that a click has occurred. What you need to do is just click in ListFragment

and not add an onClickListener to each kind of your adapter.

 @Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    /*
     * Get item at the clicked position from your adapter and
     * get its string tag before triggering interface
     */
   mCallback.onListClick(tag);
}

      

Use the onListItemClick method for this. You will get the position of the list that was clicked and you can easily get that element from your adapter and get its tag value to return to your host's activity.

0


source







All Articles