简体   繁体   中英

Fix the position of recyclerview selected item

I am making app on android TV. I have a RecyclerView that display video thumbnail in horizontal direction that is selected by DPAD control. I already handled the selection of Recyclerview through DAPD control in apdapter. It is working fine. Now i want display selected item of RecylcerViw on first position when we scroll through DPAD control. How can I do it?

below is Adapter class

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;
        }

}

below is my 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);
@Override
public int getItemViewType(int position)
{
    return position;
}

@Override
public long getItemId(int position)
{
    return (long) position;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM