如何在RecyclerView中使用一些ListView方法?

时间:2015-06-10 10:26:16

标签: java android android-listview

我使用 RecyclerView 来显示和排序级别。因为 ListView 慢慢加速我没有使用 listView 并使用 RecyclerView
因此,我需要一些ListView方法,此方法有 SetScrollPosition SetOnItemSelect

我该如何制作这种方法?

我的RecyclerView适配器代码:

public class levels_card_adapter extends
    RecyclerView.Adapter<levels_card_adapter.ViewHolder> {
private static String[] adapter_levels_number;

public levels_card_adapter(String[] adapter_levels_num_data) {
    adapter_levels_number = adapter_levels_num_data;



}

@Override
public levels_card_adapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                         int viewType) {

    View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
            R.layout.levels_recycler_layout, null);


    ViewHolder viewHolder = new ViewHolder(itemLayoutView);
    return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {

    viewHolder.adapter_levels_text.setText(adapter_levels_number[position].toString());

    viewHolder.adapter_levels_version = adapter_levels_number[position];



}

@Override
public int getItemCount() {
    return adapter_levels_number.length;
}

public static class ViewHolder extends RecyclerView.ViewHolder {

    public TextView adapter_levels_text;

    public String adapter_levels_version;


    public ViewHolder(View itemLayoutView) {
        super(itemLayoutView);

        adapter_levels_text = (TextView) itemLayoutView
                .findViewById(R.id.level_thumnail_text);

        itemLayoutView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent question_page_intent = new Intent(v.getContext(), com.tellfa.braingame.activities.question_Page.class);
                v.getContext().startActivity(question_page_intent);

            }
        });

    }

}

活动代码:

public class play_Page extends ActionBarActivity {

private RecyclerView play_recycler_levels;
private RecyclerView.Adapter play_adapter_levels;
private TextView play_header_text;
private tellfa_textView play_thumbnail_levels_num, play_time_level_num;
private DiscreteSeekBar play_scroll_seekbar;
private com.github.jorgecastilloprz.FABProgressCircle fb;
private Context context;
private ListView play_list_levels;

public static String[] levels_number = {"1", "2", "3", "4", "5", "6", "7", "8", "9",
        "10", "11", "12", "13", "14", "15", "16"};

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

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    ///////////// Font Style
    Typeface yekan_font = Typeface.createFromAsset(getAssets(), "fonts/yekan.ttf");

    play_header_text = (TextView) findViewById(R.id.header_play_text);
    play_header_text.setTypeface(yekan_font);
    /////////////

    play_thumbnail_levels_num = (tellfa_textView) findViewById(R.id.level_thumnail_text);

    play_time_level_num = (tellfa_textView) findViewById(R.id.time_levels_num);

    ///////////// SeekBar
    play_scroll_seekbar = (DiscreteSeekBar) findViewById(R.id.scroll_seekbar_play);



    ///////////// Recycler View
    play_recycler_levels = (RecyclerView) findViewById(R.id.levels_recycler);

    play_recycler_levels.setHasFixedSize(true);

    play_recycler_levels.setLayoutManager(new LinearLayoutManager(this));

    play_adapter_levels = new levels_card_adapter(levels_number);

    play_recycler_levels.setAdapter(play_adapter_levels);

    ////////////

    /*
    //////////// List
    com.tellfa.braingame.adapters.levels_list_adapter adapter=new com.tellfa.braingame.adapters.levels_list_adapter
            (this, levels_number);
    play_list_levels=(ListView)findViewById(R.id.list);
    play_list_levels.setAdapter(adapter);
    play_list_levels.setScrollingCacheEnabled(true);
        ///////////
    */

    FrameLayout containerLayout = (FrameLayout) findViewById(R.id.rel_id);
    HeaderAnimator animator = new HeaderAnimator(this);

    StikkyHeaderBuilder.stickTo(play_recycler_levels)
            .setHeader(R.id.header, containerLayout)
            .minHeightHeader(250)
            .animator(animator)
            .build();

    ///////////// Refresh
    final SwipeRefreshLayout play_refresh = (SwipeRefreshLayout) findViewById(R.id.refresh_play_layout);
    play_refresh.setColorSchemeResources(R.color.refresh1, R.color.refresh2, R.color.refresh3, R.color.refresh4, R.color.refresh5);
    play_refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            play_refresh.setRefreshing(true);

        }
    });

    ////


}

1 个答案:

答案 0 :(得分:0)

您可以在onBindViewHolder中添加onClickListener,以获得与ListView中的setOnItemSelect相同的结果。

@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {

    viewHolder.adapter_levels_text.setText(adapter_levels_number[position].toString());

    viewHolder.adapter_levels_version = adapter_levels_number[position];

    viewHolder.adapter_levels_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("Output", "From inside recycler view");
        }
    });
}

如果使用的是LinearLayoutManger或StaggeredGridLayoutManager方法,则使用scrollToPositionWithOffset()方法,该方法同时获取顶部值的位置和像素数。

例如,如果您希望RecyclerView中的第1项距离顶部

为50像素
linearLayoutManager.scrollToPositionWithOffset(1, 50);