을 사용하여 carousel view
을 사용하려고합니다. 이는 Fragment
입니다.
Fragment
도 MainActivity
에있는 다른 ViewPager
에 의해 보여진다.조각 내 ViewPager가 표시되지 않음 ImageView가 표시됩니다.
단편의 ViewPager
에는 ImageView
이 표시되지 않습니다.
나는 ViewPager
의 배경을 변경하고 각각 ImageView
의 배경을 변경했기 때문에, 배경은 ImageView
님의 배경이 표시되지 않았 음을 고려하면 ViewPager
은 괜찮다고 생각합니다.
도와주세요.
MainActivity.java
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 익스텐트있는 편리한입니다.
HomeFragment.java
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;
}
}
fragment_home.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"
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>
VpAdapter.java
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]);
}
}
VpFragment.java
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;
}
}
,
는 item_home_viewpager.xml 당신이 주요 활동에 tablayout 및 viewpager를 설정해야
<?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>
아, tablayout은 표시기에 불과하므로 아무런 이유가 아닙니다. – Jihye
그래서 문제가 무엇입니까? 당신이 정확히 원하는 걸 설명해 주실 수 있습니까? –
필자가 작성한 것처럼 일부 이미지를 표시하기 위해 뷰 페이지를 사용했습니다. 그러나 이미지 뷰는 보이지 않지만 뷰 페이지는입니다. – Jihye