Images not inflated in viewpager and recyclerview - android

My image is not showing in the viewpager, I have the xml with viewpager at the top and recyclerview at the bottom.
I do not know if I need to do implementation with interfaces. I follow the image slider example from here at Android Studio Tutorial - 70 - Implement Swipe Views
public class Home_activity extends BaseActivity {
ViewPager viewPager;
TextView testview;
private static final String urlHome =
"http://www.androidbegin.com/tutorial/jsonparsetutorial.txt";
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private List<ListItem> listItems;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
viewPager = (ViewPager) findViewById(R.id.viewPager);
SwipeAdapter swipeAdapter = new
SwipeAdapter(getSupportFragmentManager());
viewPager.setAdapter(swipeAdapter);
recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
listItems = new ArrayList<>();
loadRecyclerViewData();
}
<--- PageFagment.java--->
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class PageFragment extends Fragment {
public PageFragment() {
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup
container, Bundle savedInstanceState) {
TextView textView;
View view = inflater.inflate(R.layout.image_fragment_layout, container,
false);
textView = (TextView) view.findViewById(R.id.textViewFragment);
Bundle bundle = getArguments();
final String message = Integer.toString(bundle.getInt("count "));
textView.setText("This id the " + message);
return view;
}
}
<--- SwipeAdapter.java--->
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class SwipeAdapter extends FragmentPagerAdapter {
public SwipeAdapter(FragmentManager fm) {
super(fm);
}
#Override
public android.support.v4.app.Fragment getItem(int i) {
Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putInt("count ", i + 1);
fragment.setArguments(bundle);
return fragment;
}
#Override
public int getCount() {
return 3;
}
}
this is the xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="9">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="150dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/topbuttonlinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="btn1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="btn1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="btn1" />
</LinearLayout>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/topbuttonlinearLayout"
android:layout_weight="7">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<include layout="#layout/bottom_navigation_layout" />
</FrameLayout>
</LinearLayout>

Related

ViewPager2 does not restore state after fragment resumes

I use ViewPager2 to create a Horizontal Scrollable layout. Now when an item of ViewPager2 is clicked a CustomTab intent is launched to load a URL, on Exiting the CustomTab Screen ViewPager2 malfunctions, the parent layout takes the height of the whole screen, and the content of ViewPager2 disappears, Also the same fragment also contains a Linear Recylerivew which works fine. I am unable to diagnose the problem.
Here is a brief view of problem
fragment_courses.xml
<FrameLayout
style="#style/parent.contentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:src="#drawable/tickinsidecircle" />
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="#dimen/normalPadding"
android:paddingTop="#dimen/normalPadding"
android:paddingEnd="60dp"
android:paddingBottom="#dimen/normalPadding" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/question_posts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="#layout/item_question_post" />
</LinearLayout>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</FrameLayout>
FragmentCourses.java
package com.example.increaseyouriq.ui.fragments;
public class FragmentCourses extends Fragment implements MatchCourseClickListener, PostClickListener {
private static final String LOG_TAG = "FragmentCourses";
FragmentCoursesBinding binding;
private FirestoreScorecardViewModel scorecardViewModel;
private QuestionPost questionPost;
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
return binding.getRoot();
}
private void loadCourses(UserScorecard scorecard) {
CollectionReference coursesRef = FirebaseFirestore.getInstance().collection(COURSES);
FirestoreRecyclerOptions < MatchCourse > courses = new FirestoreRecyclerOptions.Builder < MatchCourse > ()
.setQuery(coursesRef, MatchCourse.class)
.setLifecycleOwner(getViewLifecycleOwner()).build();
CourseAdapter courseAdapter = new CourseAdapter(courses, getContext(), this);
binding.viewPager.setAdapter(courseAdapter);
binding.viewPager.setOffscreenPageLimit(1);
int nextItemVisiblePx = (int) getResources().getDimension(R.dimen.viewpager_next_item_visible);
int currentItemHorizontalMarginPx = (int) getResources().getDimension(R.dimen.viewpager_current_item_horizontal_margin);
binding.viewPager.setPageTransformer(new SnappyPageTransformer(nextItemVisiblePx, currentItemHorizontalMarginPx));
binding.viewPager.addItemDecoration(new HorizontalMarginItemDecoration(
requireContext(), R.dimen.viewpager_current_item_horizontal_margin_testing,
R.dimen.viewpager_next_item_visible_testing));
binding.viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
super.onPageScrolled(position, positionOffset, positionOffsetPixels);
}
});
}
item_viewpager
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/cardViewCourse"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
app:cardCornerRadius="15dp"
app:cardPreventCornerOverlap="true"
>
<LinearLayout
android:id="#+id/shopperCardBacground"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#drawable/gradient_nelson"
android:gravity="center"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:id="#+id/tv_cantidad_cursos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"/>
<TextView
android:id="#+id/tv_participants"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start" />
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="180dp"
android:layout_gravity="center"
android:scaleType="fitCenter"
android:src="#drawable/android_developer" />
<TextView
android:id="#+id/course_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:text="#string/fake_course_title" />
</LinearLayout>
</androidx.cardview.widget.CardView>
And the last part of Puzzle CourseAdapter.java
package com.example.increaseyouriq.ui.adapter;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import com.example.increaseyouriq.databinding.ItemShopCardBinding;
import com.example.increaseyouriq.ui.listeners.MatchCourseClickListener;
import com.example.increaseyouriq.ui.model.MatchCourse;
import com.firebase.ui.firestore.FirestoreRecyclerAdapter;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.squareup.picasso.Picasso;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class CourseAdapter extends FirestoreRecyclerAdapter < MatchCourse, CourseAdapter.ViewHolder > {
private final MatchCourseClickListener matchCourseClickListener;
public CourseAdapter(FirestoreRecyclerOptions < MatchCourse > options, Context context, MatchCourseClickListener listener) {
super(options);
this.matchCourseClickListener = listener;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
ItemShopCardBinding itemShopCardBinding = ItemShopCardBinding.inflate(inflater, parent, false);
return new ViewHolder(itemShopCardBinding);
}
#Override
protected void onBindViewHolder(#NonNull ViewHolder holder, int position, #NonNull MatchCourse model) {
holder.binding.cardViewCourse.setOnClickListener(view - > matchCourseClickListener.onScrollPagerItemClick(model));
holder.setData(model);
}
static class ViewHolder extends RecyclerView.ViewHolder {
ItemShopCardBinding binding;
ViewHolder(ItemShopCardBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
public void setData(MatchCourse matchCourse) {
binding.tvParticipants.setText(String.valueOf(matchCourse.getNumberOfParticipants()));
binding.courseTitle.setText(matchCourse.getName());
if (matchCourse.getImageResource().isEmpty());
else
Picasso.get().load(matchCourse.getImageResource()).into(binding.image);
}
}
}

I am not able to see the grid view in my Bookmark which extends Fragment

I am facing an issue the problem is I can see the Fragment but not the
GridView which I have there created, in the LogCat no error nothing but somehow the code it is not working.
But if I remove GridView from bookmark.xml and only leave a TextView than I can show the TextView
I am trying to reach a GridView into a Fragment.
This is my code.
Fragment
public class FragmentBookmark extends Fragment {
View paramView;
public FragmentBookmark() {
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
paramView = inflater.inflate(R.layout.bookmark, container, false);
return paramView;
}
}
This is the Main Activity
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.AddFragment(new FragmentExplore(), "");
adapter.AddFragment(new FragmentBookmark(), "");
adapter.AddFragment(new FragmentStore(), "");
mViewPager.setAdapter(adapter);
mTabLayout.setupWithViewPager(mViewPager);
This is the xml for Bookmark Fragment
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</LinearLayout>
Try like this using framelayout
<FrameLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<GridView
android:id = "#+id/gvList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:stretchMode="spacingWidth"
android:numColumns="2"/>
</FrameLayout>
You need to set adapter to your GridView as only GridView will never show anything as it is a simple ViewGroup.. Also rather than using GridView try to use RecyclerView in grid mode. Below is a simple tutorial you can follow-
RecyclerView as GridView with GridLayoutManager
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/m_recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp" />
</LinearLayout>
grid_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/imageview1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:contentDescription="#null" />
<TextView
android:id="#+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:maxLines="1"
android:singleLine="true"
tools:text="Category" />
</LinearLayout>
</android.support.v7.widget.CardView>
Adapter:
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private Context mContext;
private List<String> myNameList;
public MyAdapter(Context context, List<String> categoryInfo) {
mContext = context;
myNameList = categoryInfo;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_item, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, final int position) {
String myName = myNameList.get(position);
holder.tvName.setText(myName);
}
#Override
public int getItemCount() {
return myNameList.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
private TextView tvName;
MyViewHolder(View itemView) {
super(itemView);
tvName = itemView.findViewById(R.id.tv_name);
}
}
}
In your Activity or Fragment:
Context mContext = this;
RecyclerView mRecyclerView;
mRecyclerView = findViewById(R.id.m_recyclerView);
mRecyclerView.setLayoutManager(new android.support.v7.widget.GridLayoutManager(mContext, 3));
ArrayList<String> nameArray = new ArrayList<>();
nameArray.add("name 1");
nameArray.add("name 2");
nameArray.add("name 3");
nameArray.add("name 4");
mRecyclerView.setAdapter(new MyAdapter(mContext, nameArray));

How to make multiple layout for one screen?

I want to make Screen in android with multiple layouts. Fox example On the top of the screen there is the header with image slider (this part is done) below that there will be a list view and below that there will be grid view.
Activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.root.multipleview.MainActivity">
<!--Header image-->
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoStart="true"></android.support.v4.view.ViewPager>
<!--ListView Below header -->
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:height="10dp" />
</RelativeLayout>
CustomLayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:background="#f1f3f7"
android:orientation="vertical">
<!--Header image with slider-->
<ImageView
android:id="#+id/headerimg"
android:layout_width="fill_parent"
android:layout_height="180dp"
android:scaleType="centerCrop"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:src="#mipmap/ic_launcher" />
<!--ListView Below Header-->
<RelativeLayout
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:layout_marginTop="20dp"
android:paddingLeft="2dp">
<ImageView
android:id="#+id/imageView"
android:layout_width="100dp"
android:layout_height="90dp"
android:layout_weight="0.05"
app:srcCompat="#mipmap/ic_launcher"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:gravity="center"/>
<TextView
android:id="#+id/textName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="40dp"
android:layout_marginStart="40dp"
android:layout_marginTop="11dp"
android:layout_toEndOf="#+id/imageView"
android:layout_toRightOf="#+id/imageView"
android:text="TextView" />
<TextView
android:id="#+id/textdesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_marginBottom="20dp"
android:layout_alignBottom="#+id/imageView"
android:layout_alignLeft="#+id/textName"
android:layout_alignStart="#+id/textName"/>
</RelativeLayout>
</LinearLayout>
ListView.java
package com.example.root.multipleview;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
/**
* Created by root on 11/18/2017.
*/
public class ListView extends AppCompatActivity {
int[] Images = {R.drawable.salad,R.drawable.salami,R.drawable.flour,R.drawable.salt,
R.drawable.sandwich,R.drawable.sausage,R.drawable.seeds,R.drawable.cheese,R.drawable.shrimp,R.drawable.cupcake,R.drawable.cup,R.drawable.spices,R.drawable.cabbage
,R.drawable.spoon,R.drawable.apple,R.drawable.asparagus,R.drawable.cupcake,R.drawable.fish,R.drawable.corn,R.drawable.cupcake,R.drawable.spatula,R.drawable.olives
,R.drawable.garlic,R.drawable.taco,R.drawable.broccoli,R.drawable.cabbage};
String[] Names = {"a","B","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
String[] Description = {"VVV","DDD","ddd","dddsa","ddsdsds","zsxx","wwwwq","ddwqK","EQWK","FgggFFF","FFFkklF","FghhFFF","FFhhFF","FFhhFF",
"FFmmmFF","FgggFFF","FFFFbbb","FFFFfeee","gfffFFFF","FFFFfff","FFFgffF","FFFFfgffg","FFFfgfgfF","FFFgffF","FFFgfggdF","FFFFdssa"};
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
android.widget.ListView listView= (android.widget.ListView) findViewById(R.id.listView);
CustomAdapter customAdapter = new CustomAdapter();
listView.setAdapter(customAdapter);
}
public class CustomAdapter extends BaseAdapter {
#Override
public int getCount() {
return Images.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = getLayoutInflater().inflate(R.layout.custom_layout,null);
ImageView imageView = (ImageView)view.findViewById(R.id.imageView);
TextView textView_N = (TextView)view.findViewById(R.id.textName);
TextView textView_D = (TextView)view.findViewById(R.id.textdesc);
imageView.setImageResource(Images[i]);
textView_N.setText(Names[i]);
textView_D.setText(Description[i]);
return view;
}
}
}
MainActivity.java
package com.example.root.multipleview;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager)findViewById(R.id.viewPager);
ViewPageAdapter viewPageAdapter = new ViewPageAdapter( this);
viewPager.setAdapter(viewPageAdapter);
}
}
ViewPageAdapter
package com.example.root.multipleview;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
/**
* Created by root on 11/18/2017.
*/
public class ViewPageAdapter extends PagerAdapter{
private Context context;
private LayoutInflater layoutInflater;
private Integer[] images = {R.drawable.b,R.drawable.c,R.drawable.e,R.drawable.j,R.drawable.r,R.drawable.x,R.drawable.y};
public ViewPageAdapter(Context context){this.context = context;}
#Override
public int getCount() {
return images.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater =(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate((R.layout.custom_layout), null);
ImageView imageView = (ImageView) view.findViewById(R.id.headerimg);
imageView.setImageResource((images[position]));
ViewPager vp = (ViewPager) container;
vp.addView(view, 0);
return view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
ViewPager vp = (ViewPager) container;
View view = (View) object;
vp.removeView(view);
}
}
When I check this all layout separately it works but after adding in one layout it's not working. output screenshot is added
enter image description here
How to make multiple layout for one screen?
my approach for this goal is using Fragments
how?
i do make a single main_layout and set a FrameLayout in order to put fragments with different layouts.so i have a screen with different layouts.
this is my best answer for your question .

How to hide layout when click outside of it?

In my app in need to hide layout when i click out side (MobileFragment) of that layout. You can understand the whole problem from given below images.
This is my app sturcture:
This is the problem:
Here is my code:
MainActivity.java
package com.example.admin.smsrupee;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private TabLayout tabLayout;
private ViewPager viewPager;
private ImageView menu_icon,profile_icon;
private LinearLayout layout,profile_layout;
ViewPagerFinder adapter;
CoordinatorLayout coordinatorLayout;
private int[] tabIcons = {
R.drawable.mobile_icon,
R.drawable.dth_icon,
R.drawable.data_card_icon,
R.drawable.landline_icon,
R.drawable.electricity_icon,
R.drawable.gas_icon
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
viewPager = (ViewPager)findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
menu_icon = (ImageView)findViewById(R.id.menu_icon);
profile_icon = (ImageView)findViewById(R.id.profile_icon);
layout = (LinearLayout)findViewById(R.id.menu_list);
profile_layout = (LinearLayout)findViewById(R.id.profile_menu_list);
menu_icon.setOnClickListener(this);
profile_icon.setOnClickListener(this);
viewPager.setOnClickListener(this);
coordinatorLayout.setOnClickListener(this);
setupTabIcons();
FragmentStatePagerAdapter a = (FragmentStatePagerAdapter)viewPager.getAdapter();
Fragment ft = (Fragment)a.instantiateItem(viewPager, viewPager.getCurrentItem());
System.out.println("Fragment---" + ft);
}
private void setupTabIcons() {
Drawable mobile_img;
Resources res1 = getResources();
mobile_img = ContextCompat.getDrawable(MainActivity.this, R.drawable.mobile_icon);
mobile_img.setBounds(0, 0, 20, 25);
TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabOne.setText("Mobile");
tabOne.setCompoundDrawables(null,mobile_img, null, null);
tabLayout.getTabAt(0).setCustomView(tabOne);
Drawable dth_img ;
Resources res2 = getResources();
dth_img = ContextCompat.getDrawable(MainActivity.this, R.drawable.dth_icon);
dth_img.setBounds(0, 0, 20, 25);
TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabTwo.setText("DTH");
tabTwo.setCompoundDrawables(null, dth_img, null, null);
tabLayout.getTabAt(1).setCustomView(tabTwo);
Drawable data_img ;
Resources res3 = getResources();
data_img = ContextCompat.getDrawable(MainActivity.this, R.drawable.data_card_icon);
data_img.setBounds(0, 0, 20, 25);
TextView tabThree = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabThree.setText("Data Card");
tabThree.setCompoundDrawables(null, data_img, null, null);
tabLayout.getTabAt(2).setCustomView(tabThree);
Drawable landline_img ;
Resources res4 = getResources();
landline_img = ContextCompat.getDrawable(MainActivity.this, R.drawable.landline_icon);
landline_img.setBounds(0, 0, 20, 25);
TextView tabFour = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabFour.setText("LandLine");
tabFour.setCompoundDrawables(null,landline_img, null, null);
tabLayout.getTabAt(3).setCustomView(tabFour);
Drawable electricity_img ;
Resources res5 = getResources();
electricity_img = ContextCompat.getDrawable(MainActivity.this, R.drawable.electricity_icon);
electricity_img.setBounds(0, 0, 20, 25);
TextView tabFive = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabFive.setText("Electricity");
tabFive.setCompoundDrawables(null,electricity_img, null, null);
tabLayout.getTabAt(4).setCustomView(tabFive);
Drawable gas_img ;
Resources res6 = getResources();
gas_img = ContextCompat.getDrawable(MainActivity.this, R.drawable.gas_icon);
gas_img.setBounds(0, 0, 20, 25);
TextView tabSix = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabSix.setText("Gas");
tabSix.setCompoundDrawables(null,gas_img , null, null);
tabLayout.getTabAt(5).setCustomView(tabSix);
}
private void setupViewPager(ViewPager viewPager) {
adapter = new ViewPagerFinder(getSupportFragmentManager());
adapter.addFragment(new MobileFragment(), "Mobile");
adapter.addFragment(new DTHFragment(), "DTH");
adapter.addFragment(new DataCardFragment(), "Data Card");
adapter.addFragment(new LandLineFragment(), "Landline");
adapter.addFragment(new ElectricityFragment(), "Electricity");
adapter.addFragment(new GasFragment(), "Gas");
viewPager.setAdapter(adapter);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.menu_icon:
if(layout.getVisibility() == View.INVISIBLE){
layout.setVisibility(View.VISIBLE);
}else{
layout.setVisibility(View.INVISIBLE);
}
break;
case R.id.profile_icon:
if(profile_layout.getVisibility() == View.INVISIBLE) {
profile_layout.setVisibility(View.VISIBLE);
} else {
profile_layout.setVisibility(View.INVISIBLE);
}
break;
default:
break;
}
}
class ViewPagerFinder extends FragmentStatePagerAdapter{
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerFinder(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position){
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
public Fragment returnFragment(int position){
return mFragmentList.get(position);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
/* if (id == R.id.action_settings) {
return true;
}*/
return super.onOptionsItemSelected(item);
}
}
MobileFragemnt.java
package com.example.admin.smsrupee;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
/**
* Created by admin on 11/4/2015.
*/
public class MobileFragment extends android.support.v4.app.Fragment {
EditText editText;
private ImageView menuicon;
private LinearLayout layout;
private RelativeLayout relativeLayout;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.mobile_fragment, container, false);
return view;
}
public static MobileFragment newInstance(int index) {
MobileFragment f = new MobileFragment();
Bundle args = new Bundle();
args.putInt("index", index);
f.setArguments(args);
return f;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/cordinator"
android:clickable="true"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent" android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:contentInsetStart="0dp"
android:contentInsetLeft="0dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
android:background="?attr/colorPrimary" app:popupTheme="#style/AppTheme.PopupOverlay">
<include layout="#layout/content_main"/>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#2f5392"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
/>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_height="wrap_content"
app:tabMode="scrollable"
app:tabGravity="fill"/>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_marginTop="50dp"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" >
</android.support.v4.view.ViewPager>
<!--<include layout="#layout/dummy_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
/>-->
<include layout="#layout/menu_contents"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
/>
<include layout="#layout/profile_contents"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
/>
</android.support.design.widget.CoordinatorLayout>
mobile_fragment.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/mobile_relative_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="info.androidhive.materialtabs.fragments.OneFragment">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#2f5392"
android:id="#+id/view" />
<LinearLayout
android:layout_width="wrap_content"
android:id="#+id/linearlayout_one1"
android:layout_marginTop="30dp"
android:layout_centerHorizontal="true"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prepaid"
android:textColor="#2f5392"
android:layout_marginRight="20dp"
android:id="#+id/prepaid_radio1"
android:checked="false" />
<RadioButton
android:layout_width="wrap_content"
android:textColor="#2f5392"
android:layout_height="wrap_content"
android:text="Postpaid"
android:id="#+id/postpaid_radio1"
android:checked="false" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:id="#+id/linearlayout_two1"
android:layout_below="#+id/linearlayout_one1"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_marginTop="20dp"
android:orientation="vertical"
>
<EditText
android:layout_width="300dp"
android:hint="Enter Your Mobile Number"
android:textSize="15sp"
android:text="123xxxxxx"
android:id="#+id/mobile_number1"
android:paddingLeft="30dp"
android:layout_height="wrap_content"
android:drawableRight="#drawable/editbox_icon"
/>
<EditText
android:layout_width="300dp"
android:hint="Select Operator"
android:paddingLeft="30dp"
android:id="#+id/operator1"
android:textSize="15sp"
android:layout_height="wrap_content"
android:drawableRight="#drawable/down_arrow"
/>
<EditText
android:layout_width="300dp"
android:drawableLeft="#drawable/indian_rupee"
android:hint=" Amount"
android:id="#+id/amount1"
android:textSize="15sp"
android:layout_height="wrap_content"
android:drawableRight="#drawable/browse_plan"
/>
</LinearLayout>
<Button
android:layout_below="#+id/linearlayout_two1"
android:layout_marginTop="35dp"
android:layout_width="250dp"
android:id="#+id/proceed1"
android:layout_centerHorizontal="true"
android:layout_height="37dp"
android:background="#2f5392"
android:textColor="#ffffff"
android:text="PROCEED"/>
</RelativeLayout>
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main" tools:context=".MainActivity">
<RelativeLayout
android:id="#+id/relative_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sms_rupee_logo"
/>
<ImageView
android:id="#+id/menu_icon"
android:layout_width="wrap_content"
android:layout_marginRight="10dp"
android:layout_alignParentRight="true"
android:layout_height="wrap_content"
android:src="#drawable/menu_icon"
/>
<ImageView
android:id="#+id/wallet_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:layout_toLeftOf="#+id/menu_icon"
android:src="#drawable/wallet_icon" />
<ImageView
android:id="#+id/profile_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:layout_toLeftOf="#+id/wallet_icon"
android:src="#drawable/profile_icon" />
</RelativeLayout>
</RelativeLayout>
profile_content.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/profile_menu_list"
android:layout_width="165dp"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:clickable="true"
android:layout_alignParentTop="true"
android:background="#2f5392"
android:orientation="vertical"
android:visibility="invisible"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_gravity="center_vertical"
android:padding="8dp"
android:layout_height="wrap_content"
android:src="#drawable/add_user"/>
<TextView
android:id="#+id/register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_gravity="center_vertical"
android:padding="8dp"
android:text="Register"
android:textColor="#fff"
android:textSize="17dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:padding="8dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="#drawable/login"/>
<TextView
android:id="#+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="3dp"
android:padding="8dp"
android:text="Login"
android:textColor="#ffffff"
android:textSize="17dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:padding="8dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="#drawable/referral"/>
<TextView
android:id="#+id/referrals"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_gravity="center_vertical"
android:padding="8dp"
android:text="Referrals"
android:textColor="#ffffff"
android:textSize="17dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:padding="8dp"
android:layout_gravity="center_vertical"
android:layout_height="wrap_content"
android:src="#drawable/recharge"/>
<TextView
android:id="#+id/configure_easychange"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_gravity="center_vertical"
android:padding="8dp"
android:text="Configure EasyCharge"
android:textColor="#ffffff"
android:textSize="17dp"
/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Firstly, you can get an instance of an Activity inside its fragment by help of getActivity() method. Use this instance to call any method of the activity from your fragment like this :-
((MainActivity) getActivity()).methodName();
Secondly, I would suggest you to convert that bar into a NavigationDrawer which will benefit you by providing you with a much cleaner user interface with more cleaner transitions.
MainActivity
public class HomeActivity extends AppCompatActivity {
public static BottomNavigationView mNavigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//-----------------------------------------------| Bottom navigation menu
mNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
mNavigationView.setOnNavigationItemSelectedListener(new ActionHandler());
}
private class ActionHandler implements BottomNavigationView.OnNavigationItemSelectedListener {
//-----------------------------------------------| Bottom navigation menu listener
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_menu:
getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.enter_from_left, 0).add(android.R.id.content, new MenuFragment(), "myFragmentTag").addToBackStack("tag").commit(); // without FrameLayout
return true;
case R.id.action_account:
//
return true;
}
return false;
}
}
}
fragment_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/root_view"
android:background="#android:color/transparent"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".views.fragments.MenuFragment">
<LinearLayout
android:orientation="vertical"
android:layout_marginEnd="150dp"
android:background="#color/colorBackgroundGrey"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:background="#color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="250dp">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/header_photo"
app:civ_border_width="1dp"
app:civ_border_color="#color/colorLightGrey"
app:tint="#color/colorWhite"
android:src="#drawable/ic_add_photo"
android:layout_centerInParent="true"
android:layout_width="140dp"
android:layout_height="140dp"/>
<TextView
android:id="#+id/nav_header_title"
android:textColor="#color/colorWhite"
android:layout_marginTop="10dp"
android:text="#string/app_name"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:layout_marginBottom="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/items_recycler_view"
tools:listitem="#layout/item_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
MemuFragment
public class MenuFragment extends Fragment {
private UserModel mUser;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_menu, container, false);
//getActivity().onBackPressed();
((RelativeLayout) view.findViewById(R.id.root_view)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Utility.getInstance().onBackFragment(Objects.requireNonNull(getActivity()).getSupportFragmentManager(), new MenuFragment());
}
});
return view;
}
}
Utility
public class Utility {
private String TAG = this.getClass().getSimpleName();
private static Utility mUtility;
public static Utility getInstance() {
if (mUtility == null) {
mUtility = new Utility();
}
return mUtility;
}
//===============================================| Add fragment | Replace fragment | Remove fragment
//https://stackoverflow.com/questions/18634207/difference-between-add-replace-and-addtobackstack
public void onAddFragment(FragmentManager mManager, Fragment mFragment){
//getSupportFragmentManager().beginTransaction().add(android.R.id.content, new FragmentHome(), "myFragmentTag").addToBackStack("tag").commit(); // without FrameLayout
mManager.beginTransaction().setCustomAnimations(R.anim.fragment_fade_enter, 0).add(R.id.home_container, mFragment).addToBackStack(mFragment.getTag()).commitAllowingStateLoss(); //with back press
//getActivity().getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.fragment_fade_enter, 0).replace(layout, mFragment).addToBackStack(mFragment.getTag()).commitAllowingStateLoss(); //with back press
}
public void onReplaceFragment(FragmentManager mManager, Fragment mFragment){
mManager.beginTransaction()
.setCustomAnimations(R.anim.fragment_fade_enter, 0)
.replace(R.id.home_container, mFragment)
.addToBackStack(null)
.commit();
}
public void onRemoveFragment(FragmentManager mFragmentManager, Fragment mFragment){
mFragmentManager.beginTransaction()
.setCustomAnimations(R.anim.fragment_fade_enter, 0)
.remove(mFragment)
.addToBackStack(null)
.commit();
}
public void onBackFragment(FragmentManager mFragmentManager, Fragment mFragment){
if(mFragmentManager.getBackStackEntryCount() > 0) {
mFragmentManager.popBackStack(mFragment.getTag(), 0);
//getActivity().getSupportFragmentManager().popBackStack(new CampaignFragment().getTag(), 0);
}
}
public void onShowFragmentDialog(FragmentManager mFragmentManager, BottomSheetDialogFragment mFragment){
mFragment.setCancelable(false);
mFragment.show(mFragmentManager, mFragment.getTag());
}
}

Recycle View dont collapse toolbar in fragment! why?

i use this tutorial for making collapsing toolbar with nestedscrollview. but when i use recycleview in fragment for making tab's the toolbar not react with recycleview scrolling... how to fix that?
here my code:
layout_main.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.inthecheesefactory.lab.designlibrary.activity.MainActivity">
<android.support.design.widget.CoordinatorLayout
android:id="#+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include layout="#layout/fragment_toolbar" />
<android.support.design.widget.TabLayout
android:id="#+id/tabLayout"
app:layout_scrollFlags="scroll|enterAlways"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="#color/secondColor"
app:tabSelectedTextColor="#color/error_color" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fabBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginBottom="#dimen/fab_margin_bottom"
android:layout_marginRight="#dimen/fab_margin_right"
android:src="#drawable/ic_action_ok"
app:borderWidth="0dp"
app:fabSize="normal" />
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/header"
app:itemIconTint="#333"
app:itemTextColor="#333"
app:menu="#menu/navigation_drawer_items" />
tabview and viewpager handle in MainActivity:
final ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.addTab(tabLayout.newTab().setText("TAB1"));
tabLayout.addTab(tabLayout.newTab().setText("TAB2"));
tabLayout.addTab(tabLayout.newTab().setText("TAB3"));
tabLayout.addTab(tabLayout.newTab().setText("TAB4"));
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
ContactAdapter.java
package tab;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ContactViewHolder> {
private List<ContactInfo> contactList;
public ContactAdapter(List<ContactInfo> contactList) {
this.contactList = contactList;
}
#Override
public int getItemCount() {
return contactList.size();
}
#Override
public void onBindViewHolder(ContactViewHolder contactViewHolder, int i) {
ContactInfo ci = contactList.get(i);
contactViewHolder.vName.setText(ci.name);
contactViewHolder.vSurname.setText(ci.surname);
contactViewHolder.vEmail.setText(ci.email);
contactViewHolder.vTitle.setText(ci.name + " " + ci.surname);
}
#Override
public ContactViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.
from(viewGroup.getContext()).
inflate(R.layout.carditem, viewGroup, false);
return new ContactViewHolder(itemView);
}
public static class ContactViewHolder extends RecyclerView.ViewHolder {
protected TextView vName;
protected TextView vSurname;
protected TextView vEmail;
protected TextView vTitle;
public ContactViewHolder(View v) {
super(v);
vName = (TextView) v.findViewById(R.id.txtName);
vSurname = (TextView) v.findViewById(R.id.txtSurname);
vEmail = (TextView) v.findViewById(R.id.txtEmail);
vTitle = (TextView) v.findViewById(R.id.title);
}
}
}
PagerAdapter.java
package tab;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
TabFragment1 tab1 = new TabFragment1();
return tab1;
case 1:
TabFragment2 tab2 = new TabFragment2();
return tab2;
case 2:
TabFragment3 tab3 = new TabFragment3();
return tab3;
case 3:
TabFragment4 tab4 = new TabFragment4();
return tab4;
default:
return null;
}
}
#Override
public int getCount() {
return mNumOfTabs;
}
}
tab_fragment_1.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/cardList"
android:layout_width="match_parent"
android:layout_height="match_parent" />
tab_fragment_1.java
package tab;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
public class TabFragment1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
RecyclerView v =(RecyclerView) inflater.inflate(R.layout.tab_fragment_1, container, false);
SetupRecycleView(v);
return v;
}
void SetupRecycleView(RecyclerView recList) {
recList.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(G.context);
llm.setOrientation(LinearLayoutManager.VERTICAL);
// recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
recList.setLayoutManager(llm);
List<ContactInfo> contactInfos = new ArrayList<>();
for (int i = 0; i < 10; i++) {
ContactInfo contactInfo = new ContactInfo();
contactInfo.email = "salam " + i;
contactInfo.name = "salam " + i;
contactInfo.surname = "salam " + i;
contactInfos.add(contactInfo);
}
ContactAdapter adapter = new ContactAdapter(contactInfos);
recList.setAdapter(adapter);
}
}
carditem.xml
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
card_view:cardCornerRadius="4dp"
android:layout_margin="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="20dp"
android:background="#color/mainColor"
android:text="contact det"
android:gravity="center_vertical"
android:textColor="#android:color/white"
android:textSize="14dp"/>
<TextView
android:id="#+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:gravity="center_vertical"
android:textSize="10dp"
android:layout_below="#id/title"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"/>
<TextView
android:id="#+id/txtSurname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Surname"
android:gravity="center_vertical"
android:textSize="10dp"
android:layout_below="#id/txtName"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"/>
<TextView
android:id="#+id/txtEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email"
android:textSize="10dp"
android:layout_marginTop="10dp"
android:layout_alignParentRight="true"
android:layout_marginRight="150dp"
android:layout_alignBaseline="#id/txtName"/>
</RelativeLayout>
Go to build.gradle and make sure you're using com.android.support:recyclerview-v7:22.2.0.
It doesn't work on earlier versions.

Categories

Resources