0

ViewPager で Fragments を設定して、すべての Fragment を左右にスワイプしたいと考えています。また、フラグメントをコンテナ アイテムに接続したいと考えています。私を助けてください、私は非常に混乱しています。詳細が必要な場合は、コメントするか、チャットを開始してください。

コードの詳細は次のとおりです。

周囲のページを表示できるようにする ViewPager コンテナーの実装 (ただし、インタラクティブではありません)。PagerContainer は ViewPager (この場合は中央) を保持し、clipChildren() を false に設定し、それ自体を再描画してタッチを転送する追加のコードをいくつか持っています。

これは Hadees という名前の PagerActivity です。

  //package com.example.islamiyat;

  import android.app.Activity;
  import android.graphics.Color;
  import android.os.Bundle;
  import android.support.v4.view.PagerAdapter;
  import android.support.v4.view.ViewPager;
  import android.view.Gravity;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.TextView;

 /**
 * PagerActivity: A Sample Activity for Hadees1
 * 
   * @param
 */
    public class Hadees extends Activity {
Hadees1 mContainer;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.hadees);

    mContainer = (Hadees1) findViewById(R.id.pager_container);

    ViewPager pager = mContainer.getViewPager();
    PagerAdapter adapter = new MyPagerAdapter();
    pager.setAdapter(adapter);
    // Necessary or the pager will only have one extra page to show
    // make this at least however many pages you can see
    pager.setOffscreenPageLimit(adapter.getCount());
    // A little space between pages
    pager.setPageMargin(15);

    // If hardware acceleration is enabled, you should also remove
    // clipping on the pager for its children.
    pager.setClipChildren(false);
     }

   // Nothing special about this adapter, just throwing up colored views for
   // demo
     private class MyPagerAdapter extends PagerAdapter {
    @Override
    public int getCount() {
        return 40;

    }


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

        TextView tv = new TextView(Hadees.this);


        tv.setText("Hadees " + position);
        tv.setGravity(Gravity.CENTER);
        tv.setBackgroundColor(Color.BLUE);



  ;
    ((ViewPager) container).addView(tv, 0);
    return tv;

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


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

}}}

これは、Hadees1 という名前の PagerContainer.java です。

       import android.content.Context;
 import android.graphics.Point;
 import android.support.v4.view.ViewPager;
 import android.util.AttributeSet;
 import android.view.MotionEvent;
 import android.view.View;
 import android.widget.FrameLayout;

  public class Hadees1 extends FrameLayout implements ViewPager.OnPageChangeListener {

private ViewPager mpager;
boolean mNeedsRedraw = false;

public Hadees1(Context context) {
    super(context);
    init();
}

public Hadees1(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public Hadees1(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

private void init() {
    // Disable clipping of children so non-selected pages are visible
    setClipChildren(false);

    // Child clipping doesn't work with hardware acceleration in Android
    // 3.x/4.x
    // You need to set this value here if using hardware acceleration in an
    // application targeted at these releases.
    setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}

@Override
protected void onFinishInflate() {
     try{
   mpager = (ViewPager) getChildAt(0);
   mpager.setOnPageChangeListener(this);
    } catch (Exception e) {
   throw new IllegalStateException("The root child of PagerContainer must be a               ViewPager");
 }
  }

public ViewPager getViewPager() {

    return mpager;
}
 private Point mCenter = new Point();
 private Point mInitialTouch = new Point();

  @Override
  protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  mCenter.x = w / 2;
  mCenter.y = h / 2;
  }



   @Override
  public boolean onTouchEvent(MotionEvent ev) {
    //We capture any touches not already handled by the ViewPager
     // to implement scrolling from a touch outside the pager bounds.
     switch (ev.getAction()) {
    case MotionEvent.ACTION_DOWN:
    mInitialTouch.x = (int)ev.getX();
    mInitialTouch.y = (int)ev.getY();
     default:
    ev.offsetLocation(mCenter.x - mInitialTouch.x, mCenter.y - mInitialTouch.y);
    break;
    }


   return mpager.dispatchTouchEvent(ev);
         }




@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    //Force the container to redraw on scrolling.
    //Without this the outer pages render initially and then stay static
    if (mNeedsRedraw) invalidate();
}

@Override
public void onPageSelected(int possition ){



}

@Override
public void onPageScrollStateChanged(int state) {
    mNeedsRedraw = (state != ViewPager.SCROLL_STATE_IDLE);

}



    }

これはXMLファイルです

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.example.islamiyat.Hadees1
    android:id="@+id/pager_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#CCC" >

    <android.support.v4.view.ViewPager
        android:layout_width="150dp"
        android:layout_height="100dp"
        android:layout_gravity="center_horizontal" />
     </com.example.islamiyat.Hadees1>

    </RelativeLayout>
4

0 に答える 0