I'm trying to make carousel view
using ViewPager
which is in Fragment
.
Fragment
is also showed by another ViewPager
which is in MainActivity
.
However, ViewPager
in Fragment do not showing ImageView
.
I think ViewPager
is ok, because when I change ViewPager
's background and ImageView
's background respectively, only ImageView
's background was not showed.
Please help.
package com.rudolphriding.giftfriend.activity;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import com.rudolphriding.giftfriend.app.friend.FriendFragment;
import com.rudolphriding.giftfriend.base.BaseActivity;
import com.rudolphriding.giftfriend.app.home.HomeFragment;
import com.rudolphriding.giftfriend.R;
import com.rudolphriding.giftfriend.app.trend.TrendFragment;
public class MainActivity extends BaseActivity
{
ViewPager viewPager;
TabLayout tabLayout;
DrawerLayout drawerLayout;
View friendDrawer;
ImageButton menuButton;
ImageButton slidMenuBackButton;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//view custom action bar
customActionbar();
//initialize tab
initTab();
friendDrawer = (View) findViewById(R.id.drawer);
drawerLayout = (DrawerLayout) findViewById(R.id.activity_main);
menuButton = (ImageButton) findViewById(R.id.actionbar_button_menu);
slidMenuBackButton = (ImageButton) findViewById(R.id.sidemenu_button_close);
//set MainPagerAdapter
viewPager = (ViewPager) findViewById(R.id.activity_main_veiwpager);
MainPagerAdapter pagerAdapter = new MainPagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(pagerAdapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(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) {
}
});
//drawer open by button on action bar
draweOpnebyMenuButton();
//drawer close by button on slide menu
drawerClosebyBackButton();
}
/* /////////////////////////////////////////////////
// //
// About UI //
// //
///////////////////////////////////////////////// */
public void customActionbar()
{
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setCustomView(R.layout.item_main_actionbar);
}
public void initTab()
{
tabLayout = (TabLayout) findViewById(R.id.activity_main_tablayout);
tabLayout.addTab(tabLayout.newTab().setText("선물스토리"));
tabLayout.addTab(tabLayout.newTab().setText("친구스토리"));
}
/* /////////////////////////////////////////////////
// //
// About drawer //
// //
///////////////////////////////////////////////// */
public void draweOpnebyMenuButton()
{
menuButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
drawerLayout.openDrawer(friendDrawer);
}
});
}
public void drawerClosebyBackButton()
{
slidMenuBackButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
drawerLayout.closeDrawer(friendDrawer);
}
});
}
}
BaseActivity is for convenient which extents AppCompatActivity.
package com.rudolphriding.giftfriend.app.home;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
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 com.rudolphriding.giftfriend.R;
import com.rudolphriding.giftfriend.model.HomeItem;
import java.util.ArrayList;
import java.util.List;
public class HomeFragment extends Fragment
{
ViewPager viewPager;
VpAdapter adapter;
TabLayout tabLayout;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
{
View hView = inflater.inflate(R.layout.fragment_home, container, false);
viewPager = (ViewPager) hView.findViewById(R.id.home_viewPager);
adapter = new VpAdapter(getChildFragmentManager());
viewPager.setAdapter(adapter);
tabLayout = (TabLayout) hView.findViewById(R.id.home_dotNavi_tabLayout);
tabLayout.setupWithViewPager(viewPager, true);
return hView;
}
public static HomeFragment newInstance()
{
Bundle args = new Bundle();
HomeFragment fragment = new HomeFragment();
fragment.setArguments(args);
return fragment;
}
}
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/home_viewPager"
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="@color/ut_std_main_mint_01">
</android.support.v4.view.ViewPager>
<android.support.design.widget.TabLayout
android:id="@+id/home_dotNavi_tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/home_viewPager"
app:tabBackground="@drawable/btn_homebanner_round_selector"
app:tabGravity="center"
app:tabIndicatorHeight="0dp">
</android.support.design.widget.TabLayout>
<TableLayout
android:id="@+id/home_buttoncontainer_tableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/home_viewPager"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="30dp"
android:layout_marginBottom="15dp"
android:stretchColumns="*">
<TableRow
android:layout_weight="1"
android:weightSum="4">
<com.rudolphriding.giftfriend.widget.SquareButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_weight="1" />
<com.rudolphriding.giftfriend.widget.SquareButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1"/>
<com.rudolphriding.giftfriend.widget.SquareButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1"/>
<com.rudolphriding.giftfriend.widget.SquareButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"/>
</TableRow>
<TableRow>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:text="기념일 별\n추천 선물"
android:textAlignment="center"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:text="감정으로\n찾는 선물"
android:textAlignment="center"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:text="브랜드\n선물"
android:textAlignment="center"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:text="선물 기획전"
android:textAlignment="center"/>
</TableRow>
</TableLayout>
<Button
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="40dp"
android:background="@drawable/btn_giftfindbg_roundrect_selector"
style="?android:attr/borderlessButtonStyle"
android:text="선물 준비 시작하기"
android:textSize="20sp"
android:textStyle="bold"
android:textAlignment="center"
android:textColor="@color/ut_std_text_gray_01"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
package com.rudolphriding.giftfriend.app.home;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.View;
import com.rudolphriding.giftfriend.R;
public class VpAdapter extends FragmentStatePagerAdapter {
private int[] sampleImg = new int[] { R.drawable.img_home_cardviewbg_01, R.drawable.img_home_cardviewbg_02 };
public VpAdapter(FragmentManager fragmentManager)
{
super(fragmentManager);
}
@Override
public int getCount() {
return sampleImg.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Fragment getItem(int position) {
return VpFragment.create(sampleImg[position]);
}
}
package com.rudolphriding.giftfriend.app.home;
import android.app.Fragment;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.rudolphriding.giftfriend.R;
/**
* Created by LeeJihye on 2017-11-09.
*/
public class VpFragment extends android.support.v4.app.Fragment
{
private int image;
private ViewGroup rootView;
private ImageView imageView;
private Bitmap bitmapImg;
BitmapFactory.Options options;
public static VpFragment create(int image)
{
VpFragment fragment = new VpFragment();
Bundle args = new Bundle();
args.putInt("image", image);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
image = getArguments().getInt("image");
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
rootView = (ViewGroup) inflater.inflate(R.layout.item_home_viewpager, container, false);
options = new BitmapFactory.Options();
options.inSampleSize = 2;
bitmapImg = BitmapFactory.decodeResource(rootView.getResources(), image, options);
imageView = (ImageView) rootView.findViewById(R.id.viewPager_ImageView);
imageView.setImageBitmap(bitmapImg);
return rootView;
}
}
<?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">
<ImageView
android:id="@+id/viewPager_ImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/ut_std_text_gray_01"/>
</LinearLayout>
you need to set tablayout and viewpager in main activity is like this:
ViewPager viewPager = (ViewPager) findViewById(R.id.container);
viewPager.setAdapter(jobTitlePagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
Hope this help..If you have doubt then pls ask me.
There is an exception about bitmap limit of the device. This exception doesn't stop your app but you cannot see the background drawable because of device limit. Every device has it's own limit. Don't think your drawables are so small, if you have a lot of images on the screen, then you will reach that limit.
On This Post How to avoid the "Canvas: trying to draw too large (216090000bytes) bitmap." exception is explained.
I'm not telling this is your problem but it can be.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.