Bonjour à tous,
J'essai de mettre en place le viewpager sur une activité.
Le view pager charge une vue contenant 4 ImageView et 4 textView (toujours la même quelques soit la page) :
J'ai un adapter associé au viewpager qui est supposé charger cette vue :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 <TableLayout android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/cameraLayout"> <TableRow android:layout_height="wrap_content" android:id="@+id/ligneCam1" android:layout_width="wrap_content" android:background="@color/black"> <ImageView android:id="@+id/cam1" android:layout_height="120dp" android:layout_width="160dp" android:layout_weight="1" android:src="@drawable/loading_ring" android:scaleType="centerInside"></ImageView> <ImageView android:id="@+id/cam2" android:layout_height="120dp" android:layout_width="160dp" android:layout_weight="1" android:src="@drawable/loading_ring" android:scaleType="centerInside"></ImageView> </TableRow> <TableRow android:layout_height="wrap_content" android:id="@+id/ligneTitre1" android:layout_width="wrap_content"> <TextView android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:id="@+id/titre1" android:textColor="@color/blanc"></TextView> <TextView android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:id="@+id/titre2" android:textColor="@color/blanc"></TextView> </TableRow> <TableRow android:layout_height="wrap_content" android:id="@+id/ligneCam2" android:layout_width="wrap_content" android:background="@color/black"> <ImageView android:id="@+id/cam3" android:layout_height="120dp" android:layout_width="160dp" android:layout_weight="1" android:src="@drawable/loading_ring" android:scaleType="centerInside"></ImageView> <ImageView android:id="@+id/cam4" android:layout_height="120dp" android:layout_width="160dp" android:layout_weight="1" android:src="@drawable/loading_ring" android:scaleType="centerInside"></ImageView> </TableRow> <TableRow android:layout_height="wrap_content" android:id="@+id/ligneTitre2" android:layout_width="wrap_content"> <TextView android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:id="@+id/titre3" android:textColor="@color/blanc"></TextView> <TextView android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:id="@+id/titre4" android:textColor="@color/blanc"></TextView> </TableRow> </TableLayout>
Les ImageView et textView doivent être rempli via des infos reçu par socket. A chaque changement de page les imageView et textView doivent changer.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 public class LivePagerAdapter extends PagerAdapter { private DPCameraList cameras = null; public LivePagerAdapter(DPCameraList cams) { this.cameras = cams; } @Override public int getCount() { return (int) Math.ceil(((float)this.cameras.size() / 4)); } public void setCameras(DPCameraList cams) { this.cameras = cams; } @Override public Object instantiateItem(View container, int position) { LayoutInflater inflater = (LayoutInflater) container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.pager,null); ((ViewPager) container).addView(view); return view; } @Override public void destroyItem(View pager, int position, Object view) { ((ViewPager) pager).removeView((View) view); } @Override public boolean isViewFromObject(View view, Object object) { return view == ((View) object); } }
Quand je lance mon activité pour la première fois :
pas de problème , mes 4 imageView et textview sont bien remplis. En revanche si je change de page , je me retrouve avec mes imageView et textView dans leur état initial (la vue est recharger mais pas mise à jour) alors que la page précédente à été mise à jour.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 adapter = new LivePagerAdapter(this.listeCamera); pager = (ViewPager) findViewById(R.id.camspager); pager.setAdapter(adapter); pager.setCurrentItem(0); pager.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int currentPage) { miseajour(); } });
La mise à jour se fait simplement en utilisant un findview et l'id de l'élément à mettre à jour.
Pour résumer, quand dans mon code je fait un findViewById(R.id.montext).setText("Test"); j'ai l'impression qu'il ne fait pas référence à la page courante , mais à la page précédente.
Comment puis je m'assurer que l'élément choisi est bien la page courante ?
Partager