带指示器的回收器项目

时间:2016-11-17 09:59:34

标签: android android-recyclerview

我的需求是我在Recycler中充气JSON数据。好的。现在我想要一个指示符LIKE THIS STYLE,它将显示您在RecyclerView中有多少项目。当一件物品已经被戴上或当前时,指示器将被点燃,当使用向下滚动时,其顶部选定的位置项目将是明智的。请帮助我如何做到这一点..

我的适配器方法

     @Override
    public MessageAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_message, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MessageAdapter.ViewHolder holder, int position) {

        /*Tuesday, April 10, 2001 3:51 PM*/

        msgModels = msgList.get(position);
        if (msgList.get(position) != null) {

                holder.msgTitle.setText(msgModels.getMessageTitle());
                holder.msgDescription.setText(msgModels.getMessageDescription());
                holder.msgDate.setText(msgModels.getMessageDate());
//

        }

        if (firstVisible==position) {
            holder.msgTitle.setTextColor(Color.RED);
            holder.msgDescription.setTextColor(Color.RED);
            holder.msgDate.setTextColor(Color.RED);
            holder.layerLyt.setAlpha(1);
        }

        if (position==msgList.size()-1) {
            holder.msgTitle.setTextColor(Color.RED);
            holder.msgDescription.setTextColor(Color.RED);
            holder.msgDate.setTextColor(Color.RED);
            holder.layerLyt.setAlpha(1);
        }
        else{
            holder.msgTitle.setTextColor(Color.BLUE);
            holder.msgDescription.setTextColor(Color.BLUE);
            holder.msgDate.setTextColor(Color.BLUE);
            holder.layerLyt.setAlpha(0.2f);
        }
    }

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

    private int firstVisible = 0;
    private int lastVisible ;

    public void changeItem(int position){
        firstVisible = position;
        notifyItemChanged(firstVisible);
        notifyDataSetChanged();


    }  public void changeLastItem(int position){
        lastVisible = position;
        notifyItemChanged(lastVisible);
        notifyDataSetChanged();


    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView msgTitle, msgDescription,msgDate;
        public RelativeLayout layerLyt;
        public ViewHolder(View view) {
            super(view);
            msgTitle  = (TextView)view.findViewById(R.id.msgTitle);
            msgDescription  = (TextView)view.findViewById(R.id.msgDescription);
            layerLyt = (RelativeLayout) view.findViewById(R.id.layerLyt);
            msgDate  = (TextView)view.findViewById(R.id.msgDate);
        }
    }

我的片段

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_message, null);
    mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_viewLaws);

    linearLayoutManager = new LinearLayoutManager(getActivity());
    mRecyclerView.setLayoutManager(linearLayoutManager);
    mRecyclerView.setAdapter(msgAdapter);
    msgAdapter = new MessageAdapter(msgList, getActivity(), this);


    //msgAdapter.notifyDataSetChanged();
    mProgressDialog = new ProgressDialog(getActivity());

    mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);

            LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();

            int firstVisible = manager.findFirstVisibleItemPosition();
            int lastVisible = manager.findLastVisibleItemPosition();
            Log.i("TAG", "onScrolled: " + firstVisible);
            Log.i("TAG", "Last Index: " + lastVisible);
            msgAdapter.changeItem(firstVisible);
            msgAdapter.changeLastItem(lastVisible);
        }
    });


    if (NetworkCheck.getInstant(getActivity()).isConnectingToInternet()) {
        loadAPI();
    } else {
        ErrorMessageDialog.getInstant(getActivity()).show("No Network");
    }
    return view;
}

private void loadAPI() {
    mProgressDialog.setMessage("Loading...");
    mProgressDialog.setCancelable(false);
    mProgressDialog.show();
    new PostStringRequest(getActivity(), setMessagePostBody(), this, API_MSG_CALL, NetworkUtility.MESSAGE_API);
}

1 个答案:

答案 0 :(得分:0)

您可以使用第三方库轻松实现

https://github.com/andraskindler/quickscroll