为RecycleView设置自定义适配器

时间:2016-06-15 17:47:47

标签: android android-recyclerview

我有这个自定义适配器

package client.tclient.com.client;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;

import com.twitter.sdk.android.core.models.Tweet;
import com.twitter.sdk.android.tweetui.Timeline;
import com.twitter.sdk.android.tweetui.TweetTimelineListAdapter;


public class CustomTweetTimelineListAdapter extends TweetTimelineListAdapter {

    private Context context;
    /**
     * Constructs a TweetTimelineListAdapter for the given Tweet Timeline.
     *
     * @param context  the context for row views.
     * @param timeline a Timeline<Tweet> providing access to Tweet data items.
     * @throws IllegalArgumentException if timeline is null
     */
    public CustomTweetTimelineListAdapter(Context context, Timeline<Tweet> timeline) {
        super(context, timeline);
        this.context = context;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);

        //disable subviews
        if(view instanceof ViewGroup){
            disableViewAndSubViews((ViewGroup) view);
        }

        //enable root view and attach custom listener
        view.setEnabled(true);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Long tweetId = getItemId(position);
                //Toast.makeText(context, tweetId, Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(context,TweetActivity.class);
                intent.putExtra("TWEET_ID", tweetId);
                context.startActivity(intent);
            }
        });
        return view;
    }
    private void disableViewAndSubViews(ViewGroup layout) {
        layout.setEnabled(false);
        for (int i = 0; i < layout.getChildCount(); i++) {
            View child = layout.getChildAt(i);
            if (child instanceof ViewGroup) {
                disableViewAndSubViews((ViewGroup) child);
            } else {
                child.setEnabled(false);
                child.setClickable(false);
                child.setLongClickable(false);
            }
        }
    }
}

这是我使用它的类。

public class FollowingActivity extends AppCompatActivity {

RecyclerView homeList;
SwipeRefreshLayout mySwipeRefreshLayout;
FloatingActionButton fab;

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


    homeList = (RecyclerView) findViewById(R.id.homeList);
    loadHomeTimeLine();
    mySwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swiperefresh);
    mySwipeRefreshLayout.setOnRefreshListener(
            new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    loadHomeTimeLine();
                    Toast.makeText(FollowingActivity.this,"Refreshing",Toast.LENGTH_LONG);
                    mySwipeRefreshLayout.setRefreshing(false);

                }
            }
    );

    fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Se presionó el FAB", Snackbar.LENGTH_SHORT)
                    .setAction("Action", null).show();
        }
    });
}

public void loadHomeTimeLine(){
    TwitterApiClient twitterApiClient = TwitterCore.getInstance().getApiClient();
    final StatusesService statusesService = twitterApiClient.getStatusesService();
    statusesService.homeTimeline(200, null, null, null, null, null, null,
            new Callback<List<Tweet>>() {
                @Override
                public void success(Result<List<Tweet>> result) {

                    final FixedTweetTimeline timeline = new FixedTweetTimeline.Builder()
                            .setTweets(result.data)
                            .build();
                    final CustomTweetTimelineListAdapter adapter = new CustomTweetTimelineListAdapter(FollowingActivity.this,timeline);

                    homeList.setAdapter( adapter);//here is the problem
                }

                public void failure(TwitterException exception) {
                }
            }
    );
}
}

我无法传递CustomTweetTimelineListAdapter homelist.setadapter,因为我需要RecycleView.Adapter,如果我使用的是ListView而不是RecycleView,那么我需要RecycleView设置浮动操作按钮动画

1 个答案:

答案 0 :(得分:0)

似乎还不支持RecyclerView

更多信息: Vagrant documentation