修复recyclerview所选项目的位置

时间:2016-09-23 07:17:18

标签: android android-recyclerview

我在Android TV上制作应用程序。我有一个RecyclerView,可以显示DPAD控件选中的水平方向视频缩略图。我已经在apdapter中处理了RecyclerviewDAPD控件的选择。它工作正常。现在我希望在滚动DPAD控件时在第一个位置显示RecylcerViw的所选项目。我该怎么办?

下面的

是适配器类

public class TrendingAdapter extends RecyclerView.Adapter<TrendingAdapter.MyViewHolder>{
    Activity context;
    int focusedItem = 0;
    ArrayList<VideoContentModel> catVideoList;

    public TrendingAdapter(Activity context,ArrayList<VideoContentModel> catVideoList) {
        super();
        this.context = context;
        this.catVideoList = catVideoList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.trending_item_row,parent,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.itemView.setSelected(focusedItem == position);

        VideoContentModel model = catVideoList.get(position);
        ImageLoader imageLoader = MyApplication.getInstance().getImageLoader();
        holder.thumbnailView.setImageUrl(model.getVideoThumbnailUrl(), imageLoader);
        //holder.thumbnailView.setImageUrl("http://www.calkinsmobile.com/uploads/hd-image/1436441227_87302_cook_this___1.jpg", imageLoader);

    }

    @Override
    public int getItemCount() {
        return catVideoList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder{
        NetworkImageView thumbnailView;

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

            thumbnailView = (NetworkImageView)itemView.findViewById(R.id.trend_thumbnail);


            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // Redraw the old selection and the new
//                    notifyItemChanged(focusedItem);
//                    focusedItem = getLayoutPosition();
//                    notifyItemChanged(focusedItem);
                }
            });

        }

    }



        @Override
    public void onAttachedToRecyclerView(final RecyclerView recyclerView) {
        // TODO Auto-generated method stub
        super.onAttachedToRecyclerView(recyclerView);

        recyclerView.setOnKeyListener(new OnKeyListener() {

            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // TODO Auto-generated method stub
                RecyclerView.LayoutManager lm = recyclerView.getLayoutManager();

                // Return false if scrolled to the bounds and allow focus to move off the list
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
                        return tryMoveSelection(lm, 1);
                    } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
                        return tryMoveSelection(lm, -1);
                    }else if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER){
                        //Toast.makeText(context, "Item click on positon ="+focusedItem, Toast.LENGTH_LONG).show();

                        Intent intent = new Intent(context,VideoDetailsActivity.class);
                        intent.putParcelableArrayListExtra("videoList",catVideoList);
                        context.startActivity(intent);

                    }
                }


                return false;
            }
        });

    }

        private boolean tryMoveSelection(RecyclerView.LayoutManager lm, int direction) {
            int tryFocusItem = focusedItem + direction;

            // If still within valid bounds, move the selection, notify to redraw, and scroll
            if (tryFocusItem >= 0 && tryFocusItem < getItemCount()) {
                focusedItem = tryFocusItem;
                notifyItemChanged(focusedItem);
                lm.scrollToPosition(focusedItem);
                return true;
            }

            return false;
        }

}

下面是我的recyclerview

recyclerView = (RecyclerView)findViewById(R.id.trending_recylcer);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        TrendingAdapter adapter = new TrendingAdapter(this, videoList);
        recyclerView.setAdapter(adapter);

0 个答案:

没有答案