Jedinou mnou známou možností jak něco změnit něco v fragmentu, je mít v něm metodu, která to vykoná. Už jsem snad prozkoušel všechny možností, abych jí zavolal. Chtěl bych změnit TextView guest_name_id.
MainActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Init();
//Set default fragment.
fragmentTransaction = fragmentManager.beginTransaction(); //Start of transaction.
Fragment defaultFragment=new DefaultFragment();
fragmentTransaction.add(R.id.content_layout, defaultFragment); // add fragment to layout.
defaultFragment=null;
fragmentTransaction.commit(); //end of transaction.
//Listener for NameList (ListView). The listener change fragments on screen.
NameList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
final String TAG="tag";
fragmentTransaction = fragmentManager.beginTransaction(); //Start of transaction.
fragmentTransaction.replace(R.id.content_layout, new GuestFragment(), TAG);
fragmentTransaction.addToBackStack(null); //add fragment tag (id) for latest editing.
fragmentTransaction.commit(); //end of transaction.
GuestFragment gf= (GuestFragment)getFragmentManager().findFragmentByTag(TAG);
gf.setName("SDsd");
}
});
fragment class
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
public class GuestFragment extends Fragment {
private TextView nameTextView;
private ListView orderListView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view=inflater.inflate(R.layout.guest, container, false); //create fragment from XML file
nameTextView=(TextView) view.findViewById(R.id.guest_name_id);
return view;
}
public void setName(String name){
nameTextView.setText(name);
}
}