Please keep in mind I'm a beginner in Android, so it is possible that somewhere I overlooked something. Also I have more then one question here, because my approach to the whole project might be wrong.
I have an application that has side menu implemented via DrawerLayout. The "main screen" is a Fragment and with Drawer I create the side menu with a list in it. After that this is how I change the fragments upon clicking on the side menu:
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new FavoritesFragment();
break;
case 2:
fragment = new LastVisitedFragment();
break;
case 3:
fragment = new SettingsFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
Log.e("MainActivity", "Error in creating fragment");
}
}
The code above is implemented in my main activity. Next this is how my HomeFragment looks: (The home fragment is my home page so to say)
public class HomeFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.i("HomeFragment", "OK");
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
rootView.setOnClickListener(new View.OnClickListener() {
@
Override
public void onClick(View v) {
Log.i("HomeFragment", "OK0");
switch (v.getId()) {
case R.id.rec_prod1:
Log.i("HomeFragment", "OK1");
Intent intent1 = new Intent(getActivity(), SingleItemActivity.class);
startActivity(intent1);
break;
case R.id.rec_prod2:
Log.i("HomeFragment", "OK2");
Intent intent2 = new Intent(getActivity(), SingleItemActivity.class);
startActivity(intent2);
break;
case R.id.rec_prod3:
Log.i("HomeFragment", "OK3");
Intent intent3 = new Intent(getActivity(), SingleItemActivity.class);
startActivity(intent3);
break;
}
}
});
return rootView;
}
}
I have tried it with onTouchListener to but didn't work either, the single difference was that with onTouch it entered in to the onTouch method, bet nothing happened after. Also somewhere on this site I have red to set the scrollview clickable=true and all childs to false in order for it to work, but that didn't do either. Also fragment_home.xml is a custom layout with the Parent being a ScrollView (It's a list with three coloumns and 5 rows but each element is different, they all have an Imageview and different number of textviews, I didn't use a ListView or GridList since each row has a title, and the elements in each row have different layouts) I attach only an overview of it since the real xml code is over 800 lines long.
<ScrollView>
<LinearLayout>
<!--This part repeats 5 times creating each row -->
<TextView>
<LinearLayout>
<!-- This part repeats 3 times for each element of a column -->
<RelativeLayout>
<ImageView>
<!-- This part changes acodringly to the number of textviews, I can have 1 to 3 TextViews here-->
<LinearLayout>
<TextView>
<TexytView>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
So I have two main questions. 1. Why doesn't the clicking/touching work. 2. Is the project structure(architecture) ok like this? The whole idea is that when a click happens some elements take me to a product view screen, while others to a list of product elements (the data that fills all this will come from a server, that's why I started a new Activity in the onClick method).