2016-01-07 56 views
1

我試圖實現與twitter結構佈局的RecyclerView,我可以在Activity的onCreate函數中獲取twitter時間軸,但它沒有被顯示。RecyclerView與twitter結構

public class TweetsActivity extends AppCompatActivity { 

    private RecyclerView listTweetView; 
    private TweetTimelineListAdapter listAdapter; 
    private TweetAdapter mTweetAdapter; 

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

     listTweetView = (RecyclerView) findViewById(R.id.list_tweet_view); 
     listTweetView.setLayoutManager(new LinearLayoutManager(getApplication())); 

     mTweetAdapter = new TweetAdapter(getApplication()); 
     listTweetView.setAdapter(mTweetAdapter); 


     final TwitterApiClient twitterApiClient = TwitterCore.getInstance().getApiClient(); 
     final StatusesService statusesService = twitterApiClient.getStatusesService(); 
     final TwitterSession session = Twitter.getSessionManager().getActiveSession(); 
     statusesService.userTimeline(session.getId(), null, null, null, null, null, null, null, null, 
       new Callback<List<Tweet>>() { 
        @Override 
        public void success(Result<List<Tweet>> listResult) { 
//      Log.i(TWITTER_USER_TIMELINE_REQUEST, "Successfully retrieved tweets"); 
//      TODO Handle empty list 
         Log.d("TweetActivity", "Total tweet count" + listResult.data.size()); 
         if (listResult.data != null){ 
          mTweetAdapter.setTweets(listResult.data); 
         }else{ 

         } 

        } 

        @Override 
        public void failure(TwitterException e) { 
        } 
       }); 
    } 

    public void setListAdapter(TweetTimelineListAdapter listAdapter) { 
     this.listAdapter = listAdapter; 
    } 
} 

我的適配器是這樣

public class TweetAdapter extends RecyclerView.Adapter<TweetAdapter.ViewHolderTweet> { 

    private LayoutInflater layoutInflater; 
    private List<Tweet> mListTweet ; 
    private ImageLoader mImageLoader; 


    public TweetAdapter(Context context){ 

     layoutInflater=LayoutInflater.from(context); 
    } 

    public TweetAdapter() {} 

    @Override 
    public ViewHolderTweet onCreateViewHolder(ViewGroup parent, int viewType) { 

     View view = layoutInflater.inflate(R.layout.content_tweets, parent, false); 
     ViewHolderTweet viewHolderTweet = new ViewHolderTweet(view); 
     return viewHolderTweet; 
    } 

    public void setTweets(List<Tweet> mListTweet) { 
     this.mListTweet = mListTweet; 
     //update the adapter to reflect the new set of movies 
     notifyDataSetChanged(); 
    } 


    @Override 
    public void onBindViewHolder(ViewHolderTweet holder, int position) { 
     Tweet currentTweet = mListTweet.get(position); 

     //Set user details 
     User user = currentTweet.user; 
     holder.mUserName.setText(user.name); 
     holder.mUserHandle.setText(user.screenName); 
     loadImages(user.profileImageUrl, holder); 

     //retrieved date may be null 
//  String createdAt = currentTweet.createdAt; 

     holder.mRetweetCount.setText(currentTweet.retweetCount); 
     holder.mFavCount.setText(currentTweet.favoriteCount); 
     holder.mTweet.setText(currentTweet.text); 

//  mPreviousPosition = position; 


    } 

    private void loadImages(String urlThumbnail, final ViewHolderTweet holder) { 
     if (!urlThumbnail.equals(Constants.NA)) { 
      mImageLoader.get(urlThumbnail, new ImageLoader.ImageListener() { 

       @Override 
       public void onErrorResponse(VolleyError error) { 

       } 

       @Override 
       public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) { 
        holder.mProfilePic.setImageBitmap(response.getBitmap()); 
       } 
      }); 
     } 
    } 

    @Override 
    public int getItemCount() { 

     return mListTweet.size(); 
    } 

    static class ViewHolderTweet extends RecyclerView.ViewHolder { 

     private ImageView mProfilePic; 
     private TextView mTweet; 
     private TextView mRetweetCount; 
     private TextView mFavCount; 
     private TextView mUserName; 
     private TextView mUserHandle; 


     public ViewHolderTweet(View itemView) { 
      super(itemView); 

      mProfilePic = (ImageView) itemView.findViewById(R.id.iv_user_image); 
      mTweet = (TextView) itemView.findViewById(R.id.tv_user_tweet); 
      mRetweetCount = (TextView) itemView.findViewById(R.id.tv_retweet_count); 
      mFavCount = (TextView) itemView.findViewById(R.id.tv_fav_count); 
      mUserName = (TextView) itemView.findViewById(R.id.tv_user_name); 
      mUserHandle = (TextView) itemView.findViewById(R.id.tv_user_handle); 


     } 
    } 

} 

佈局activity_tweet.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:fitsSystemWindows="true" 
    tools:context=".activity.TweetsActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/appbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="@dimen/appbar_padding_top" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.design.widget.TabLayout 
      android:id="@+id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

    </android.support.design.widget.AppBarLayout> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/list_tweet_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scrollbars="vertical" /> 

    <!--<include layout="@layout/content_tweets" />--> 

</android.support.design.widget.CoordinatorLayout> 

在任何方向將理解。

+0

你從哪裏調用'setListAdapter()'? –

回答

0

在設置適配器之後是否調用了setLayoutManager?

  // Set layout manager to position the items 
      listTweetView.setLayoutManager(new LinearLayoutManager(getApplication());