ViewPager에 대한 SimpleViewPagerIndicator 설정
12481 단어 android
나는 여기에서 다시 한 번 말하지만, 단지 그것의 실현 방법을 시험해 보고 싶을 뿐이다.
기록해 두면 나중에 빠르게 수정 교체할 수 있어요~~~
아주 간단한 종류:
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
public class SimpleViewPagerIndicator extends LinearLayout
{
private static final int COLOR_TEXT_NORMAL = 0xFF999999;
private static final int COLOR_INDICATOR = Color.BLACK;
private String[] mTitles;
private int mTabCount;
private int mIndicatorColor = COLOR_INDICATOR;
private float mTranslationX;
private Paint mPaint = new Paint();
private int mTabWidth;
private int oldPosition = 0;
private ViewPager viewPager;
public SimpleViewPagerIndicator(Context context)
{
this(context, null);
}
public SimpleViewPagerIndicator(Context context, AttributeSet attrs)
{
super(context, attrs);
mPaint.setColor(mIndicatorColor);
mPaint.setStrokeWidth(10.0F);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
super.onSizeChanged(w, h, oldw, oldh);
mTabWidth = w / mTabCount;
}
public void setTitles(String[] titles)
{
mTitles = titles;
mTabCount = titles.length;
}
public void setIndicatorColor(int indicatorColor)
{
this.mIndicatorColor = indicatorColor;
}
@Override
protected void dispatchDraw(Canvas canvas)
{
super.dispatchDraw(canvas);
canvas.save();
canvas.translate(mTranslationX, getHeight() - 2);
canvas.drawLine(0, 0, mTabWidth, 0, mPaint);
canvas.restore();
}
public void setViewPager(ViewPager viewPager)
{
if (viewPager instanceof ViewPager)
{
this.viewPager = viewPager;
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener()
{
@Override
public void onPageSelected(int position)
{
View v = getChildAt(position);
if (v instanceof TextView)
{
TextView oldTv = (TextView) getChildAt(oldPosition);
oldTv.setTextColor(COLOR_TEXT_NORMAL);
TextView tv = (TextView) v;
tv.setTextColor(COLOR_INDICATOR);
oldPosition = position;
}
if (onPageChangeListener != null)
{
onPageChangeListener.onPageSelected(position);
}
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
{
scroll(position, positionOffset);
}
@Override
public void onPageScrollStateChanged(int state)
{
}
});
}
}
public void scroll(int position, float offset)
{
mTranslationX = getWidth() / mTabCount * (position + offset);
invalidate();
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev)
{
return super.dispatchTouchEvent(ev);
}
private void generateIndicators()
{
if (getChildCount() > 0)
this.removeAllViews();
int count = mTitles.length;
setWeightSum(count);
for (int i = 0; i < count; i++)
{
TextView tv = new TextView(getContext());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT);
lp.weight = 1;
tv.setTag(i);
tv.setGravity(Gravity.CENTER);
tv.setTextColor(COLOR_TEXT_NORMAL);
tv.setText(mTitles[i]);
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
tv.setLayoutParams(lp);
tv.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
int position = (Integer) v.getTag();
if (viewPager != null)
{
viewPager.setCurrentItem(position);
}
if(onItemClickListener!=null){
onItemClickListener.onItemClick(v, position);
}
}
});
addView(tv);
}
}
private OnPageChangeListener onPageChangeListener;
public void setOnPageChangeListener(OnPageChangeListener onPageChangeListener)
{
this.onPageChangeListener = onPageChangeListener;
}
public interface OnPageChangeListener
{
public void onPageSelected(int position);
}
private OnItemClickListener onItemClickListener;
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
public interface OnItemClickListener
{
public void onItemClick(View view,int position);
}
/**
* 。 setTitles(),setViewPager() !
*
* @param position
*/
public void setCurrentItem(int position)
{
oldPosition = position;
generateIndicators();
if (viewPager != null)
{
viewPager.setCurrentItem(position);
}
}
}
사용법:
package com.example.testviewpagerindicator;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import com.me.view.SimpleViewPagerIndicator;
public class MainActivity extends FragmentActivity
{
private Context context;
private String[] titles = new String[]
{ " 1 ", " 2 ", " 3 ", " 4 " };
private SimpleViewPagerIndicator indicator;
private ViewPager viewpager;
private FragmentPagerAdapter pagerAdapter;
private TabFragment[] fragments = new TabFragment[titles.length];
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
initViews();
}
private void initViews()
{
for (int i = 0; i < titles.length; i++)
{
fragments[i] = (TabFragment) TabFragment.newInstance(titles[i]);
}
pagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager())
{
@Override
public int getCount()
{
return titles.length;
}
@Override
public Fragment getItem(int position)
{
return fragments[position];
}
};
viewpager = (ViewPager) findViewById(R.id.viewpager);
viewpager.setAdapter(pagerAdapter);
indicator = (SimpleViewPagerIndicator) findViewById(R.id.indicator);
indicator.setTitles(titles);
indicator.setViewPager(viewpager);
indicator.setCurrentItem(1);
// indicator.setOnPageChangeListener(new SimpleViewPagerIndicator.OnPageChangeListener()
// {
//
// @Override
// public void onPageSelected(int position)
// {
// // TODO Auto-generated method stub
// Toast.makeText(context, position + " clicked", Toast.LENGTH_SHORT).show();
// }
// });
}
}
참고: SimpleViewPagerIndicator를 사용한 후에는 indicator를 사용해야 합니다.setOnPageChangeListener(...)페이지의 전환을 감청하고viewpager를 사용하지 마십시오.setOnPageChangeListener(...)설치했습니다.
마지막으로 Fragment:
package com.example.testviewpagerindicator;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class TabFragment extends Fragment
{
public static final String TITLE = "title";
private String mTitle = "Defaut Value";
private TextView mTextView;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (getArguments() != null)
{
mTitle = getArguments().getString(TITLE);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_tab, container, false);
mTextView = (TextView) view.findViewById(R.id.id_info);
mTextView.setText(mTitle);
return view;
}
public static TabFragment newInstance(String title)
{
TabFragment tabFragment = new TabFragment();
Bundle bundle = new Bundle();
bundle.putString(TITLE, title);
tabFragment.setArguments(bundle);
return tabFragment;
}
}
주 레이아웃:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.example.testviewpagerindicator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<com.me.view.SimpleViewPagerIndicator
android:id="@+id/indicator"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#ffffffff" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:flipInterval="30"
android:persistentDrawingCache="animation"
/>
</LinearLayout>
http://www.open-open.com/lib/view/open1426474795101.html
튕김 효과가 있는 ViewPager 클래스는 다음과 같습니다.
http://gundumw100.iteye.com/blog/1775588
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Kotlin의 기초 - 2부지난 글에서는 Kotlin이 무엇인지, Kotlin의 특징, Kotlin에서 변수 및 데이터 유형을 선언하는 방법과 같은 Kotlin의 기본 개념에 대해 배웠습니다. 유형 변환은 데이터 변수의 한 유형을 다른 데이터...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.