안녕하세요, 안드로이드 공부를 하고 있는데요
viewpager 이용해서 화면 제일 위에는 dot indicator를 배치했고
그 아래 글씨랑 그림을 배치 했는데
dot indicator는 정상 작동하는데 그 밑에 글씨랑 그림이 안보입니다.
파일은 레이아웃 : activity_main.xml, activity_sub.xml(2개) / 자바 : MainActivity.java, ViewPagerAdapter.java(2개) 입니다.
Java 코드에서 문제점 찾아서 알려주시면 감사드리겠습니다... 여러 글 보고 참고해서 하다 보니 많이 꼬여있네요 ㅠㅜ (아래와 같이 실행은 되고 dot indicator는 나옵니다.)
MainActivity
package com.example.passtech.test18;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
ViewPager viewpager;
LinearLayout SlidingPage;
private int pageindex;
private ImageView[] imageindex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewpager = (ViewPager) findViewById(R.id.viewPager);
SlidingPage = (LinearLayout) findViewById(R.id.SlidingPage);
ViewPagerAdapter adapter = new ViewPagerAdapter(this);
viewpager.setAdapter(adapter);
pageindex = adapter.getCount();
imageindex = new ImageView[pageindex];
for(int i = 0; i < pageindex; i++){
imageindex[i] = new ImageView(this);
imageindex[i].setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.other_page_dot));
LinearLayout.LayoutParams dotarea = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
dotarea.setMargins(8, 0, 8, 0);
SlidingPage.addView(imageindex[i], dotarea);
}
imageindex[0].setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.current_page_dot));
viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
for(int i = 0; i < pageindex; i++){
imageindex[i].setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.other_page_dot));
}
imageindex[position].setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.current_page_dot));
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
}
ViewPagerAdapter
package com.example.passtech.test18;
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;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ViewPagerAdapter extends PagerAdapter{
private Context context;
ImageView imageview;
TextView textView;
LayoutInflater layoutInflater;
private int[] image = {R.drawable.e11, R.drawable.e22, R.drawable.e33, R.drawable.e44, R.drawable.e55};
private String[] name = {"파이리", "꼬부기", "이상해씨", "구구", "버터풀"};
public ViewPagerAdapter(Context context){
this.context = context;
}
@Override
public int getCount() {
return image.length;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout linearLayout = (LinearLayout) layoutInflater.inflate(R.layout.activity_sub, null );
textView = (TextView) linearLayout.findViewById(R.id.textview);
imageview = (ImageView) linearLayout.findViewById(R.id.imageview);
textView.setText(name[position]);
imageview.setImageResource(image[position]);
container.addView(linearLayout, 0);
return linearLayout;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View)object);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return equals(object);
}
}