Custom sliding menu - android

I've seen many post here, and one helped me, but I think I miss something...
I created an ActivitySlider with that (just a sample of code)
public ActivitySlider(FragmentActivity activity, String loginU, String passU, String phpsessid)
{
this.activity = activity;
bundle = new Bundle();
bundle.putString("loginU", loginU);
bundle.putString("passU", passU);
bundle.putString("phpsessid", phpsessid);
// We get the View of the Activity
this.content = (View) activity.findViewById(R.id.drawer_layout).getParent();
ListView listTmp = (ListView) content.findViewById(R.id.left_drawer);
String[] mMenuTitles = activity.getResources().getStringArray(R.array.menu_array);
String[] tmp = {"Hello","'Suuppppp"," ? "};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.activity, R.layout.drawer_list_item, tmp);
listTmp.setAdapter(adapter);
// And its parent
// The container for the menu Fragment is added to the parent. We set an id so we can perform FragmentTransactions later on
this.menuContainer = new FrameLayout(this.activity);
this.menuContainer.setId(R.id.content_frame);
// We set visibility to GONE because the menu is initially hidden
//this.menuContainer.setVisibility(View.GONE);
ViewGroup parent = (ViewGroup) this.content.getParent();
parent.addView(this.menuContainer);
}
public <T extends Fragment> void setMenuFragment(Class<T> cls)
{
Fragment fragment = Fragment.instantiate(this.activity, cls.getName());
setMenuFragment(fragment);
}
public void setMenuFragment(Fragment fragment)
{
FragmentManager manager = this.activity.getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
fragment.setArguments(bundle);
transaction.replace(R.id.content_frame, fragment);
transaction.commit();
}`
So, I call this Activity in another Activity (I can't use the navigation drawer with toggle because I already have tabs) :
slider = new ActivitySlider(this, loginU, passU, sessid);
slider.setMenuFragment(MenuFragment.class);
That's my MenuFragment code :
/**
* "Constructeur" : inflate la vue
*/
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
//Récupération des arguments
int i = getArguments().getInt(ARG_MENU_NUMBER);
String loginU = getArguments().getString("loginU");
String passU = getArguments().getString("passU");
String sessid = getArguments().getString("phpsessid");
Log.i("MenuFrag loginU + passU",loginU + " - " + passU);
Log.i("resArg", i + "");
String section = "";
View rootView = null;
switch(i)
{
//Home
case 0:
rootView = inflater.inflate(R.layout.home, container, false);
section = getResources().getStringArray(R.array.menu_array)[i];
getActivity().setTitle(section);
return rootView;
//Demandes/Propositions
case 1:
rootView = inflater.inflate(R.layout.activity_viewpagertabs, container, false);
section = getResources().getStringArray(R.array.menu_array)[i];
getActivity().setTitle(section);
Intent askpurp = new Intent(getActivity(),PagerWithTab.class);
Bundle bundle = new Bundle();
bundle.putString("loginU",loginU);
bundle.putString("passU",passU);
bundle.putString("phpsessid",sessid);
askpurp.putExtras(bundle);
getActivity().startActivity(askpurp);
return rootView;
//Awesome
case 2:
rootView = inflater.inflate(R.layout.awesome, container, false);
section = getResources().getStringArray(R.array.menu_array)[i];
getActivity().setTitle(section);
return rootView;
default:
rootView = inflater.inflate(R.layout.awesome2, container, false);
section = getResources().getStringArray(R.array.menu_array)[i];
getActivity().setTitle(section);
return rootView;
}
}
Sliding is working fine, but I've a white box on my left, and I expect my menu to be in there. What should I fix?

The MenuFragment class cannot be used like that. Here the new MenuFragment:
public class MenuFrag extends ListFragment
{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.menutest, null, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
String[] sections = getResources().getStringArray(R.array.menu_array);
ArrayAdapter<String> sectionAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, sections);
setListAdapter(sectionAdapter);
}
}
And here is the layout:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/android:list"
android:layout_alignParentRight="true"
android:background="#color/wezo_color_blue" android:layout_alignParentTop="true"/>
</RelativeLayout>
You can now call:
slider.setMenuFragment(MenuFrag.class);
The last problem to address is the menu size. All that needs to be done is to change the following line:
this.content = (View) activity.findViewById(R.id.drawer_layout).getParent();
to
this.content = (View) activity.findViewById(android.R.id.content).getParent();
If you don't do this, then it takes the actual content size (just after tabs).

Related

how to retriev image from recyclerview adapter to fragment

I try to pass data from recyclerview viewholder adapter to another fragment, only i got is text but images do not show, i tries different codes but no image show, it shows blank, image not display. if i use picasso it gives target must not be zero error
my adapter
Picasso.with(context).load(image).fit().into(holder.clothImage);
holder.clothImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Clicked", Toast.LENGTH_SHORT).show();
holder.btn_add_to_cart.setText("Clicked");
GoodDetailFragment detailFragment = new GoodDetailFragment();
AppCompatActivity activity = (AppCompatActivity) v.getContext();
GoodDetailFragment detailFragment1 = new GoodDetailFragment().newInstance(itemList.get(pos).getPrice(), itemList.get(pos).getIcon(), itemList.get(pos).getName());
activity.getSupportFragmentManager()
.beginTransaction()
.replace(R.id.realtabcontent, detailFragment1)
.addToBackStack(null).commit();
}
});
and my next fragment (detail fragment)
*/
public class GoodDetailFragment extends BaseFragment {
private static final String GOOD_PRICE = "";
private static final String GOOD_IMG = "GOOD_IMG";
private static final String GOOD_TITLE = "GOOD_IMG";
#BindView(R.id.dela_img)
DraweeView goodImg;
#BindView(R.id.bei_dela)
TextView delaPrice;
public GoodDetailFragment newInstance(String price, int icon, String title) {
Bundle bundle = new Bundle();
//bundle.putInt(GOOD_IMG, icon);
bundle.putString(GOOD_PRICE, price);
bundle.putString(GOOD_TITLE, title);
bundle.getInt(GOOD_IMG, icon);
GoodDetailFragment detailFragment = new GoodDetailFragment();
detailFragment.setArguments(bundle);
return detailFragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO: inflate a fragment view
View rootView = super.onCreateView(inflater, container, savedInstanceState);
ButterKnife.bind(this, rootView);
return rootView;
}
#Override
public View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_good_detail, container, false);
ButterKnife.bind(this, view);
return view;
}
#Override
public void init() {
int image = getArguments().getInt(GOOD_IMG);
String price = getArguments().getString(GOOD_PRICE);
String tittle = getArguments().getString(GOOD_TITLE);
Uri uri = Uri.parse(String.valueOf(image));
//Picasso.with(getContext()).load(image).fit().into(goodImg); //GIves Error : Resource ID must not be zero.
delaPrice.setText(price);
Bitmap bitmap = BitmapFactory.decodeFile(String.valueOf(new File(String.valueOf(image))));
// goodImg.setImageBitmap(bitmap); // no image show
goodImg.setImageURI(uri);// still no image show
//Picasso.with(getContext()).load(uri).into(goodImg);// still no image show
if there is way i can get image from recycler view to detail fragment, please help

Pass data between Activity and Fragment

I want to pass data from Activity to Fragment using Bundle (I used Bundle to passing data between 2 Fragment, and that's worked).
In my Activity I have a ListView, in the OnItemClickListener I want to open a new Fragment, and the Item, which clicked pass through the Activity to this Fragment. But the Item is null, so basically nothing is send to the Fragment.
Here is my Activity:
public class Jegyek extends AppCompatActivity {
View v;
DB mydb;
ListView listView;
private String teszt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_jegyek);
listView = (ListView)findViewById(R.id.jegyekLista);
mydb = new DB(this);
final ArrayList<String> thelist = new ArrayList<>();
Cursor data = mydb.getTantargynev();
if (data.getCount() == 0) {
Toast.makeText(this, "Nincs jegyek hozzáadva", Toast.LENGTH_SHORT).show();
}
else {
while (data.moveToNext()) {
thelist.add(data.getString(0));
ListAdapter listadapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, thelist);
listView.setAdapter(listadapter);
}
listView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
teszt = thelist.get(i);
Bundle bundle = new Bundle();
bundle.putString("Tantárgy neve", teszt);
Toast.makeText(Jegyek.this, teszt, Toast.LENGTH_SHORT).show();
Fragment jegyekAllando = new jegyekAllando();
jegyekAllando.setArguments(bundle);
FragmentTransaction FragTan = getSupportFragmentManager().beginTransaction();
FragTan.replace(R.id.jegyekMenu, jegyekAllando);
ListView listaNezet = (ListView) findViewById(R.id.jegyekLista);
listaNezet.setVisibility(View.GONE);
FragTan.commit();
}
}
);
}
}
public String getTanNev () {
System.out.println("WARNING: "+ teszt);
return teszt;
}
}
And my Fragment:
public class jegyekAllando extends Fragment {
DB mydb;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_jegyek_allando, container, false);
Bundle bundle = new Bundle();
String tanNev = bundle.getStrin
g("Tantárgy neve");
Jegyek jegyAct = new Jegyek();
String tanNevv = jegyAct.getTanNev();
mydb = new DB(getActivity());
String jegyAtlag =String.valueOf(mydb.JegyekAtlaga(tanNev));
String jegyDarab =String.valueOf(mydb.JegyekDarabszama(tanNev));
return rootView;
}
}
When I realized, that the bundle is null, I try to pass that List Item through getter, so I changed my Fragment code for that:
Jegyek jegyek = new Jegyek();
String jegy = jegyek.getTanNev();
But that's not solved my problem, so I back to the Bundle, which also have some problem, but I can't find it. Intresting, that in the OnItemClickListener when I Toast the Item it's correct and works, but when I want to display in the console or pass to the Fragment, the value's is null.
From Activity you send data with intent as:
Bundle bundle = new Bundle();
bundle.putString("edttext", "From Activity");
// set Fragmentclass Arguments
Fragmentclass fragobj = new Fragmentclass();
fragobj.setArguments(bundle);
and in Fragment onCreateView method:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext = getArguments().getString("edttext");
return inflater.inflate(R.layout.fragment, container, false);
}
Hope this helps
Please follow this example:
In your activity, Pass data like
Bundle bundle = new Bundle();
bundle.putString("test", "Test Data");
// set in your testFragment Arguments
TestFragment testFragment = new TestFragment();
testFragment.setArguments(bundle);
And Receive Data from your TestFragment Like
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String text = getArguments().getString("test");
return inflater.inflate(R.layout.fragment, container, false);
}
In fragment "jegyekAllando" you must get your string from onCreate() with Bundle data = getArguments(), but you create new bundle.

Android Fragment-Listview to open new Fragment and transfer the data

I have a Fragment that has a listview,
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/containerMySubjects"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="15dp">
<ListView
android:id="#+id/lvSubjectLists"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
</LinearLayout>
I want to transfer the data from the listiview when selected to a new fragment. Here is my code in my onItemClick
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
InstructorFragmentSelectedSubject newFragment = new InstructorFragmentSelectedSubject();
Bundle args = new Bundle();
args.putString("code", code);
args.putString("title", title);
newFragment.setArguments(args);
getFragmentManager().beginTransaction().replace(R.id.containerMySubjects, newFragment).commit();
}
Here is my code in my new fragment, onCreateView
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_instructor_selected_subject, container, false);
tvSelectedCode = (TextView) view.findViewById(R.id.tvSelectedCode);
tvSelectedTitle = (TextView) view.findViewById(R.id.tvSelectedTitle);
String code = getArguments().getString("code");
String title = getArguments().getString("title");
tvSelectedCode.setText(code);
tvSelectedTitle.setText(title);
return view;
}
Try to change LinearLayout to Frame Layout, and see what will happens :)
I think yout code add fragment below your listview, and it has match_parent so it is invisible for user.
Try to avoid making containers for fragment with child view. Better create new fragment with listview and than add to the container. Than replace view by clicking listitem.
//EDITED:
If you change listview to listview fragment you will need communication between fragments. In your listview click listener you should make a callback to the activity, and activity can change fragments using FragmentManager and put some values to the new fragment.
Here you can check how to add callback:
https://developer.android.com/training/basics/fragments/communicating.html
Read Fragment Life Cycle try this code.
private String code = null;
private String title = null;
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_instructor_selected_subject, container, false);
tvSelectedCode = (TextView) view.findViewById(R.id.tvSelectedCode);
tvSelectedTitle = (TextView) view.findViewById(R.id.tvSelectedTitle);
tvSelectedCode.setText(code);
tvSelectedTitle.setText(title);
return view;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
code = getArguments().getString("code");
title = getArguments().getString("title");
}
}

Use single Fragment for multiple UI tasks?

I am using the Fragment master detail architecture
All Fragment Classes have the same logic
Is it possible to "detach" the Fragment from the layout and use just one Fragment class
So this code:
FragmentTransaction fragManager = getSupportFragmentManager().beginTransaction();
if("001".equalsIgnoreCase(id)){
arguments.putString(Fragment001.ARG_ITEM_ID, id);
Fragment001 fragment = new Fragment001();
fragment.setArguments(arguments); fragManager.replace(R.id.item_detail_container, fragment);
}
else if("002".equalsIgnoreCase(id)){
arguments.putString(Fragment002.ARG_ITEM_ID, id);
Fragment002 fragment = new Fragment002();
fragment.setArguments(arguments);
fragManager.replace(R.id.item_detail_container, fragment);
}
fragManager.commit();
Will turn into Something like:
FragmentTransaction fragManager = getSupportFragmentManager().beginTransaction();
GenericFragment fragment = new GenericFragment();
fragment.setUiId(id)
fragManager.replace(R.id.item_detail_container, fragment);
fragManager.commit();
List item
yes its possible, you can check and select which layout to use in onCreateView...
public static final String ARG_ITEM_ID1 = "fragment001";
public static final String ARG_ITEM_ID2 = "fragment002";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = null;
String id = "fragment001";
if(ARG_ITEM_ID1.equalsIgnoreCase(id)){
rootView = inflater.inflate(R.layout.fragment_1_layout, container, false);
}
else {
rootView = inflater.inflate(R.layout.fragment_2_layout, container, false);
}
return rootView;
}

How to setCurrentPage in ViewPager according to user selection & how do I know which list item called the activity

I'm new to Android programming.
I have a list of items, when the user clicks on an item it takes them to a screen with that item details.
I want the user to have the ability to swipe right and left to view other items' details in the list, instead of going back to the list and choosing another item.
I read that I need to use ViewPager for the ability to swipe right and left, so I did that.
ViewPager works fine, but my problem when I click any item on the list I always get to the first page in the ViewPager.
I don't want that, what I want is if I click on item 4 on the list it takes me to page 4 in the view pager and still have the ability to swipe right and left to view the details of other items.
I know how to set the page in viewpager by using mPager.setCurrentItem(0)
But I don't know how to set it according to which item was selected from the list (i.e which item launched the activity).
Here is my code:
Main activity which contains the listview:
public class Main extends SherlockListActivity implements OnItemClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView mylist = (ListView) findViewById(android.R.id.list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.simple_list_reda_1, R.id.list_content, getResources().getStringArray(R.array.items_list_array) );
mylist.setAdapter(adapter);
mylist.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0,View arg1, int position, long arg3)
{
Intent n = null;
switch (position){
case 0:
n = new Intent(getApplicationContext(), ViewPagerClass.class);
break;
case 1:
n = new Intent(getApplicationContext(), ViewPagerClass.class);
break;
case 2:
n = new Intent(getApplicationContext(), ViewPagerClass.class);
break;
case 3:
n = new Intent(getApplicationContext(), ViewPagerClass.class);
break;
}
if(null!=n)
startActivity(n);
}
});
}
}
ViewPagerClass
public class ViewPagerClass extends SherlockFragmentActivity{
static final int NUM_ITEMS = 4;
MyAdapter mAdapter;
ViewPager mPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.viewpager_layout);
mAdapter = new MyAdapter(getSupportFragmentManager());
mPager = (ViewPager) findViewById(R.id.viewpager);
mPager.setAdapter(mAdapter);
//mPager.setCurrentItem(2);
final ActionBar ab = getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
ab.setDisplayUseLogoEnabled(false);
ab.setDisplayShowHomeEnabled(false);
}
public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return NUM_ITEMS;
}
#Override
public Fragment getItem(int position) {
switch(position){
case 0: return FirstPageFragment.newInstance();
case 1: return SecondPageFragment.newInstance();
case 2: return ThirdPageFragment.newInstance();
case 3: return FourthPageFragment.newInstance();
}
return null;
}
}
public static class FirstPageFragment extends Fragment {
public static FirstPageFragment newInstance() {
FirstPageFragment f = new FirstPageFragment();
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.fragment1, container, false);
return V;
}
}
public static class SecondPageFragment extends Fragment {
public static SecondPageFragment newInstance() {
SecondPageFragment f = new SecondPageFragment();
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.fragment2, container, false);
return V;
}
}
public static class ThirdPageFragment extends Fragment {
public static ThirdPageFragment newInstance() {
ThirdPageFragment f = new ThirdPageFragment();
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.fragment3, container, false);
return V;
}
}
public static class FourthPageFragment extends Fragment {
public static ThirdPageFragment newInstance() {
ThirdPageFragment f = new ThirdPageFragment();
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.fragment4, container, false);
return V;
}
}
Finally viewpager_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<android.support.v4.view.ViewPager
android:id="#+android:id/viewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
So in short What I want is:
If I click on item 3 in the list I go to screen with the details on item 3, and if I swipe to the right I get to item 4, and if I swipe to the left I get to item 2.
In the onItemClickListener() you need to add an extra to the intent so you can get it when that activity launches.
n.putExtra("POSITION_KEY", position);
In the ViewPagerClass onCreate() using the
int position = getIntent().getIntExtra("POSITION_KEY", 0);
mPager.setCurrentItem(position);
That should do what you are wanting to do.

Categories

Resources