0
我目前正在嘗試實現類庫的視圖。由於它已被棄用,我一直在尋找替代品。在下面的鏈接中,我已經成功地嘗試了通用映像加載程序的圖像視圖尋呼機和戴夫史密斯的視圖尋呼機的實現。 http://commonsware.com/blog/2012/08/20/multiple-view-viewpager-options.html結果是一個畫廊式的小部件。將onclicklistener設置爲多viewpager(圖庫小部件的替代)
編輯 我現在的問題是,onclicklistener似乎只是在對焦頁面進行激活。我試圖將onclicklistener設置爲viewPager,但問題仍然存在。任何幫助和建議表示讚賞。
這裏是我的代碼片段:
(更新)我的活動:
public class PagerActivity extends Activity {
PagerContainer mContainer;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mContainer = (PagerContainer) 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 Object instantiateItem(ViewGroup container, int position) {
View page=getLayoutInflater().inflate(R.layout.page, container, false);
TextView tv = (TextView)page.findViewById(R.id.pagetext);
tv.setText("Item "+position);
tv.setGravity(Gravity.CENTER);
tv.setBackgroundColor(Color.argb(255, position * 50, position * 10, position * 50));
final int cPos = position;
tv.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(PagerActivity.this,"Page at: "+String.valueOf(cPos) , Toast.LENGTH_LONG)
.show();
}
});
container.addView(page);
return page;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View)object);
}
@Override
public int getCount() {
return 5;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return (view == object);
}
}
}
佈局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.pagercontainer.PagerContainer
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.pagercontainer.PagerContainer>
</RelativeLayout>
我無法重現您的問題。我已經更新了這個示例的版本,以在每個「TextView」上註冊點擊處理程序,並且它們都按預期啓動:https://github.com/commonsguy/cw-omnibus/tree/master/ViewPager/MultiView1 – CommonsWare
鏈接不會使用容器來環繞視圖頁面。我正在嘗試使用這個容器的Dave Smith的代碼。我認爲問題是隻有專注的頁面才能夠觸發。 – user2533611