2013-12-09 63 views
0

您好我正在使用本教程http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizontal-view-paging製作一個ViewPager應用程序,此處使用的ViewPager很酷的一點是每個頁面都有基於xml的不同佈局。問題是我嘗試了不同的方法來使我在佈局中創建的按鈕能夠工作,但我無法找到放置onClick監聽器的地方。下面是該ViewPager適配器:在視圖尋呼機中實現監聽器

class UniversePagerAdapter extends PagerAdapter { 

protected static final String[] CONTENT = new String[] { "One", "Two", "Three", "Four", }; 

public int getCount() { 
    return 4; 
} 

Dashboard activity; 
public UniversePagerAdapter(Dashboard activity){ 
    this.activity = activity; 
} 

public Object instantiateItem(final View collection, int position) { 
    LayoutInflater inflater = (LayoutInflater) collection.getContext() 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    int resId = 0; 
    switch (position) { 
    case 0: 
     resId = R.layout.one; 
     break; 
    case 1: 
     resId = R.layout.two; 
     break; 
    case 2: 
     resId = R.layout.three; 
     break; 
    case 3: 
     resId = R.layout.four; 
     break; 
    } 
    View view = inflater.inflate(resId, null); 
    ((ViewPager) collection).addView(view, 0); 
    return view; 
} 



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

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

@Override 
public Parcelable saveState() { 
    return null; 
} 

@Override 
public CharSequence getPageTitle(int position) { 
    return UniversePagerAdapter.CONTENT[position % CONTENT.length]; 
} 
} 

這裏的地方它綁定部分:

public class Dashboard extends BaseActivity implements OnClickListener { 

Button bNewButton; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.dashboard); 

    setAdapter(); 

} 

public void initPagerView(int position, View view) { 

    bNewButton = (Button) view.findViewById(R.id.bNewUniverse); 
    bNewButton.setOnClickListener(this); 

} 

private void setAdapter() { 
    // TODO Auto-generated method stub 
    mAdapter = new UniversePagerAdapter(Dashboard.this); 

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

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

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    Toast.makeText(getApplicationContext(), "It Lives!!!!", 
      Toast.LENGTH_LONG).show(); 
} 
} 

我正在初始化,並使用在XML佈局創建的對象,因爲他們都在的問題適配器。 請幫我一下嗎?

+0

測試答案,並會盡快給出反饋:-) –

回答

2

你可以有功能的活動稱爲initPagerView如下:

public void initPagerView(int position,View view){ 
    //Here you can use view.findViewById(); 
} 

而且在適配器:

private class MyPagerAdapter extends PagerAdapter { 
    public int getCount() { 
     return 5; 
    } 
    <YourActivityClass> activity; 
    public MyPagerAdapter(<YourActivityClass> activity){ 
     this.activity=activity; 
    } 
    public Object instantiateItem(View collection, int position) { 
     LayoutInflater inflater = (LayoutInflater) collection.getContext() 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     int resId = 0; 
     switch (position) { 
     case 0: 
      resId = R.layout.farleft; 
      break; 
     case 1: 
      resId = R.layout.left; 
      break; 
     case 2: 
      resId = R.layout.middle; 
      break; 
     case 3: 
      resId = R.layout.right; 
      break; 
     case 4: 
      resId = R.layout.farright; 
      break; 
     } 
     View view = inflater.inflate(resId, null); 
     ((ViewPager) collection).addView(view, 0); 
     activity.initPagerView(position,view); 
     return view; 
    } 
    @Override 
    public void destroyItem(View arg0, int arg1, Object arg2) { 
     ((ViewPager) arg0).removeView((View) arg2); 
    } 
    @Override 
    public boolean isViewFromObject(View arg0, Object arg1) { 
     return arg0 == ((View) arg1); 
    } 
    @Override 
    public Parcelable saveState() { 
     return null; 
    } 
} 

這是做這件事。 現在在initPagerView(int position,View view)這是你的活動中,你可以初始化你的視圖,這是從適配器添加到視圖尋呼機中。

您還可以創建一個Interface像:

public Interface PagerViewInitialiser{ 
    public initPagerView(int position,View view); 
} 

,讓您的活動實現這一點。

而在你的尋呼機適配器:

PagerViewInitialiser activity; 
public MyPagerAdapter(PagerViewInitialiser activity){ 
    this.activity=activity; 
} 
+0

嗨,對不起,花了很長時間。我已經添加了初始化視圖的方法,但沒有發生點擊事件。我已經更新了代碼以使用init ...需要幫助 –

+0

您需要調用活動。initPagerView(位置,視圖);這在instantiateItem只是在你返回一個視圖 –

+0

我調用了init的pagerView,但得到一個空指針異常,當我添加一個監聽器到一個按鈕 –

0

如果你在你的佈局按鈕,在instantiateItem膨脹的觀點後,您可以在視圖 通話findViewById你只是膨脹來獲得你的按鈕。喜歡的東西:

Button btn = (Button) view.findViewById(R.id.yourBtn); 
btn.setOnClickListener(new OnClickListener() { 
      }); 
+0

將無法​​正常工作....已經嘗試 –

0

一個辦法是:

做延伸的ViewGroup(RelativeLayout的,LinearLayout中......)對每個自定義頁面自定義視圖。 通上下文類擴展查看

public class TestView extends RelativeLayout{ 


public enum Action{ 
    ACTION_TEST_BTN, 
    ACTION_SOMETHING_ELSE 
} 

public interface MyTestViewActionListener{ 
     public void onAction(Action myAction); 
} 


private MyTestViewActionListener listener; 

public TestView(Context context) { 
    super(context); 
    LayoutInflater mInflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    mInflater.inflate(R.layout.left, this, true); 
    } 
public void setListener(MyTestViewActionListener listener){ 

    this.listener = listener;  

    Button btnTest = (Button)findViewById(this,R.id.btnTest); 
    btnTest.setOnClickListener(
       new OnClickListener() { 

        @Override 
        public void onClick(View v) { 
               listener.onAction(Action.ACTION_TEST_BTN); 

        } 
       }); 
} 
. 
. 
. 
} 

接下來,您可以在您的PageAdapter:

TestView testV = new TestView(activity); 
testV.setListener(listener); 

定義構造函數,像這樣一個

public MyPagerAdapter(<YourActivityClass> activity, MyTestViewActionListener listener){ 
     this.activity = activity; 
     this.listener = listener; 
    } 

在最後你可以在你的activity/fragment工具界面MyTestViewActionListener和 的實現方法:

onAction(Action action) 
{ 
    switch(action) 
    case(Action.ACTION_TEST_BTN): 
    //do something 
    break; 
    . 
    . 
} 
+0

我在哪裏放置TestView testV = new TestView(activity); \t testV.setListener(listener); ...它目前在適配器中..但當我點擊但沒有任何反應 –