我尝试使用carousel view
中的ViewPager
来Fragment
。{
Fragment
中的另一个ViewPager
也显示MainActivity
。
然而,片段中的ViewPager
未显示ImageView
我认为ViewPager
没问题,因为当我分别更改ViewPager
的背景和ImageView
背景时,只有ImageView
' s背景没有显示。
请帮忙。
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是方便的,它扩展了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>
答案 0 :(得分:0)
你需要在主要活动中设置tablayout和viewpager是这样的:
ViewPager viewPager = (ViewPager) findViewById(R.id.container);
viewPager.setAdapter(jobTitlePagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
希望这有帮助。如果您有疑问,请问我。
答案 1 :(得分:0)
有关设备位图限制的例外情况。此异常不会停止您的应用,但由于设备限制,您无法看到可绘制的背景。每个设备都有自己的限制。不要认为你的绘图是如此之小,如果屏幕上有很多图像,那么你将达到这个限制。
On This Post 如何避免“画布:试图绘制太大(216090000bytes)位图”。异常被解释。
我不是说这是你的问题,但可能是。