I want to replace a fragment containing a list of items with a new fragment depending on the item clicked. This is my code, how do I need to approach this? Should I declare my ListView, Adapter and such in onCreateView or onActivityCreated? The container is a ViewPager if that makes a difference.
public class PreferenceListFragment extends SherlockFragment
{
ListView lv;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return (LinearLayout)inflater.inflate(R.layout.settings, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
lv = (ListView)getView().findViewById(R.id.SettingsLV);
lv.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, Preferences.TITLES));
lv.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Log.i("FragmentList", "Item clicked: " + arg2);
// Switch case that starts fragment depending on 'position'.
Fragment1 f1 = new Fragment1();
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.replace(R.id.pager, f1);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
}
}
);
}
}
Link to some very weird behaviour from my app.
The setting of adapter and listview should be done in onActivityCreated. OnCreateView is just to draw the user interface for the first time. The processing should be in onActivityCreated(). Refer http://developer.android.com/guide/topics/fundamentals/fragments.html
Related
When i replace fragment Listview place fragment Listview
I had trouble this in photo but i can't fix it .
i need to fix it,plz.
UI_screenshot
Code
#Override
public void onActivityCreated(Bundle savedInstanceState) {
final String[] titel = {"asdasd", "asda", "bdfg", "asdqweqwe"};
int idimg = R.mipmap.icon_main_list;
MyArrayAdapter adapter = new MyArrayAdapter(getActivity(), titel, idimg);
setListAdapter(adapter);
super.onActivityCreated(savedInstanceState);
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
switch (position) {
case 0:
Anmalz anmalz=new Anmalz();
Main_List_Fragment main_list_fragment=new Main_List_Fragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment, anmalz);
fragmentTransaction.remove(main_list_fragment).commit();
break;
Change your onCreateView() to -
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.main_fragment_list, container, false);
}
Also, in onListItemClick(), you should not remove/replace the same fragment again and again. You should just refresh the list adapter.
I am using android PagerStrip Slidding tab. I have two fragments. In the first fragment I have a listview.I want to navigate to the second fragment wen i click on the listview item in the first fragment . How do i do this? Thanks.
Here is the OnClick Listener in Fragment1
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragmentcontainer,Fragment2.newInstance());
transaction.addToBackStack(null);
}
});
//Here is my Fragment 2 which i want to navigate to
public class Fragment2 extends Fragment {
private static final String ARG_POSITION = "position";
public static final String TAG = Fragment2.class
.getSimpleName();
#InjectView(R.id.textView)
TextView textView;
private int position;
public static Fragment2 newInstance(int position) {
Fragment2 f = new Fragment2();
Bundle b = new Bundle();
b.putInt(ARG_POSITION, position);
f.setArguments(b);
return f;
}
public static Fragment2 newInstance(){
Fragment2 f = new Fragment2();
return f;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
position = getArguments().getInt(ARG_POSITION);
//setContentView(R.layout.activity_maps);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_card,container,false);
ButterKnife.inject(this, rootView);
ViewCompat.setElevation(rootView, 50);
textView.setText("CARD "+position);
return rootView;
}
}
I have figured out how to do this.
1. In the MainActivity your might already have
ViewPager pager;
and in your Oncreate method you also have
......
pager.setAdapter(adapter);
Now create the method below in the MainActivity below.ie(The Activity which controls all your fragments etc)
public void setCurrentItem (int position, boolean smoothScroll) {
pager.setCurrentItem(item,true);
}
Now Lets assume your fragment1 has a listview or button.And you want to navigate to fragment2 by onClick.
This is what you need to do
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3) {
((MainActivity)getActivity()).setCurrentItem (1, true);
}
});
//Remeber int position is the position of the fragment you want to navigate to in your Adapter
I have nested fragments. I have activity A that has fragment B which has List fragment, when user clicks item from ListFragment I want to open new fragment using onListItemClick method.
But I am getting no view found for id error.
Code looks like this:
private ArrayAdapter arrayAdapter;
private ArrayList warnings;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
warnings = new ArrayList();
for(int i=0;i<10;i++) {
warnings.add(i);
}
arrayAdapter = new ArrayAdapter(inflater.getContext(), android.R.layout.simple_list_item_1, warnings);
setListAdapter(arrayAdapter);
//View view = inflater.inflate(R.layout.activity_other, container, false);
return super.onCreateView(inflater,container,savedInstanceState);
}
#Override
public void onItemClick(AdapterView adapter, View view, int position, long id) {
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
ListItemFragment listItemFragment = new ListItemFragment();
FragmentManager manager = getChildFragmentManager();
android.support.v4.app.FragmentTransaction ft = manager.beginTransaction();
ft.replace(android.R.id.tabcontent, listItemFragment);
ft.attach(listItemFragment).addToBackStack(null).commit();
}
it doesn't seem to like android.R.id.tabcontent.
I have other child fragments working fine.
Please help.
Are you nesting the fragments in xml? You can only add nested fragments programmatically. See the documentation here.
The problem I was having, I was trying to attach fragment when I am already in fragment.
This should be done by detaching current fragment and attaching another one.
Now my app works just fine.
Something like this:
ft.detach(getParentFragment()).replace(android.R.id.tabcontent, fragment);
ft.attach(fragment).addToBackStack(null).commit();
I am trying to get onItemClick on ListItems to work from a fragment. Here is my code:
public class MyBudgetPageMenuFragment extends Fragment {
private Context context;
private ListView listView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.my_budget_listview,
container, false);
ListView listView = (ListView) myFragmentView
.findViewById(android.R.id.list);
context = this.getActivity().getApplicationContext();
String[] values = new String[4];
ListAdapter adapter = new ListAdapter(context, values);
listView.setAdapter(adapter);
listView.setOnItemClickListener( new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
selectItem(position);
}
});
return myFragmentView;
}
private void selectItem(int position) {
FragmentManager fragmentManager = getFragmentManager();
Fragment fragment;
switch (position) {
case 0:
fragment = new MyBudgetPageFragments();
fragmentManager.beginTransaction()
.replace(R.id.listFragment, fragment).commit();
break;
default:
String message1 = Integer.toString(position);
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setMessage("Position: " + message1);
alertDialog.show();
break;
}
}
}
But every time when I select an item, it isn't doing anything or throwing any exceptions. It seems that the event doesn't get registered. I debuged the code and it doesn't enter my event.
Can someoane tell me what I'm doing wrong?
Note that the ListView blocks clicks of an item that contains at least one focusable
descendant but it doesn’t make the content focus-reachable calling
setItemsCanFocus(true). One workaround is by disabling focusability of descendants, using
android:descendantFocusability="blocksDescendants"
in the layout defining your list item. (First learned of this myself from http://cyrilmottier.com/2011/11/23/listview-tips-tricks-4-add-several-clickable-areas/) Does your layout contain buttons? If so, you'd fall into this case.
Just go the simple way.
In your Adapter class, between getView method, initialize View v = convertView; then
v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "test "+ list.get(position).getS_name(), Toast.LENGTH_SHORT).show();
}
});
What I want to do is to call one fragment from another on click on item of listview. Program builds without errors however it crash on click on item. Btw I’m making here local Fragment_3 object, I have got already done it in my mainactivity, how to pass it to this function?
Fragment_1.java content:
public class Fragment_1 extends SherlockFragment implements OnItemClickListener{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// requestWindowFeature(Window.FEATURE_NO_TITLE);
View view = inflater.inflate(R.layout.fragment_1, container, false);
ListView listView = (ListView) view.findViewById(R.id.listView1);
listView.setOnItemClickListener(this);
return view;
}
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// TODO Auto-generated method stub
Fragment Fragment3 = new Fragment_3();
Integer fragmentId = (Integer) v.getTag();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(fragmentId, Fragment3);
fragmentTransaction.commit();
}
}
My suspicion is that v.getTag() is null; So Integer fragmentId = (Integer) v.getTag(); will end up in fragmentId being null. When unboxing that to an int you'll get a NullPointerException. Why don't you replace fragmentId in fragmentTransaction.replace(fragmentId, Fragment3); with the id of the container that holds current instance of Fragment_1?