Android viewpager with recyclerview not working in fragment - android

I am working with recyclerview with viewpager in fragment.
I want to make navigation on bottom of screen.
enter image description here
and when I click each button, each fragment show in the frameLayout.
the problem is happen when I enter into the artist fragment second time.(at first time it show up find as I intended.)
as you can see in this image enter image description here
there is noting shows up in the fragment. and swiping the screen not working properly. It should moving stick to the left or right but it stop at the point where I stop moving my finger.
here is my source.
activity_main_board.xml
<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:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/navigation"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true">
</FrameLayout>
<LinearLayout
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="7dp"
android:layout_weight="1"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="fitXY"
app:srcCompat="#drawable/image_background7" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="53dp"
android:layout_weight="1"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/my_board_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView10"
android:layout_width="30dp"
android:layout_height="30dp"
app:srcCompat="#drawable/ic_action_my" />
<TextView
android:id="#+id/textView34"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MY"
android:textSize="10sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/artwork_board_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView13"
android:layout_width="30dp"
android:layout_height="30dp"
app:srcCompat="#drawable/ic_action_artwork" />
<TextView
android:id="#+id/textView35"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Artwork"
android:textSize="10sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/artist_board_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView14"
android:layout_width="30dp"
android:layout_height="30dp"
app:srcCompat="#drawable/ic_action_artist" />
<TextView
android:id="#+id/textView36"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Artist"
android:textSize="10sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/alarm_board_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView20"
android:layout_width="30dp"
android:layout_height="30dp"
app:srcCompat="#drawable/ic_action_alert" />
<TextView
android:id="#+id/textView40"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alarm"
android:textSize="10sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/menu_board_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView19"
android:layout_width="30dp"
android:layout_height="30dp"
app:srcCompat="#drawable/ic_action_menu" />
<TextView
android:id="#+id/textView38"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="전체"
android:textSize="10sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
MainBoradActivity.java
public class MainBoardActivity extends AppCompatActivity implements View.OnClickListener{
private static LinearLayout my_board_button;
private static LinearLayout artwork_board_button;
private static LinearLayout artist_board_button;
private static LinearLayout menu_board_button;
private static LinearLayout alarm_board_button;
private static FrameLayout container;
private static MyBoardFragment myBoardFragment;
private static ArtworkBoardFragment artworkBoardFragment;
private static ArtistBoardFragment artistBoardFragment;
private static AlarmBoardFragment alarmBoardFragment;
private static MenuBoardFragment menuBoardFragment;
private static FragmentManager fragmentManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_board);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
container = (FrameLayout) findViewById(R.id.container);
my_board_button = (LinearLayout) findViewById(R.id.my_board_button);
artwork_board_button = (LinearLayout) findViewById(R.id.artwork_board_button);
artist_board_button = (LinearLayout) findViewById(R.id.artist_board_button);
menu_board_button = (LinearLayout) findViewById(R.id.menu_board_button);
alarm_board_button = (LinearLayout) findViewById(R.id.alarm_board_button);
my_board_button.setOnClickListener(this);
artwork_board_button.setOnClickListener(this);
artist_board_button.setOnClickListener(this);
menu_board_button.setOnClickListener(this);
alarm_board_button.setOnClickListener(this);
myBoardFragment = new MyBoardFragment();
artworkBoardFragment = new ArtworkBoardFragment();
artistBoardFragment = new ArtistBoardFragment();
alarmBoardFragment = new AlarmBoardFragment();
menuBoardFragment = new MenuBoardFragment();
fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().add(R.id.container, myBoardFragment).commitAllowingStateLoss();
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.my_board_button:
FragmentTransaction myTransaction = fragmentManager.beginTransaction();
myTransaction.replace(R.id.container, myBoardFragment);
myTransaction.addToBackStack("0");
myTransaction.commitAllowingStateLoss();
break;
case R.id.artwork_board_button:
FragmentTransaction artworkTransaction = fragmentManager.beginTransaction();
artworkTransaction.replace(R.id.container, artworkBoardFragment);
artworkTransaction.addToBackStack("1");
artworkTransaction.commitAllowingStateLoss();
break;
case R.id.artist_board_button:
FragmentTransaction artistTransaction = fragmentManager.beginTransaction();
artistTransaction.replace(R.id.container, artistBoardFragment);
artistTransaction.addToBackStack("2");
artistTransaction.commitAllowingStateLoss();
break;
case R.id.alarm_board_button:
FragmentTransaction alarmTransaction = fragmentManager.beginTransaction();
alarmTransaction.replace(R.id.container, alarmBoardFragment);
alarmTransaction.addToBackStack("3");
alarmTransaction.commitAllowingStateLoss();
break;
case R.id.menu_board_button:
FragmentTransaction menuTransaction = fragmentManager.beginTransaction();
menuTransaction.replace(R.id.container, menuBoardFragment);
menuTransaction.addToBackStack("4");
menuTransaction.commitAllowingStateLoss();
break;
}
}
}
ArtistBoardFragment.java
public class ArtistBoardFragment extends Fragment {
private static TabLayout artist_tabs;
private static ViewPager artist_container;
private static FragmentPagerAdapter pageAdapter;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_artist_board, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
artist_tabs = (TabLayout) view.findViewById(R.id.artist_tabs);
artist_container = (ViewPager) view.findViewById(R.id.artist_container);
pageAdapter = new FragmentPagerAdapter(getActivity().getSupportFragmentManager()) {
ArtistNewFragment artistNewFragment = new ArtistNewFragment();
ArtistCategoryFragment artistCategoryFragment = new ArtistCategoryFragment();
private final String[] menuFragmentNames = new String[]{
"new",
"category"
};
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
Bundle recentBundle = new Bundle();
recentBundle.putInt("page", position);
artistNewFragment.setArguments(recentBundle);
return artistNewFragment;
case 1:
Bundle bestBundle = new Bundle();
bestBundle.putInt("page", position);
artistCategoryFragment.setArguments(bestBundle);
return artistCategoryFragment;
default:
return null;
}
}
#Override
public int getCount() {
return menuFragmentNames.length;
}
#Override
public CharSequence getPageTitle(int position) {
return menuFragmentNames[position];
}
};
artist_container.setAdapter(pageAdapter);
artist_container.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
artist_tabs.setupWithViewPager(artist_container);
}
#Override
public void onResume() {
super.onResume();
}
}
ArtistNewFragment.java
public class ArtistNewFragment extends Fragment {
private static RecyclerView new_content_list;
int pastVisibleItems, visibleItemCount, totalItemCount;
private static TimelineAdapter timelineAdapter;
private static RequestManager requestManager;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_artist_new_content, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
new_content_list = (RecyclerView) view.findViewById(R.id.new_content_list);
requestManager = Glide.with(getActivity());
final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL,false);
new_content_list.setLayoutManager(layoutManager);
new_content_list.setHasFixedSize(true);
new_content_list.setNestedScrollingEnabled(false);
//final NotiAdapter notiAdapter = new NotiAdapter(getApplicationContext(), FunctionBase.createFilter, false, lastCheckTime);
timelineAdapter = new TimelineAdapter(getActivity(), requestManager);
timelineAdapter.setObjectsPerPage(3);
new_content_list.setAdapter(timelineAdapter);
new_content_list.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
Log.d("dx", String.valueOf(dx));
Log.d("dy", String.valueOf(dy));
if(dy > 0) {
visibleItemCount = layoutManager.getChildCount();
totalItemCount = layoutManager.getItemCount();
pastVisibleItems = layoutManager.findFirstVisibleItemPosition();
if ( (visibleItemCount + pastVisibleItems) >= totalItemCount) {
timelineAdapter.loadNextPage();
}
}
}
});
}
#Override
public void onResume() {
super.onResume();
timelineAdapter.loadObjects(0);
}
}

It's hard to tell exactly due to the navigational structure of your app, but think the solution to your problem is to use the childFragmentManager inside of the ArtistBoardFragment. i.e, instead of
pageAdapter = new FragmentPagerAdapter(getActivity().getSupportFragmentManager()) ...
Use
pageAdapter = new FragmentPagerAdapter(getChildFragmentManager()) ...

Related

Create Imageview when I click recyclerView item in tabLayout with ViewPager2

First, my app is like this.
I want to add imageview to fragment 1 when I click recyclerview item but imageview doesn't appear
I don't know the problem
Thank you in advance!
I try this :
SpaceEditAdapter.java (recyclerView adapter)
public class SpaceEditAdapter extends RecyclerView.Adapter<SpaceEditAdapter.ItemViewHolder> {
public ArrayList<SpaceEditData> spaceData =new ArrayList<>();
#NonNull
#Override
public ItemViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType ){
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_space_edit_item,parent,false);
}
#Override
public void onBindViewHolder(#NonNull ItemViewHolder holder, int position){
holder.onBind(spaceData.get(position));
}
#Override
public int getItemCount(){
return spaceData.size();
}
public void addItem(SpaceEditData d){
spaceData.add(d);
}
class ItemViewHolder extends RecyclerView.ViewHolder{
ImageView myItemImageView;
ItemViewHolder(View itemView){
super(itemView);
myItemImageView=itemView.findViewById(R.id.myItemImg);
myItemImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int pos = getAdapterPosition() ;
if (pos != RecyclerView.NO_POSITION) {
SpaceEditData item=spaceData.get(pos);
Bundle bundle=new Bundle();
bundle.putSerializable("num", item);
SpaceFragment sf=new SpaceFragment();
sf.setArguments(bundle);
}
}
});
}
void onBind(SpaceEditData data){
myItemImageView.setImageResource(data.getMyItemImg());
}
}
}
SpaceFragment.java(fragment1 in image)
public class SpaceFragment extends Fragment {
private View v;
private ViewPager2 viewPager;
private TabLayout tabLayout;
private SpaceEditPaintFragment paintF;
private SpaceEditForestFragment forestF;
private SpaceEditSeaFragment seaF;
private SpaceEditMudFragment mudF;
private int count=1;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v=inflater.inflate(R.layout.fragment_space, container, false);
createFragment();
createViewPager();
settingTabLayout();
ImageView imageView = new ImageView(v.getContext());
Bundle bundle=getArguments();
if(bundle!=null) {
SpaceEditData data=(SpaceEditData)bundle.getSerializable("num");
ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(layoutParams);
imageView.setImageResource(data.getMyItemImg());
spaceLay.addView(imageView);
}
return v;
}
private void createFragment(){
paintF=new SpaceEditPaintFragment();
forestF=new SpaceEditForestFragment();
seaF=new SpaceEditSeaFragment();
mudF=new SpaceEditMudFragment();
}
private void createViewPager(){
viewPager = (ViewPager2)v.findViewById(R.id.spaceEditViewpager);
SpaceEditViewPagerAdapter pagerAdapter=new SpaceEditViewPagerAdapter(getFragmentManager(),getLifecycle());
pagerAdapter.addFragment(paintF);
pagerAdapter.addFragment(forestF);
pagerAdapter.addFragment(seaF);
pagerAdapter.addFragment(mudF);
viewPager.setAdapter(pagerAdapter);
viewPager.setUserInputEnabled(false);
}
private void settingTabLayout(){
tabLayout=(TabLayout)v.findViewById(R.id.spaceEditTabLayout);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){
#Override
public void onTabSelected(TabLayout.Tab tab) {
int pos = tab.getPosition();
switch(pos){
case 0:
viewPager.setCurrentItem(0);
break;
case 1:
viewPager.setCurrentItem(1);
break;
case 2:
viewPager.setCurrentItem(2);
break;
case 3:
viewPager.setCurrentItem(3);
break;
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
}
SpaceEditData.java(RecyclerView item)
import java.io.Serializable;
public class SpaceEditData implements Serializable {
private int myItemImg;
public int getMyItemImg() {
return myItemImg;
}
public void setMyItemImg(int myItemImg) {
this.myItemImg = myItemImg;
}
}
fragment_space.xml(fragment1 in image)
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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/spaceShopFLay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/lightBeige"
tools:context=".SpaceFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/spaceLay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/lightBeige">
<ImageView
android:id="#+id/spaceShop"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="280dp"
android:layout_marginLeft="280dp"
android:layout_marginTop="20dp"
android:clickable="true"
android:contentDescription="#string/imageView"
android:focusable="true"
android:src="#drawable/ic_shop"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/spaceEdit"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="20dp"
android:layout_marginStart="25dp"
android:layout_marginLeft="25dp"
android:contentDescription="#string/imageView"
android:src="#drawable/ic_edit"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="#id/spaceShop"
android:clickable="true"
android:focusable="true" />
<com.example.zeve.StickerView
android:id="#+id/stickerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/spaceEditLay"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="404dp"
android:background="#color/brown">
<com.google.android.material.tabs.TabLayout
android:id="#+id/spaceEditTabLayout"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:tabGravity="center">
<com.google.android.material.tabs.TabItem
android:id="#+id/editPaintTab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/paint"></com.google.android.material.tabs.TabItem>
<com.google.android.material.tabs.TabItem
android:id="#+id/editForestTab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/forest"></com.google.android.material.tabs.TabItem>
<com.google.android.material.tabs.TabItem
android:id="#+id/editSeaTab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sea"></com.google.android.material.tabs.TabItem>
<com.google.android.material.tabs.TabItem
android:id="#+id/editMudTab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/mud"></com.google.android.material.tabs.TabItem>
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/spaceEditViewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:layout_marginTop="40dp"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>
fragment_space_edit_item.xml(recyclerView item 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"
android:layout_width="75dp"
android:layout_height="60dp"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:orientation="vertical">
<ImageView
android:id="#+id/myItemImg"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="5dp"
android:layout_gravity="center"
android:clickable="true"
android:focusable="true"/>
android:background="#color/lightBeige"
android:contentDescription="#string/imageView"
</LinearLayout>
It's silly but in your fragment_space_edit_item.xml
<ImageView
android:id="#+id/myItemImg"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="5dp"
android:layout_gravity="center"
android:clickable="true"
android:focusable="true"/>
android:background="#color/lightBeige"
android:contentDescription="#string/imageView"
background and contentDescription are Error if that is the case.

ViewPager with FragmentPagerAdapter Not showing Content

I am trying to use Fragment
This is my activity class:
public class InterfaceAct extends FragmentActivity implements View.OnClickListener {
private ViewPager mViewPager;
private List<Fragment> datas;
private ViewPagerFragmentAdapter viewPagerFragmentAdapter;
private LinearLayout mLLHome,mLLTimer,mLLEdit,mLLAbout;
private ImageView mImageViewHome,mImageViewTimer,mImageViewEdit,mImageViewAbout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activiy_interface);
initDatas();// Init Data For Fragments
initView();// Init Component
initEvent();// Sign for Click Listener
viewPagerFragmentAdapter=new ViewPagerFragmentAdapter(getSupportFragmentManager(),datas);// Init Adpater Class
mViewPager.setAdapter(viewPagerFragmentAdapter);
}
private void initDatas() {
datas=new ArrayList<Fragment>();
datas.add(new MyFragment1());
datas.add(new MyFragment2());
datas.add(new MyFragment3());
datas.add(new MyFragment4());
}
private void initEvent() {
mLLHome.setOnClickListener(this);
mLLTimer.setOnClickListener(this);
mLLEdit.setOnClickListener(this);
mLLAbout.setOnClickListener(this);
mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {//ViewPager Scrolling Switch Listner
#Override
public void onPageSelected(int arg0) {
int currentItem=mViewPager.getCurrentItem();
resetImag();
switch (currentItem) {
case 0:
mImageViewHome.setImageResource(R.drawable.home_yes);
break;
case 1:
mImageViewTimer.setImageResource(R.drawable.timer_yes);
break;
case 2:
mImageViewEdit.setImageResource(R.drawable.edit_yes);
break;
case 3:
mImageViewAbout.setImageResource(R.drawable.about_yes);
break;
default:
break;
}
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
private void initView() {
mViewPager = (ViewPager) findViewById(R.id.viewpager);
mLLHome = (LinearLayout) findViewById(R.id.ll_home);
mLLTimer = (LinearLayout) findViewById(R.id.ll_timer);
mLLEdit = (LinearLayout) findViewById(R.id.ll_edit);
mLLAbout = (LinearLayout) findViewById(R.id.ll_about);
mImageViewHome = (ImageView) findViewById(R.id.img_home);
mImageViewTimer = (ImageView) findViewById(R.id.img_timer);
mImageViewEdit = (ImageView) findViewById(R.id.img_edit);
mImageViewAbout = (ImageView) findViewById(R.id.img_about);
}
#Override
public void onClick(View v) {
resetImag();
switch (v.getId()) {
case R.id.ll_home:
mViewPager.setCurrentItem(0);
mImageViewHome.setImageResource(R.drawable.home_yes);
break;
case R.id.ll_timer:
mViewPager.setCurrentItem(1);
mImageViewTimer.setImageResource(R.drawable.timer_yes);
break;
case R.id.ll_edit:
mViewPager.setCurrentItem(2);
mImageViewEdit.setImageResource(R.drawable.edit_yes);
break;
case R.id.ll_about:
mViewPager.setCurrentItem(3);
mImageViewAbout.setImageResource(R.drawable.about_yes);
break;
default:
break;
}
}
private void resetImag() {// Reset Picture
mImageViewHome.setImageResource(R.drawable.home_no);
mImageViewTimer.setImageResource(R.drawable.timer_no);
mImageViewEdit.setImageResource(R.drawable.edit_no);
mImageViewAbout.setImageResource(R.drawable.about_no);
}
}
The ViewPagerFragmentAdapter Looks like this:
public class ViewPagerFragmentAdapter extends FragmentStatePagerAdapter {
private List<Fragment> datas;
public ViewPagerFragmentAdapter(FragmentManager fm,List<Fragment> datas) {
super(fm);
this.datas=datas;
}
#Override
public Fragment getItem(int position) {// Return View Object
return datas.get(position);
}
#Override
public int getCount() {// Return View Num
return datas.size();
}
#Override
public Object instantiateItem(ViewGroup container, int position) {// Init View
return super.instantiateItem(container, position);
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {// Destroy View
super.destroyItem(container, position, object);
}
}
The layout for the page, it includes a viewpager and also a menu bar in linear layout:
?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.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="449dp"
android:layout_weight="1"></android.support.v4.view.ViewPager>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#ffffffff"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/ll_home"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/img_home"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#0000"
android:src="#drawable/home_yes" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home"
android:textColor="#b6b3b3" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll_timer"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/img_timer"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#0000"
android:src="#drawable/timer_no" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Timer"
android:textColor="#b6b3b3" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll_edit"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/img_edit"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#0000"
android:src="#drawable/edit_no" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edit"
android:textColor="#b6b3b3" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll_about"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/img_about"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#0000"
android:src="#drawable/about_no" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="About"
android:textColor="#b6b3b3" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
And This is one of the fragment:
public class MyFragment1 extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.tab1,null);
}
}
I have four fragment prepared for viewpager(they are basiclly the same but with differnt layout), here is what the tab1 layout might look like:
<?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:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Chat"
android:textSize="30sp" >
</TextView>
</LinearLayout>
And I only got the menu bar when I run it, the viewpager is not set and I don't know why
Remove android:layout_weight="1" <Viewpager> and add android:orientation="vertical" to Linear layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="449dp"
></android.support.v4.view.ViewPager>
//-----------

I have a Spinner field in custom layout of adapter. problem to load data position wise

Code
AuthorlistSpinnerAdapter authorlistSpinnerAdapter = new AuthorlistSpinnerAdapter (context, booksDetailsArrayList);
holder.spinner_product_details.setAdapter(authorlistSpinnerAdapter );
/**
*
*/
private void showMakeDialog() {
if (mValuationParametersData == null) {
getParameters(true);
return;
}
String make = LiteralsStr.BUY_SELECT_MAKE;
final List<AttributeUI> attributeUIList = new ArrayList<>();
List<Make> makeList = mValuationParametersData.getMakes();
for (int i = 0; i < makeList.size(); i++) {
AttributeUI attributeUI = new AttributeUI();
attributeUI.setAttributeStr(makeList.get(i).getName());
attributeUIList.add(attributeUI);
}
SpinnerDialogFragment fragment = SpinnerDialogFragment.getInstance(mMakePosition, make, attributeUIList, new SpinnerItemCheckListener() {
#Override
public void onCheckChange(int position) {
mMakePosition = position;
mMakeId = mValuationParametersData.getMakes().get(position).getId();
mBinding.edtMake.edtAttribute.setText(attributeUIList.get(position).getAttributeStr());
mBinding.edtModel.edtAttribute.setText("");
mBinding.edtYear.edtAttribute.setText("");
mModelPosition = -1;
mYearPosition = -1;
}
});
fragment.show(getActivity().getFragmentManager(), "dialog");
}
public class SpinnerDialogFragment extends DialogFragment implements View.OnClickListener {
public static final String TAG = SpinnerDialogFragment.class.getCanonicalName();
private static List<AttributeUI> mAttributeUIList;
private SpinnerAdapter mSpinnerAdapter;
private static SpinnerItemCheckListener spinnerItemCheckListener;
private RecyclerView rvSpinner = null;
private TextView tvCancel, tvDone, tvTitle;
private static String mTitle = "";
private static int mPosition = -1;
public static SpinnerDialogFragment getInstance(int position, String pTitle, List<AttributeUI> pAttributeUIList, SpinnerItemCheckListener pSpinnerItemCheckListener) {
mPosition = position;
mTitle = pTitle;
mAttributeUIList = pAttributeUIList;
spinnerItemCheckListener = pSpinnerItemCheckListener;
SpinnerDialogFragment fragment = new SpinnerDialogFragment();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Translucent);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_app_spinner, container);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dismiss();
}
});
// Alert Dialog cancelable false.
setCancelable(true);
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
rvSpinner = (RecyclerView) (view).findViewById(R.id.rvSpinner);
tvCancel = (TextView) (view).findViewById(R.id.tvCancel);
tvCancel.setOnClickListener(this);
tvDone = (TextView) (view).findViewById(R.id.tvDone);
tvDone.setOnClickListener(this);
tvTitle = (TextView) view.findViewById(R.id.tvTitle);
tvTitle.setText(mTitle);
tvDone.setText(LiteralsStr.CHANGE_PASSWORD_DONE);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
rvSpinner.setLayoutManager(layoutManager);
mSpinnerAdapter = new SpinnerAdapter(mAttributeUIList, new SpinnerItemCheckListener() {
#Override
public void onCheckChange(int position) {
mPosition = position;
}
});
rvSpinner.setAdapter(mSpinnerAdapter);
smoothScroll();
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tvCancel:
dismiss();
break;
case R.id.tvDone:
if (mPosition == -1) {
Util.showToastMsg(getActivity(), getString(R.string.please) + mTitle);
return;
}
if (spinnerItemCheckListener != null)
spinnerItemCheckListener.onCheckChange(mPosition);
dismiss();
break;
default:
break;
}
}
private void smoothScroll() {
if (mPosition < 0)
return;
rvSpinner.getLayoutManager().scrollToPosition(mPosition);
mAttributeUIList.get(mPosition).setSelected(true);
}
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:layoutDirection="ltr">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:alpha="0.85"
android:background="#color/colorGray"
android:gravity="center"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="#dimen/_20sdp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="#dimen/_30sdp"
android:background="#drawable/rounded_view_blue_top"
android:gravity="center"
android:orientation="horizontal"
android:paddingStart="#dimen/_12sdp"
android:paddingEnd="#dimen/_10sdp">
<com.ecomplanners.topcar.customfontview.FontTextView
android:id="#+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start"
android:padding="#dimen/_5sdp"
android:text=""
android:textColor="#color/colorWhite"
android:textSize="#dimen/_12sdp"
app:customFont="#string/HelveticaNeueLTStd_65_Medium"
tools:text="Title" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/rvSpinner"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/colorTransparent"
android:clipToPadding="false"
android:fadeScrollbars="true"
android:nestedScrollingEnabled="false"
android:overScrollMode="never"
android:scrollIndicators="none"
android:scrollbars="none"
tools:listitem="#layout/item_app_spinner" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="#dimen/_35sdp"
android:gravity="center_vertical"
android:orientation="horizontal"
android:weightSum="10">
<com.ecomplanners.topcar.customfontview.FontTextView
android:id="#+id/tvCancel"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:background="#drawable/bottom_left_rounded_rectangle_gray"
android:gravity="center"
android:padding="#dimen/_8sdp"
android:text="#string/cancel"
android:textColor="#color/colorBlue"
android:textSize="#dimen/_10sdp"
app:customFont="#string/HelveticaNeueLTStd_65_Medium" />
<com.ecomplanners.topcar.customfontview.FontTextView
android:id="#+id/tvDone"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:background="#drawable/bottom_right_rounded_rectangle_blue"
android:gravity="center"
android:padding="#dimen/_8sdp"
android:text="#string/done"
android:textColor="#color/colorWhite"
android:textSize="#dimen/_10sdp"
app:customFont="#string/HelveticaNeueLTStd_65_Medium" />
</LinearLayout>
</LinearLayout>
</FrameLayout>

how to set fragment in activity and add cardview in recyclerview

I am trying to build a e-commerce app, now I am stucked here. When try to add fragment class in my activity some errors showing.
when I open my fragment recyclerview items bottom margin is too long, its adjusting automatically.
when I back to activity fragment class items and activity items all showing
when I back a blank page is shown
my codes are below
//this is my activity.xml
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >
<FrameLayout
android:id="#+id/fragment_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
In this activity.xml had a frame layout.i try to add fragment in this frame layout. and i have two fragnent class.fragment one and two
This is my one_fragment.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/app_bar"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar"
android:layout_margin="16dp">
<android.support.v7.widget.RecyclerView
android:id="#+id/old_order_recycle"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
</RelativeLayout>
//two_fragment.xml
<android.support.v4.widget.NestedScrollView
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:orientation="vertical"
tools:context="com.example.sirajmalayil.sneakers.OrderActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="2dp"
app:titleTextAppearance="#style/Toolbar.TitleText"
android:minHeight="?attr/actionBarSize"
android:background="#color/colorPrimaryDark"/>
<LinearLayout
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="20dp"
android:layout_below="#+id/toolbar"
android:background="#color/colorWhite">
<TextView
android:text="no active orders"
android:textAllCaps="true"
android:textStyle="bold"
android:layout_marginTop="20dp"
android:textSize="14sp"
android:fontFamily="sans-serif"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="There are no recent orders to show."
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:fontFamily="sans-serif"
android:textSize="12sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:src="#mipmap/footprints"
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="100dp" />
<TextView
android:id="#+id/txt_start_shopping"
android:text="start shopping"
android:background="#drawable/login_btn"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp"
android:layout_gravity="center"
android:textAllCaps="true"
android:textColor="#color/colorPink"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:id="#+id/txt_old_orders"
android:text="show my old orders"
android:gravity="center"
android:padding="10dp"
android:textAllCaps="true"
android:layout_margin="16dp"
android:textColor="#color/colorPink"
android:textStyle="bold"
android:background="#drawable/rectangle_colored_bg"
android:layout_below="#+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
//this is my two_fragment class
public class two_Fragment extends Fragment {
private TextView txtStartShoppi,txtoldOrders;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_active_orders,container,false);
txtStartShoppi = (TextView) rootView.findViewById(R.id.txt_start_shopping);
txtoldOrders = (TextView) rootView.findViewById(R.id.txt_old_orders);
Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
Drawable drawable = ContextCompat.getDrawable(getActivity(),R.drawable.ic_close_black_24dp);
toolbar.setNavigationIcon(drawable);
AppCompatActivity activity = (AppCompatActivity) getActivity();
activity.setSupportActionBar(toolbar);
ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
actionBar.setTitle("ORDERS");
toolbar.setTitleTextColor(Color.WHITE);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
clickListeners();
return rootView;
}
private void clickListeners() {
txtStartShoppi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getActivity(),MainActivity.class));
}
});
txtoldOrders.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment fragment = new OldOrderFragment();
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.fragment_frame,fragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
}
}
//this is my activity class
public class OrderActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);
if (savedInstanceState == null) {
Fragment fragment = new ActiveOrdersFragment();
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.fragment_frame, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
}
//this is my one_fragment
public class one_fragment extends Fragment {
RecyclerView recyclerView;
private List<Orders> ordersList = new ArrayList<>();
private List<products> productList = new ArrayList<>();
OldOrdersAdapter oldOrdersAdapter;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_old_orders,container,false);
Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
AppCompatActivity activity = (AppCompatActivity) getActivity();
activity.setSupportActionBar(toolbar);
ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
actionBar.setTitle("MY ARCHIVED ORDERS");
toolbar.setTitleTextColor(Color.WHITE);
actionBar.setDisplayHomeAsUpEnabled(true);
prepareOrderList();
recyclerView = (RecyclerView) rootView.findViewById(R.id.old_order_recycle);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
oldOrdersAdapter = new OldOrdersAdapter(getActivity(),ordersList);
recyclerView.setAdapter(oldOrdersAdapter);
return rootView;
}
private void prepareOrderList() {
Orders orders = new Orders("delivered","1245-5566-7890-1123","Placed on Sunday, December, 2017",
"Rs.750.45","Friday, January 5, 2018");
ordersList.add(orders);
orders = new Orders("delivered","1245-5566-7890-1123","Placed on Sunday, December, 2017",
"Rs.750.45","Friday, January 5, 2018");
ordersList.add(orders);
orders = new Orders("delivered","1245-5566-7890-1123","Placed on Sunday, December, 2017",
"Rs.750.45","Friday, January 5, 2018");
ordersList.add(orders);
orders = new Orders("delivered","1245-5566-7890-1123","Placed on Sunday, December, 2017",
"Rs.750.45","Friday, January 5, 2018");
ordersList.add(orders);
//oldOrdersAdapter.notifyDataSetChanged();
}
}
//this is my Adapter class
public class OldOrdersAdapter extends RecyclerView.Adapter<OldOrdersAdapter.MyViewHolder> {
Context context;
private List<Orders> ordersList = new ArrayList<>();
public OldOrdersAdapter(Context context, List<Orders> ordersList) {
this.context = context;
this.ordersList = ordersList;
}
#NonNull
#Override
public OldOrdersAdapter.MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_old_orders,parent,false);
return new MyViewHolder(rootView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Orders orders = ordersList.get(position);
holder.orderStatus.setText(orders.getOrder_status());
holder.orderNo.setText(orders.getOrder_no());
holder.orderPlacedDate.setText(orders.getOrder_placed_date());
holder.orderdItemPrice.setText(orders.getOrder_item_price());
holder.orderDeliverDate.setText(orders.getOrder_delivered_date());
}
#Override
public int getItemCount() {
return ordersList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
private TextView orderStatus,orderNo,orderPlacedDate,orderdItemPrice,orderDeliverDate;
public MyViewHolder(View itemView) {
super(itemView);
orderStatus = (TextView) itemView.findViewById(R.id.txt_order_status);
orderNo = (TextView) itemView.findViewById(R.id.txt_order_no);
orderPlacedDate = (TextView) itemView.findViewById(R.id.txt_order_placed_date);
orderdItemPrice = (TextView) itemView.findViewById(R.id.txt_order_price);
orderDeliverDate = (TextView) itemView.findViewById(R.id.txt_order_item_deliver_date);
}
}
}
Ok, all codes here.iam new in android . so i dont know whtais the problrm here. some time fragment and activity contents are mixing..
the problems are only in xml file.not in programattic code
Try this code..
set both fragment background color into root attribute it means Relative layout tag.
android:background="#android:color/white"
and card view refer this link it give good demo for that..
https://www.androidhive.info/2016/05/android-working-with-card-view-and-recycler-view/
add fragment into activity then call onCreate method in below code..
Fragment fragment = new MainFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_frame, fragment, fragment.getClass().getSimpleName()).addToBackStack(null).commit();

ViewPager inside DialogFragment

So I know this has been answered multiple times, such as Fragment Inside Fragment. I am using the support library for getChildFragmentManager() as advised in the post. The issue is that my viewpager's fragments are not appearing despite being inflated. My tabs (with icons) are showing perfectly fine. Here is the relevant code:
DialogFragment:
public class DialogFragment extends DialogFragment {
private static final String ARG_PAGE = "ARG_PAGE";
private int mPage;
private HotSpot hotSpot;
private Gson gson = new Gson();
private Map nonEmptyPerks;
private TabLayout tabLayout;
private ViewPager viewPager;
public DialogFragment() {
// Empty constructor required for DialogFragment
}
public static PerkDetailDialogFragment newInstance(int page, String hotSpotJson) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
args.putString("HOTSPOTDETAILED", hotSpotJson);
PerkDetailDialogFragment fragment = new PerkDetailDialogFragment();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARG_PAGE);
String json = getArguments().getString("HOTSPOTDETAILED");
hotSpot = gson.fromJson(json, HotSpot.class);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_dialog, parent, false);
// Get the ViewPager and set it's PagerAdapter so that it can display items
viewPager = (ViewPager) view.findViewById(R.id.viewpager);
tabLayout = (TabLayout) view.findViewById(R.id.perkSelector);
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
nonEmptyPerks = getNonEmptyPerks(hotSpot.getPerks());
PerkFragmentPagerAdapter pagerAdapter =
new PerkFragmentPagerAdapter(getChildFragmentManager(), nonEmptyPerks.size());
viewPager.setAdapter(pagerAdapter);
// Give the TabLayout the ViewPager
tabLayout.setupWithViewPager(viewPager);
// ..... More code here
}
#Override
public void onStart() {
super.onStart();
getDialog().getWindow().setLayout(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
}
public class PerkFragmentPagerAdapter extends FragmentPagerAdapter {
private int pageCount;
public PerkFragmentPagerAdapter(FragmentManager fm, int pageCount) {
super(fm);
this.pageCount = pageCount;
}
#Override
public int getCount() {
return pageCount;
}
#Override
public Fragment getItem(int position) {
String perk = "temp";
return PerkFragment.newInstance(position + 1, perk);
}
}
}
PerkFragment:
public class PerkFragment extends Fragment {
private static final String ARG_PAGE = "ARG_PAGE";
private static final String ARG_PERK_DESCRIPTION = "PERK_DESCRIPTION";
private int mPage;
private String perkDescription;
public static PerkFragment newInstance(int page, String perk) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
args.putString(ARG_PERK_DESCRIPTION, perk);
PerkFragment fragment = new PerkFragment();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARG_PAGE);
perkDescription = getArguments().getString(ARG_PERK_DESCRIPTION);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_perk, container, false);
TextView perkDescriptionTextView = (TextView) view.findViewById(R.id.perkDescription);
perkDescriptionTextView.setText(perkDescription);
return view;
}
}
fragment_dialog.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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/hotSpotImg"
android:layout_width="match_parent"
android:layout_height="250dp"
android:scaleType="centerCrop"
android:src="#drawable/img_venue_placeholder" />
<ImageView
android:id="#+id/iconBackArrow"
android:layout_width="50dp"
android:layout_height="60dp"
android:layout_alignStart="#+id/hotSpotImg"
android:layout_alignTop="#+id/hotSpotImg"
android:src="#drawable/img_back_arrow_shadow" />
<ImageView
android:id="#+id/iconGPS"
android:layout_width="50dp"
android:layout_height="60dp"
android:layout_alignBottom="#+id/hotSpotImg"
android:layout_alignStart="#+id/hotSpotImg"
android:src="#drawable/img_gps" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/hotSpotImg">
<TextView
android:id="#+id/memberSpace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:text="#string/member_space"
android:textAllCaps="true"
android:textColor="#color/grey_2"
android:textSize="16sp" />
<com.test.test.helpers.LetterSpacingTextView
android:id="#+id/hotSpotName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/memberSpace"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:text="That filler info"
android:textAlignment="center"
android:textAllCaps="true"
android:textColor="#color/black"
android:textSize="22sp" />
<android.support.design.widget.TabLayout
android:id="#+id/perkSelector"
style="#style/MyCustomTabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/hotSpotName"
android:layout_marginBottom="10dp"
app:tabGravity="center" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp" />
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/checkIn"
android:layout_below="#id/perkSelector"
android:layout_centerHorizontal="true">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingEnd="20dp"
android:paddingStart="20dp">
<TextView
android:id="#+id/hotSpotHours"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="#string/hours"
android:textColor="#color/grey"
android:textSize="14sp" />
<TextView
android:id="#+id/hotSpotDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/hotSpotHours"
android:layout_marginTop="20dp"
android:lineSpacingExtra="10dp"
android:text="FILLER DESCRIPTION"
android:textAlignment="center"
android:textColor="#color/grey"
android:textSize="16sp" />
</RelativeLayout>
</ScrollView>
<TextView
android:id="#+id/checkIn"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:background="#drawable/text_view_button"
android:clickable="true"
android:text="#string/check_in"
android:textAlignment="center"
android:textAllCaps="true" />
</RelativeLayout>
</RelativeLayout>
fragment_perk.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/perkDescription"
style="#style/MyCustomTextAppearance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PERK FILLER"
android:textColor="#color/magnises_grey"
android:textSize="18sp" />
The issue is that the viewpager should include tablayout inside the viewpager tags. This fixes the problem.
e.g:
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
<android.support.design.widget.TabLayout
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/MY.DIALOG"
card_view:tabTextColor="#666666"
card_view:tabSelectedTextColor="#666666" />
</android.support.v4.view.ViewPager>

Categories

Resources