如何在Recycler View中使用Circle Page Indictor?

时间:2016-11-18 10:52:45

标签: android android-recyclerview

我的需要是我想在我的recyclerVie中使用Circle Indicator。我想在github上添加一些库,但它们不支持Indicator。它们仅针对ViewPager显示。请指导我。我想,当我滚动我的recyclerView项目,然后根据位置,指标应该改变。

我的片段

@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);
    //fastScroller = (FastScroller)view. findViewById(R.id.fastscroll);
    linearLayoutManager = new LinearLayoutManager(getActivity());
    CircleIndicator indicator = (CircleIndicator) view.findViewById(R.id.indicator);
    mRecyclerView.setLayoutManager(linearLayoutManager);
    mRecyclerView.setAdapter(msgAdapter);
   /* fastScroller.setRecyclerView(mRecyclerView);
    fastScroller.setBubbleTextAppearance(R.style.StyledScrollerTextAppearance);*/
    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);
}

private Map<String, String> setMessagePostBody() {
    mapobject = new HashMap<String, String>();
    mapobject.put("user_id", UserShared.getUserId(getActivity()));
    mapobject.put("auth_token", UserShared.getUserAuthToken(getActivity()));
    mapobject.put("email", UserShared.getUserEmail(getActivity()));

    return mapobject;
}

我的适配器

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

    else 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 if(position!=firstVisible && position!=lastVisible){
        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();
}

0 个答案:

没有答案