23

画像付きのビューページャファイルでページインジケータを取得する必要があります。ここに私のコードです。このコードでページビューア付きのAndroidビューページャャ

public class IndicatorActivity extends Activity { 


/** Called when the activity is first created. */ 
    @Override 

     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 

      MyPagerAdapter adapter = new MyPagerAdapter(); 
      ViewPager myPager = (ViewPager) findViewById(R.id.pager); 
      myPager.setAdapter(adapter); 
      myPager.setCurrentItem(0); 
      TitlePageIndicator indicator = (TitlePageIndicator)findViewById(R.id.indicat); 
      indicator.setViewPager(myPager); 
    } 
} 

、私はTitlePageIndicator cannot be resolved to a typeとしてTitlePageIndicator indicator = (TitlePageIndicator)findViewById(R.id.indicat);でエラーが発生しました。このエラーは何ですか?どうすれば解決できますか?ここ

は私のXMLコードです:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <android.support.v4.view.ViewPager 
     android:id="@+id/pager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     /> 
    <com.viewpagerindicator.TitlePageIndicator 
    android:id="@+id/indicat" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" /> 
</LinearLayout> 
私は TitlePageIndicatorで記述する必要が何をコード

フラグメントを使用せずにこれを実行したいと思います。

class MyPagerAdapter extends PagerAdapter { 

    private static Integer[] titles = new Integer[] 
       { 
     R.drawable.jk,R.drawable.lm,R.drawable.no 
       }; 

    public int getCount() { 
      return 3; 
    } 

    public Object instantiateItem(View collection, int position) { 

      LayoutInflater inflater = (LayoutInflater) collection.getContext() 
          .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      View view; 
      ImageView iv; 

      view = inflater.inflate(R.layout.first, null); 

      ((ViewPager) collection).addView(view, 0); 
      switch (position) { 
      case 0: 
       iv = (ImageView)view.findViewById(R.id.imageView1); 
       iv.setImageResource(titles[position]); 
        break; 
      case 1: 
       iv = (ImageView)view.findViewById(R.id.imageView1); 
       iv.setImageResource(titles[position]); 
        break; 
      case 2: 
       iv = (ImageView)view.findViewById(R.id.imageView1); 
       iv.setImageResource(titles[position]); 
        break; 
      } 
      return view; 
    } 

    public void destroyItem(View arg0, int arg1, Object arg2) { 
      ((ViewPager) arg0).removeView((View) arg2); 

    } 



    public boolean isViewFromObject(View arg0, Object arg1) { 
      return arg0 == ((View) arg1); 

    } 

} 

が、私はこのクラス以上のものをやりたいんでした:

は私ものようなクラスを作成しましたか?ここでは、ヘルプ

+0

'com.viewpagerindicator.TitlePageIndicator'をインポートしましたか? Eclipseで作業している場合は、Ctrl + Shift + Oを押してみてください。 –

+0

私は日食だけで働いています。私はそれをしましたが、それはプログラムにインポートされていません.. –

+0

それらをインポートするための他の方法ですか? –

答えて

12

を事前に

おかげであなたがする必要があるいくつかの事です:あなたはすでにそれを行っていない場合

libraryを1-ダウンロードしてください。

2 - Eclipseにインポートします。あなたはライブラリを使用するプロジェクト

、3-セット: プロジェクト - >プロパティ - >アンドロイドは - >、ライブラリのセクションまでスクロール追加をクリックして...とviewpagerindicatorを選択します。

4-今すぐcom.viewpagerindicator.TitlePageIndicatorをインポートできるはずです。

は今の断片を使用せずにこれを実装について:

viewpagerindicatiorが付属していたサンプルでは、​​ライブラリはFragmentPagerAdapterを持ってViewPagerで使用されていることがわかります。

しかし、実際にはライブラリ自体はFragment独立です。ただViewPagerが必要です。 FragmentPagerAdapterの代わりにPagerAdapterを使用すればいいです。

+0

PageAdapterを使用してクラスを作成しました。 Plsは私のプログラムをチェックします。私もそれを更新しました...そして今私を助けてください... –

+1

あなたが言ったように私は図書館を設定しました。しかし、「プロジェクト」 - >「プロパティ」 - >「Android」 - >「ライブラリにスクロールする」セクションにはviewpagerindicatorがありません。「追加」をクリックします。 –

+0

私はTitlePageIndicatorまたは他のクラスの名前で任意のクラスを作成しますか? –

8

これはすでに回答済みですが、ViewPagerインジケータのシンプルで実用的でない実装を探している人にとって、私はオープンソースを実装しました。 Jake Whartonのバージョンを少し複雑にしている人は、https://github.com/jarrodrobins/SimpleViewPagerIndicatorをご覧ください。

+2

@JRodはあなたのライブラリを試しましたが、私のアプリはImageViewのラインでクラッシュし続けますitem = items.get(i);私はビューをスワイプしようとすると、setCurrentItem()メソッドで。 – irobotxxx

5

また、@ JRODのSimpleViewPagerIndicatorも使用しました。 @manuelJの記述どおりにクラッシュします。

彼のドキュメントによると:

SimpleViewPagerIndicator pageIndicator = (SimpleViewPagerIndicator) findViewById(R.id.page_indicator); 
pageIndicator.setViewPager(pager); 

はあなたにも、この行を追加していることを確認してください:SimpleViewPagerIndicatorが正しくインスタンス化取得されていないため、

pageIndicator.notifyDataSetChanged(); 

それは境界例外のうち配列でクラッシュし、項目は空です。 notifyDataSetChangedを呼び出すと、すべての値が正しく設定されるか、正しくリセットされます。

49

UPDATE: 22/03/2017

メインフラグメントレイアウト:

 mViewPaper = (ViewPager) view.findViewById(R.id.viewpager); 
     mViewPaper.setAdapter(adapder); 

     mPageGroup = (RadioGroup) view.findViewById(R.id.page_group); 
     mPageGroup.setOnCheckedChangeListener(this); 

     mViewPaper.addOnPageChangeListener(this); 

     ************************************************* 
     ************************************************* 

    @Override 
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 
    } 

    @Override 
    public void onPageSelected(int position) { 
     // when current page change -> update radio button state 
     int radioButtonId = mPageGroup.getChildAt(position).getId(); 
     mPageGroup.check(radioButtonId); 
    } 

    @Override 
    public void onPageScrollStateChanged(int state) { 
    } 

    @Override 
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) { 
     // when checked radio button -> update current page 
     RadioButton checkedRadioButton = (RadioButton)radioGroup.findViewById(checkedId); 
     // get index of checked radio button 
     int index = radioGroup.indexOfChild(checkedRadioButton); 

     // update current page 
     mViewPaper.setCurrentItem(index), true); 
    } 

カスタムチェックボックスの状態:このようなあなたのフラグメント上のビューとイベントを設定

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:app="http://schemas.android.com/apk/res-auto" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <android.support.v4.view.ViewPager 
       android:id="@+id/viewpager" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" /> 

      <RadioGroup 
       android:id="@+id/page_group" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_horizontal|bottom" 
       android:layout_marginBottom="@dimen/margin_help_container" 
       android:orientation="horizontal"> 

       <RadioButton 
        android:id="@+id/page1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:checked="true" /> 

       <RadioButton 
        android:id="@+id/page2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" /> 

       <RadioButton 
        android:id="@+id/page3" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" /> 
      </RadioGroup> 
     </FrameLayout> 

Custom checkbox image android

Viewpエージャーチュートリアル:http://architects.dzone.com/articles/android-tutorial-using

enter image description here

+2

私はあなたにそれを正確に行う方法を教えてくれるのが好きです – okysabeni

+1

良い解決策、外部ライブラリは必要ありません!私はラジオの代わりにドットを表示する簡単なカスタムビューを使用してこのアイデアを拡張しました。どうもありがとう! –

+0

素敵な男、完璧に動作 –

1

次の操作を実行する必要があります。

Eclipseにここhttps://github.com/JakeWharton/ViewPagerIndicator ViewPager Indicator 2 - インポートから完全なプロジェクトを1-ダウンロードしてください。あなたは、画面の以下のタイプを作りたい場合は

インポートした後、次に下記の手順 -

Screen Shot

変更サンプルサークルデフォルト

package com.viewpagerindicator.sample; 
    import android.os.Bundle; 
    import android.support.v4.view.ViewPager; 
    import com.viewpagerindicator.CirclePageIndicator; 

    public class SampleCirclesDefault extends BaseSampleActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.simple_circles); 

    mAdapter = new TestFragmentAdapter(getSupportFragmentManager()); 

    mPager = (ViewPager)findViewById(R.id.pager); 
    // mPager.setAdapter(mAdapter); 

    ImageAdapter adapter = new ImageAdapter(SampleCirclesDefault.this); 
    mPager.setAdapter(adapter); 


    mIndicator = (CirclePageIndicator)findViewById(R.id.indicator); 
    mIndicator.setViewPager(mPager); 
    } 
} 

ImageAdapter

package com.viewpagerindicator.sample; 

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.TextView; 

public class ImageAdapter extends PagerAdapter { 
private Context mContext; 

private Integer[] mImageIds = { R.drawable.about1, R.drawable.about2, 
     R.drawable.about3, R.drawable.about4, R.drawable.about5, 
     R.drawable.about6, R.drawable.about7 

}; 

public ImageAdapter(Context context) { 
    mContext = context; 
} 

public int getCount() { 
    return mImageIds.length; 
} 

public Object getItem(int position) { 
    return position; 
} 

public long getItemId(int position) { 
    return position; 
} 

@Override 
public Object instantiateItem(ViewGroup container, final int position) { 

    LayoutInflater inflater = (LayoutInflater) container.getContext() 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    View convertView = inflater.inflate(R.layout.gallery_view, null); 

    ImageView view_image = (ImageView) convertView 
      .findViewById(R.id.view_image); 
    TextView description = (TextView) convertView 
      .findViewById(R.id.description); 

    view_image.setImageResource(mImageIds[position]); 
    view_image.setScaleType(ImageView.ScaleType.FIT_XY); 

    description.setText("The natural habitat of the Niligiri tahr,Rajamala   Rajamala is 2695 Mts above sea level" 
     + "The natural habitat of the Niligiri tahr,Rajamala Rajamala is 2695 Mts above sea level" 
        + "The natural habitat of the Niligiri tahr,Rajamala Rajamala is 2695 Mts above sea level"); 

    ((ViewPager) container).addView(convertView, 0); 

    return convertView; 
} 

@Override 
public boolean isViewFromObject(View view, Object object) { 
    return view == ((View) object); 
} 

@Override 
public void destroyItem(ViewGroup container, int position, Object object) { 
    ((ViewPager) container).removeView((ViewGroup) object); 
} 
} 

gallery_view.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@drawable/about_bg" 
android:orientation="vertical" > 

<LinearLayout 
    android:id="@+id/about_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:weightSum="1" > 

    <LinearLayout 
     android:id="@+id/about_layout1" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight=".4" 
     android:orientation="vertical" > 

     <ImageView 
      android:id="@+id/view_image" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@drawable/about1"> 
    </ImageView> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/about_layout2" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight=".6" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="SIGNATURE LANDMARK OF MALAYSIA-SINGAPORE CAUSEWAY" 
      android:textColor="#000000" 
      android:gravity="center" 
      android:padding="18dp" 
      android:textStyle="bold" 
      android:textAppearance="?android:attr/textAppearance" /> 


     <ScrollView 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent" 
      android:fillViewport="false" 
      android:orientation="vertical" 
      android:scrollbars="none" 
      android:layout_marginBottom="10dp" 
      android:padding="10dp" > 

      <TextView 
       android:id="@+id/description" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:textColor="#000000" 
       android:text="TextView" /> 

      </ScrollView> 
    </LinearLayout> 
</LinearLayout> 

1

@ vuhung3990によって与えられた素敵な答えをすぐに改善。 私はソリューションを実装し、素晴らしい作品が1つのラジオボタンをタッチすると選択され、何も起こりません。

ラジオボタンをタップしてもページを変更することをお勧めします。これを行うには、ラジオグループにリスナーを追加するだけです:

mPager = (ViewPager) findViewById(R.id.pager); 
final RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radiogroup);  
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(RadioGroup group, int checkedId) { 
       switch (checkedId) { 
        case R.id.radioButton : 
         mPager.setCurrentItem(0, true); 
         break; 
        case R.id.radioButton2 : 
         mPager.setCurrentItem(1, true); 
         break; 
        case R.id.radioButton3 : 
         mPager.setCurrentItem(2, true); 
         break; 
       } 
      } 
     }); 
+0

ありがとう、私は私の答えを更新しますが、 – vuhung3990

関連する問題