简体   繁体   中英

Refreshing a fragment on listview item click

I'm new to fragments and have been trying to solve this for days. I am trying to have my fragments update on a listview click, and is extremely similar to this: Fragment in fragment do not refresh where there is both a listview on the left and a tabbed ui on the right. Here is one of my fragments:

public static class DescriptionFragment extends Fragment {

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


        View rootView = inflater.inflate(R.layout.fragment_section_description, container, false);

        text = ((TextView) rootView.findViewById(R.id.description));

        text.setText(Html.fromHtml(description_text));


        return rootView;
    }

how can i call this fragment (from inside the main activity) so onCreateView is renewed and the textview 'text' is updated? Any help is greatly appreciated

NOTE: the listview is not a fragment, just a seperate layout file for a tablet. My only fragments (such as DescriptionFragment) are for the tabs

I assume you are tying to show listview and on click of listitem, value should be update:

How can I call this fragement (from inside the main activity) so onCreateView is renewed and the textview 'text' is updated?

To call fragment inside main activity, you have to define fragment in side main xml file like below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<fragment
android:id="@+id/frag_series"
android:layout_width="200dip"
android:layout_height="match_parent"
android:layout_marginTop="?android:attr/actionBarSize"
class="com.example.demo_fragment.MyListFragment" />
</LinearLayout>

Now extends Listframent instead and code to fill listview data later fire click event like below

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
String item = (String) getListAdapter().getItem(position);
DetailFragment frag = (DetailFragment) getFragmentManager().findFragmentById(R.id.frag_capt);
if (frag != null && frag.isInLayout()) {
frag.setText(getCapt(item));
}
}

and then check condition and update your vaules, like below:

private String getCapt(String ship) {
if (ship.toLowerCase().contains("android")) {
return "Andy Rubin";
}
if (ship.toLowerCase().contains("IOS")) {
return "Steve Jobs";
}
return "???";
}

This is the way you can call your fragment.

DescriptionFragment  descriptionFragment = (DescriptionFragment ) 
getFragmentManager().findFragmentById(R.id.yourFragmentId);

You have to create a listener Interface something like this

public interface FragmentTransactionListener {
    public void updateFragment(/*some attributes*/);
}

You then have to implement the interface on your Fragment and override the updateFragment method. In this method you can add your code that will execute in the Fragment when your list item is clicked. Something like this

public static class DescriptionFragment extends Fragment implements FragmentTransactionListener {

...

    @Override
    public void updateFragment(/*some attributes*/) {
        //do some stuff
    }

...

When your fragment is created in your FragmentActivity get a instance of this listener and call it when your list item is clicked. Something similar to this code

FragmentTransactionListener listener = instance of your fragment;

//in your list item you then call this
listener.updateFragment(/*some attributes*/);

hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM