扩展CardView的最后一项

时间:2018-03-10 15:41:39

标签: android android-layout android-recyclerview android-cardview

请勿将其标记为this的副本。

我正在尝试使用可折叠CardView,但扩展后的最后一张卡片不会向上推送其内容,而是用户必须明确向下滚动。我尝试过所提到的方法,但都是徒劳的。这种情况正在发生。

enter image description here

这可以通过一些图书馆来实现,但我不喜欢它。我想我需要获得当前卡的位置然后应用逻辑。

这是我对适配器的实现:

public class RVAdapter extends RecyclerView.Adapter<MyViewHolder> {

private ArrayList<AppItem> appItemList;
private Context context;

RVAdapter(ArrayList<AppItem> appItemList, Context context) {
    this.appItemList = appItemList;
    this.context = context;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View layoutView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.card_list_item, parent, false);
    return new MyViewHolder(layoutView);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    final AppItem item = appItemList.get(position);
    holder.description.setText(item.getDescription());
    holder.title.setText(item.getTitle());
    holder.appImage.setBackgroundResource(item.getImageID());

    holder.button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    });
}

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

ViewHolder: -

class MyViewHolder extends RecyclerView.ViewHolder {

ImageView appImage;
TextView title;
TextView description;
Button button;

MyViewHolder(final View itemView) {
    super(itemView);
    this.appImage = itemView.findViewById(R.id.image);
    this.title = itemView.findViewById(R.id.title);
    this.description = itemView.findViewById(R.id.description);
    this.button = itemView.findViewById(R.id.button);

    itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            LinearLayout expandableView = view.findViewById(R.id.expandable_view);
            expandableView.setVisibility(expandableView.getVisibility() == View.GONE ?
            View.VISIBLE : View.GONE);
        }
    });
}

}

2 个答案:

答案 0 :(得分:1)

使用可以在创建Activity / fragment时使用此方法:

private void bindView() {
    mRecyclerView.setLayoutManager(mLayoutManager);
    mRecyclerView.setNestedScrollingEnabled(false);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    mRecyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), mRecyclerView, new RecyclerTouchListener.ClickListener() {
        @Override
        public void onClick(final View view, final int position)
        {
            if (mRecyclerView != null)
            {


                handler.postDelayed(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        scrollToView(scrollView,view);
                    }
                },500);
            }

        }

        @Override
        public void onLongClick(View view, int position)
        {

        }
    }));
}

相关方法叫做:

/** scrolling items as per child inside child**/
private void scrollToView(final ScrollView scrollViewParent, final View view) {
    // Get deepChild Offset
    Point childOffset = new Point();
    getDeepChildOffset(scrollViewParent, view.getParent(), view, childOffset);
    // Scroll to child.
    scrollViewParent.smoothScrollTo(0, childOffset.y);
}
private void getDeepChildOffset(final ViewGroup mainParent, final ViewParent parent, final View child, final Point accumulatedOffset) {
    ViewGroup parentGroup = (ViewGroup) parent;
    accumulatedOffset.x += child.getLeft();
    accumulatedOffset.y += child.getTop();
    if (parentGroup.equals(mainParent)) {
        return;
    }
    getDeepChildOffset(mainParent, parentGroup.getParent(), parentGroup, accumulatedOffset);
}

班级RecyclerTouchListener

public class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {

private GestureDetector gestureDetector;
private ClickListener clickListener;

public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener) {
    this.clickListener = clickListener;
    gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            return true;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
            if (child != null && clickListener != null) {
                clickListener.onLongClick(child, recyclerView.getChildPosition(child));
            }
        }
    });
}

@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {

    View child = rv.findChildViewUnder(e.getX(), e.getY());
    if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
        clickListener.onClick(child, rv.getChildPosition(child));
    }
    return false;
}

@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}

@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

}

public interface ClickListener {
    void onClick(View view, int position);

    void onLongClick(View view, int position);
}}

试试这个并在评论中告诉我

答案 1 :(得分:0)

查看RecyclerView.LayoutManager的{​​{3}},了解它是否符合您的要求。

  

requestChildRectangleOnScreen

     

请求将RecyclerView的给定子项放在屏幕上。

以下是一些示例代码,一旦RecyclerView空闲,它将在屏幕上移动RecyclerView的最后一个可见子项。这不是您正在寻找的,但您可以根据自己的需要进行调整。

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
        if (newState == RecyclerView.SCROLL_STATE_IDLE) {
            LinearLayoutManager llm = (LinearLayoutManager) recyclerView.getLayoutManager();
            int lastPos = llm.findLastVisibleItemPosition();
            View lastView = llm.findViewByPosition(lastPos);
            Rect rect = new Rect(0, 0, lastView.getWidth(), lastView.getHeight());
            llm.requestChildRectangleOnScreen(recyclerView, lastView, rect, false);
        }
    }
});