IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

RecycleView + Glide + Amazon S3 = fil d'actualité Facebook


Sujet :

Android

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    Informaticien - Profesionnal master application development
    Inscrit en
    Octobre 2011
    Messages
    47
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Suisse

    Informations professionnelles :
    Activité : Informaticien - Profesionnal master application development
    Secteur : Industrie

    Informations forums :
    Inscription : Octobre 2011
    Messages : 47
    Par défaut RecycleView + Glide + Amazon S3 = fil d'actualité Facebook
    Bonjour a tous, je me suis lancé dans le développement d'une sorte de réseau social et l'affichage des publications ne semble pas très optimal.

    • Les données texte de mes utilisateurs sont stocké sur Amazon DynamoDB
    • Les images sont elles stockés sur Amazon S3.
    • Le tout est affiché au sein d'un RecycleView via un Adapter.
    • Les images sont chargé avec Glide (J'ai également testé Picasso mais ce dernier n'offre pas de meilleurs résultats)
    • De nouvelles données sont chargée lorsque l'utilisateur scroll jusqu'en fin de liste


    Lors du lancement de l'activité, lors du premier chargement de données, l'affichage est satisfaisant. Cependant lorsque de nouvelles données sont ajoutés dans le RecycleView, parfois des images ne sont plus retrouvé, des publications sont vides ou des images se retrouvent mélangées dans des publications ou elles ne devraient pas être.

    Mes interrogations portent :
    • sur mon adapter, ce dernier est-il correct au niveau du "header" - "item" - "footer".
    • Mon chargement de données (interrogation DynamoDB) est-il trop complexe ?
    • Dois-je implémenter quelque chose pour améliorer le cache des publications ou RecycleView+Glide suffisent-il ?
    • Vaux-il mieux charger nombre de données (ex 30) a chaque fois que l'utilisateur arrive en fin de liste ou plus régulièrement avec moins de données ?



    Voici mon code :

    MainFragment (Activité principal)

    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
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
     
        Map<String, AttributeValue> lastKeyEvaluatedMoi = null;
        Map<String, AttributeValue> lastKeyEvaluatedAmis = null;
        Map<String, AttributeValue> lastKeyEvaluatedAbo = null;
     
    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }
     
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
     
            View v = inflater.inflate(R.layout.fragment_fil, container, false);
     
            context = getApplicationContext();
     
            daoUser = new DaoUser(context);
     
     
            loadmore = false;
            // Lookup the swipe container view
            swipeContainer = (SwipeRefreshLayout) v.findViewById(R.id.swipeContainer);
            // Setup refresh listener which triggers new data loading
            swipeContainer.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    onRefresh = true;
                    LoadFilTask tsk = new LoadFilTask();
                    tsk.execute();
                }
            });
     
            // Configure the refreshing colors
            swipeContainer.setColorSchemeResources(android.R.color.holo_blue_bright,
                    android.R.color.holo_green_light,
                    android.R.color.holo_orange_light,
                    android.R.color.holo_red_light);
     
     
     
     
            FacebookSdk.sdkInitialize(getApplicationContext());
            profile = com.facebook.Profile.getCurrentProfile();
     
            // Initialize the Amazon Cognito credentials provider
            credentialsProvider = new CognitoCachingCredentialsProvider(
                    *********************
            );
     
            ddbClient = new AmazonDynamoDBClient(credentialsProvider);
     
     
            daoUser.open();
            idLanna = daoUser.getUserById(1).getIdLanna();
            daoUser.close();
     
            recyclerView = (RecyclerView) v.findViewById(R.id.recycler_view_fil);
     
     
            recyclerView.setHasFixedSize(false);
     
            RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
            recyclerView.setLayoutManager(layoutManager);
     
     
     
     
     
            dataPublications = new ArrayList<>();
     
            adapter = new RecycledAdapterFilPrincipal(getApplicationContext(), dataPublications, recyclerView);
     
            adapter.setLoadMoreListener(new RecycledAdapterFilPrincipal.OnLoadMoreListener() {
                @Override
                public void onLoadMore() {
     
                    recyclerView.post(new Runnable() {
                        @Override
                        public void run() {
                            loadMore();// a method which requests remote data
                        }
                    });
                    //Calling loadMore function in Runnable to fix the
                    // java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling error
                }
            });
     
            recyclerView.addItemDecoration(new DividerItemDecoration(getApplicationContext(), LinearLayoutManager.VERTICAL));
            recyclerView.setAdapter(adapter);
     
     
            LoadFilTask tsk = new LoadFilTask();
            tsk.execute();
     
            return v;
        }
     
    private void loadMore(){
     
            //dataPublications.add(null);
            //adapter.notifyItemInserted(dataPublications.size()-1);
     
            dataPublications.add(null);
            adapter.notifyItemInserted(dataPublications.size()-1);
     
            loadmore = true;
     
            if(lastKeyEvaluatedMoi == null && lastKeyEvaluatedAbo == null && lastKeyEvaluatedAmis == null){
                adapter.setMoreDataAvailable(false);
                dataPublications.remove(dataPublications.size()-1);
            }else{
                LoadFilTask tsk = new LoadFilTask();
                tsk.execute();
            }
        }
     
     public class LoadFilTask extends AsyncTask<Void, Integer, Void> {
     
            private ProgressDialog dialog;
     
            @Override
            protected void onPreExecute() {
                dialog = new ProgressDialog(getActivity());
                dialog.setMessage("Progress start");
     
                if(onRefresh){
                    lastKeyEvaluatedMoi = null;
                    lastKeyEvaluatedAbo = null;
                    lastKeyEvaluatedAmis = null;
                    loadmore = false;
                    dataPublications = new ArrayList<>();
                    latestReplies = new ArrayList<>();
                }
            }
     
            @Override
            protected Void doInBackground(Void... params) {
     
                String idfriend = "";
                String idabonnement = "";
     
                mapper = new DynamoDBMapper(ddbClient);
                UserAWS currentUser = mapper.load(UserAWS.class, idLanna);
     
                // MES PUBLICATIONS
                if((loadmore == false) || ((loadmore == true) && lastKeyEvaluatedMoi != null)) {
     
                    PublicationAWS publicationUser = new PublicationAWS();
                    publicationUser.setPublicationUserId(idLanna);
     
                    DynamoDBQueryExpression<PublicationAWS> queryExpressionMoi = new DynamoDBQueryExpression<PublicationAWS>()
                            .withHashKeyValues(publicationUser)
                            .withScanIndexForward(false)
                            .withLimit(2)
                            .withExclusiveStartKey(lastKeyEvaluatedMoi)
                            .withConsistentRead(false);
     
                    QueryResultPage<PublicationAWS> page = mapper.queryPage (PublicationAWS.class, queryExpressionMoi);
                    latestReplies = page.getResults();
                    lastKeyEvaluatedMoi = page.getLastEvaluatedKey();
                }
     
     
                // PUBLICATIONS AMIS
                if((loadmore == false) || ((loadmore == true) && lastKeyEvaluatedAmis != null)) {
                    if(currentUser != null) {
                        if (currentUser.getUserFriends() != null) {
                            latestReplies = new ArrayList<>(latestReplies);
                            for (int i = 0; i < currentUser.getUserFriends().size(); i++) {
                                idfriend = currentUser.getUserFriends().get(i);
                                PublicationAWS publicationsAmis = new PublicationAWS();
                                publicationsAmis.setPublicationUserId(idfriend);
     
                                DynamoDBQueryExpression<PublicationAWS> queryExpressionAmis = new DynamoDBQueryExpression<PublicationAWS>()
                                        .withHashKeyValues(publicationsAmis)
                                        .withScanIndexForward(false)
                                        .withLimit(10)
                                        .withExclusiveStartKey(lastKeyEvaluatedAmis)
                                        .withConsistentRead(false);
     
                                QueryResultPage<PublicationAWS> pageAmis = mapper.queryPage (PublicationAWS.class, queryExpressionAmis);
                                latestReplies.addAll(pageAmis.getResults());
                                lastKeyEvaluatedAmis = pageAmis.getLastEvaluatedKey();
                            }
                        }
                    }
                }
     
                // PUBLICATION ABONNEMENTS
     
                if((loadmore == false) || ((loadmore == true) && lastKeyEvaluatedAbo != null)) {
                    if(currentUser != null) {
                        if (currentUser.getAbonnementsPaysVille() != null) {
                            latestReplies = new ArrayList<>(latestReplies);
                            for (int i = 0; i < currentUser.getAbonnementsPaysVille().size(); i++) {
     
                                idabonnement = currentUser.getAbonnementsPaysVille().get(i);
                                String substr = idabonnement.substring(idabonnement.indexOf("/")+1, idabonnement.length());
     
                                PublicationAWS testTable = new PublicationAWS();
                                testTable.setPays(substr);
     
                                DynamoDBQueryExpression<PublicationAWS> queryExpressionAbo = new DynamoDBQueryExpression<PublicationAWS>()
                                        .withHashKeyValues(testTable)
                                        .withLimit(6)
                                        .withExclusiveStartKey(lastKeyEvaluatedAbo)
                                        .withConsistentRead(false);
     
                                QueryResultPage<PublicationAWS> pageAbo = mapper.queryPage (PublicationAWS.class, queryExpressionAbo);
                                latestReplies.addAll(pageAbo.getResults());
                                lastKeyEvaluatedAbo = pageAbo.getLastEvaluatedKey();
                            }
                        }
                    }
                }
     
     
                return null;
            }
     
            @Override
            protected void onPostExecute(Void args) {
     
                if(loadmore){
                    dataPublications.remove(dataPublications.size()-1);
                }
     
                for (int i = 0; i < latestReplies.size(); i++) {
                    String publicationUserId = latestReplies.get(i).getPublicationUserId();
                    String datePublication = latestReplies.get(i).getDatepublication();
                    String contenu = latestReplies.get(i).getContenu();
                    String imageKey = latestReplies.get(i).getImageKey();
                    String pays = latestReplies.get(i).getPays();
                    String ville = latestReplies.get(i).getVille();
                    String imageKeyProfil = latestReplies.get(i).getImageKeyProfil();
                    String auteur = latestReplies.get(i).getAuteur();
                    String publieDans = latestReplies.get(i).getPublieDans();
                    String action = latestReplies.get(i).getAction();
                    String mode = latestReplies.get(i).getMode();
                    List <String> Auteurlikes = latestReplies.get(i).getAuteurlike();
                    List<String> auteurcommentaire = latestReplies.get(i).getAuteurcommentaire();
     
                    dataPublications.add(new Publications(publicationUserId, datePublication,
                            contenu, imageKey, pays, ville, imageKeyProfil, auteur, publieDans, action, mode, Auteurlikes, auteurcommentaire));
     
                }
     
     
     
                if(onRefresh){
                    adapter.clear();
     
                    adapter = new RecycledAdapterFilPrincipal(getApplicationContext(), dataPublications, recyclerView);
     
                    adapter.setLoadMoreListener(new RecycledAdapterFilPrincipal.OnLoadMoreListener() {
                        @Override
                        public void onLoadMore() {
     
                            recyclerView.post(new Runnable() {
                                @Override
                                public void run() {
                                    loadMore();// a method which requests remote data
                                }
                            });
                            //Calling loadMore function in Runnable to fix the
                            // java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling error
                        }
                    });
     
                    recyclerView.addItemDecoration(new DividerItemDecoration(getApplicationContext(), LinearLayoutManager.VERTICAL));
                    recyclerView.setAdapter(adapter);
     
                    swipeContainer.setRefreshing(false);
                    onRefresh = false;
                }else if(loadmore){
                    dataPublications.remove(dataPublications.size()-1);
                }
     
                adapter.notifyDataChanged();
     
                if (dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        }
    Mon adapter

    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
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
     
    public class RecycledAdapterFilPrincipal  extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
        private ArrayList<Publications> galleryList;
        private Context context;
     
        private ArrayList<Publications> dataSet;
        //Context mContext;
        private TransferUtility transferUtility;
     
        AmazonDynamoDBClient ddbClient;
        PublicationAWS publication;
        CognitoCachingCredentialsProvider credentialsProvider;
        DynamoDBMapper mapper;
     
        DaoUser daoUser;
        User currentUser;
        String userIdLanna;
     
        //Publications dataModel;
        int position_click;
     
        private static final int TYPE_HEADER = 0;
        private static final int TYPE_ITEM = 1;
        private static final int TYPE_FOOTER = 2;
     
        OnLoadMoreListener loadMoreListener;
        boolean isLoading = false, isMoreDataAvailable = true;
     
        public RecycledAdapterFilPrincipal(Context context, ArrayList<Publications> galleryList, RecyclerView recyclerView) {
            this.galleryList = galleryList;
            this.context = context;
     
            credentialsProvider = new CognitoCachingCredentialsProvider(
                    context,
                    "******", // Identity Pool ID
                    Regions.EU_CENTRAL_1 // Region
            );
     
            ddbClient = new AmazonDynamoDBClient(credentialsProvider);
            mapper = new DynamoDBMapper(ddbClient);
     
            daoUser = new DaoUser(context);
            daoUser.open();
            currentUser = daoUser.getUserById(1);
            userIdLanna = currentUser.getIdLanna();
            daoUser.close();
     
     
     
        }
     
     
     
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            if (viewType == TYPE_ITEM) {
                //inflate your layout and pass it to view holder
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_list_fil_principal, parent, false);
                return new VHItem(view);
            } else if (viewType == TYPE_HEADER) {
                //inflate your layout and pass it to view holder
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.header_fil, parent, false);
                return new VHHeader(view);
            } else if (viewType == TYPE_FOOTER) {
                //return new LoadHolder(inflater.inflate(R.layout.footer_load_more,parent,false));
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.progressbar, parent, false);
                return new LoadHolder(view);
            }
     
            return  null;
     
        }
     
     
     
        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
            final int ou = i;
     
            if(i>=getItemCount()-1 && isMoreDataAvailable && !isLoading && loadMoreListener!=null){
     
                isLoading = true;
                loadMoreListener.onLoadMore();
     
            }
     
            if (viewHolder instanceof VHItem) {
     
                ((VHItem) viewHolder).txt_nom_personne.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //Intent userProfil = new Intent(mContext, Profi, lActivity.class);
                        //mContext.startActivity(userProfil);
                        //position_click = position;
                        Toast.makeText(context, getItem(ou -1).getAuteur(), Toast.LENGTH_LONG).show();
                    }
                });
     
                ((VHItem) viewHolder).txt_dans.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //Intent userProfil = new Intent(mContext, Profi, lActivity.class);
                        //mContext.startActivity(userProfil);
                        //position_click = position;
                        Toast.makeText(context, getItem(ou -1).getPublieDans(), Toast.LENGTH_LONG).show();
                    }
                });
     
                ((VHItem) viewHolder).btn_jaime.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //position_click = position;
                        Toast.makeText(context, getItem(ou -1).getDatepublication() + ou, Toast.LENGTH_LONG).show();
                        position_click = ou;
                        AddLike tsk = new AddLike();
                        tsk.execute();
                    }
                });
     
                ((VHItem) viewHolder).btn_commenter.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //position_click = position;
                        Intent CommentaireAct = new Intent(getApplicationContext(), CommentaireActivity.class);
                        CommentaireAct.putExtra("publicationUserId", getItem(ou -1).getPublicationUserId());//dataModel.getPublicationUserId());
                        CommentaireAct.putExtra("publicationDate", getItem(ou -1).getDatepublication());//dataModel.getDatepublication());
                        CommentaireAct.putExtra("currentuser", userIdLanna);
                        CommentaireAct.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(CommentaireAct);
     
                    }
                });
     
                ((VHItem) viewHolder).txt_commentaires.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //position_click = position;
                        Intent CommentaireAct = new Intent(getApplicationContext(), CommentaireActivity.class);
                        CommentaireAct.putExtra("publicationUserId", getItem(ou -1).getPublicationUserId());//dataModel.getPublicationUserId());
                        CommentaireAct.putExtra("publicationDate", getItem(ou -1).getDatepublication());//dataModel.getDatepublication());
                        CommentaireAct.putExtra("currentuser", userIdLanna);
                        CommentaireAct.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(CommentaireAct);
     
                    }
                });
     
                if (getItem(i -1).getImagekeyprofil() != null) {
     
                    Picasso.with(context).load("https://s3.amazonaws.com/******/" + getItem(ou -1).getImagekeyprofil()).noFade().into(((VHItem) viewHolder).img_post_profil);
                } else {
                    Picasso.with(context).load(R.drawable.home);
                }
     
                if (getItem(i -1).getContenu() != null) {
                    ((VHItem) viewHolder).txt_post_content.setText(getItem(i -1).getContenu());
                } else {
                    ((VHItem) viewHolder).txt_post_content.setVisibility(View.GONE);
                }
     
                if (getItem(i -1).getImageKey() != null) {
                    Glide.with(context).load("https://s3.amazonaws.com/l*****/" + getItem(i -1).getImageKey())
                            .thumbnail(0.5f)
                            .crossFade()
                            .diskCacheStrategy(DiskCacheStrategy.ALL)
                            .into(((VHItem) viewHolder).img_post);
     
                    //Picasso.with(context).load("https://s3.amazonaws.com/*****/" + getItem(i -1).getImageKey()).noFade().error(R.drawable.girl_back).into(((VHItem) viewHolder).img_post);
                } else {
                    ((VHItem) viewHolder).img_post.setVisibility(View.GONE);
                }
     
     
                if (getItem(i -1).getAuteurLikes() != null) {
                    ((VHItem) viewHolder).img_heart.setImageResource(R.drawable.heart);
                    ((VHItem) viewHolder).txt_jaime.setText(getItem(i -1).getAuteurLikes().size() + "");
                } else {
                    ((VHItem) viewHolder).img_heart.setVisibility(View.GONE);
                    ((VHItem) viewHolder).txt_jaime.setVisibility(View.GONE);
                }
     
                if (getItem(i -1).getAuteurcommentaire() != null) {
                    ((VHItem) viewHolder).txt_commentaires.setText(getItem(i -1).getAuteurcommentaire().size() + " commentaires");
                } else {
                    ((VHItem) viewHolder).txt_commentaires.setVisibility(View.GONE);
                }
     
                ((VHItem) viewHolder).txt_nom_personne.setText(getItem(i -1).getAuteur());
                ((VHItem) viewHolder).txt_action.setText(getItem(i -1).getAction() + " dans ");
                ((VHItem) viewHolder).txt_dans.setText(getItem(i -1).getPublieDans());
     
                Long diff = (new Date().getTime()) - Long.parseLong(getItem(i -1).getDatepublication());
     
                int seconds = (int) (diff / 1000) % 60;
                int minutes = (int) ((diff / (1000 * 60)) % 60);
                int hours = (int) ((diff / (1000 * 60 * 60)) % 24);
     
                String s = "";
     
                if (hours >= 24) {
                    s = "Il y à " + (hours / 24) + " jours";
                } else if (hours >= 1 && hours < 24) {
                    s = "Il y à " + hours + " heures";
                } else if (hours < 1 && minutes > 0) {
                    s = "Il y à " + minutes + " minutes";
                } else {
                    s = "Il y à " + seconds + " secondes";
                }
     
                ((VHItem) viewHolder).txt_post_date.setText(s);
     
            } else if (viewHolder instanceof VHHeader) {
     
     
     
            }else if (viewHolder instanceof LoadHolder) {
               // FooterViewHolder vh = (FooterViewHolder) viewHolder;
                //((LoadHolder) viewHolder).progressBar.setIndeterminate(true);
                ((LoadHolder) viewHolder).progressBar.setIndeterminate(true);
            }
        }
     
        public void setLoaded() {
            isLoading = false;
        }
     
        public class AddLike extends AsyncTask<Void, Integer, Void> {
     
            PublicationAWS selectedPub;
            int actualLikes = 0;
            List<String> actualLikers = new ArrayList<String>();
     
            @Override
            protected void onPreExecute() {
     
            }
     
            @Override
            protected Void doInBackground(Void... params) {
                selectedPub = mapper.load(PublicationAWS.class, galleryList.get(position_click).getPublicationUserId(), galleryList.get(position_click).getDatepublication());
     
                if (selectedPub.getAuteurlike() != null) {
                    actualLikes = selectedPub.getAuteurlike().size();
                    actualLikers = selectedPub.getAuteurlike();
                }
                actualLikers.add(userIdLanna);
                selectedPub.setAuteurlike(actualLikers);
     
                mapper.save(selectedPub);
     
                return null;
            }
     
            @Override
            protected void onPostExecute(Void args) {
            }
        }
     
     
        public static String getDate(long milliSeconds, String dateFormat) {
            // Create a DateFormatter object for displaying date in specified format.
            SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
     
            // Create a calendar object that will convert the date and time value in milliseconds to date.
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(milliSeconds);
            return formatter.format(calendar.getTime());
        }
     
        @Override
        public int getItemCount() {
     
            return galleryList.size();
        }
     
        public void clear() {
            galleryList.clear();
            notifyDataSetChanged();
        }
     
        @Override
        public int getItemViewType(int position) {
            if(galleryList.get(position) == null){
                return TYPE_FOOTER;
            }else  if(isPositionHeader (position)){
                return TYPE_HEADER;
            }else{
                return TYPE_ITEM;
            }
     
        }
     
        private boolean isPositionHeader(int position) {
            return position == 0;
        }
        private boolean isPositionFooter (int position) {
            return position == galleryList.size () + 1;
        }
     
        private Publications getItem(int position) {
            return galleryList.get(position);
        }
     
        class VHItem extends RecyclerView.ViewHolder {
            TextView txt_nom_personne;
            TextView txt_action;
            TextView txt_dans;
            TextView txt_post_date;
            ImageView img_post_profil;
            ImageView img_post;
            TextView txt_post_content;
            ImageView img_heart;
            TextView txt_jaime;
            TextView txt_commentaires;
            Button btn_jaime;
            Button btn_commenter;
     
            public VHItem(View itemView) {
                super(itemView);
     
                img_post_profil = (ImageView) itemView.findViewById(R.id.img_post_profil);
                txt_nom_personne = (TextView) itemView.findViewById(R.id.txt_nom_personne);
                txt_action = (TextView) itemView.findViewById(R.id.txt_action);
                txt_dans = (TextView) itemView.findViewById(R.id.txt_dans);
                txt_post_content = (TextView) itemView.findViewById(R.id.txt_post_content);
                txt_post_date = (TextView) itemView.findViewById(R.id.txt_date_post);
                img_post = (ImageView) itemView.findViewById(R.id.img_post);
                btn_jaime = (Button) itemView.findViewById(R.id.btn_jaime);
                img_heart = (ImageView) itemView.findViewById(R.id.img_heart);
                btn_commenter = (Button) itemView.findViewById(R.id.btn_commenter);
                txt_jaime = (TextView) itemView.findViewById(R.id.txt_jaimes);
                txt_commentaires = (TextView) itemView.findViewById(R.id.txt_commentaires);
            }
        }
     
        class VHHeader extends RecyclerView.ViewHolder {
            TextView txt_lanna;
     
            public VHHeader(View itemView) {
                super(itemView);
     
                txt_lanna = (TextView) itemView.findViewById(R.id.txt_lanna);
     
                Typeface typeface=Typeface.createFromAsset(context.getAssets(), "fonts/blenda.otf");
                txt_lanna.setTypeface(typeface);
     
            }
        }
    /*
        class FooterViewHolder extends RecyclerView.ViewHolder {
            Button btn_more;
     
            public FooterViewHolder(View itemView) {
                super(itemView);
     
                btn_more = (Button) itemView.findViewById(R.id.btn_load_more);
     
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
     
                    }
                });
            }
        }
    */
        static class LoadHolder extends RecyclerView.ViewHolder{
        public ProgressBar progressBar;
            public LoadHolder(View itemView) {
                super(itemView);
     
                progressBar = (ProgressBar) itemView.findViewById(R.id.progressBar1);
            }
        }
     
        public void setMoreDataAvailable(boolean moreDataAvailable) {
            isMoreDataAvailable = moreDataAvailable;
        }
     
        /* notifyDataSetChanged is final method so we can't override it
             call adapter.notifyDataChanged(); after update the list
             */
        public void notifyDataChanged(){
            notifyDataSetChanged();
            isLoading = false;
        }
     
     
        public interface OnLoadMoreListener{
            void onLoadMore();
        }
     
        public void setLoadMoreListener(OnLoadMoreListener loadMoreListener) {
            this.loadMoreListener = loadMoreListener;
        }
     
    }
    Le code est déjà bien avancé j'espère que vous vous y retrouverez.

    Peut-être n'ai je pas appliqué tout les best pattern usuel lors du développement de tel projet, merci de votre retour.

    Un screenshot après "LoadMore()" est en pièce jointe. Le recycleview semble perdre la référence des images parfois, comme pour les 3 publications vide. Si je scroll a nouveau certaine d'entre-elles s'affiche, un peu aléatoirement.
    Images attachées Images attachées  

Discussions similaires

  1. Réponses: 0
    Dernier message: 10/08/2014, 19h51
  2. [MCD] Modéliser Fil d'actualité
    Par Crackerz dans le forum Schéma
    Réponses: 1
    Dernier message: 26/05/2014, 13h30
  3. Réponses: 0
    Dernier message: 06/08/2013, 11h03
  4. Fil d'actualité animé
    Par ml500 dans le forum Flash/Flex
    Réponses: 2
    Dernier message: 29/05/2011, 12h29
  5. Réponses: 1
    Dernier message: 27/11/2010, 15h22

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo