将自定义视图添加到Recyclerview列表

时间:2019-03-25 23:13:44

标签: android view android-animation

我有一个自定义视图,该视图具有使用可运行模式的圆形动画(圆形在屏幕边缘之间水平反弹)。这在recyclerview之外起作用,但是我遇到的麻烦是在列表中添加了圆形动画。

圆形动画课

public class CircleAnimationView extends View {

    Handler handler = new Handler(Looper.getMainLooper());
    int mCircleRadius, mCircleSpeed, mCircleX, mCenterX, mCircleDirection, 
mColor;
    Paint mBodyPaint;
    boolean initPos = true;
    final long COUNT_INTERVAL = 16L;

    public CircleAnimationView(Context context) {
        this(context,null);
    }

    public CircleAnimationView(Context context, @Nullable AttributeSet 
attrs) {
        super(context,attrs);

        mCircleRadius = (int)dpToPx(10);

        TypedArray array = context.obtainStyledAttributes(attrs, 
R.styleable.CircleAnimationView);

        array.recycle();

        initPaints();

        mUpdateCircleRunnable.run();
    }

    private void initPaints() {
        mBodyPaint = new Paint();
        mBodyPaint.setColor(Color.BLACK);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mCenterX = getWidth()/2;
        if (initPos)
            canvas.drawCircle(mCenterX,100,mCircleRadius,mBodyPaint);
        initPos = false;
        canvas.drawCircle(mCircleX,100,mCircleRadius,mBodyPaint);
    }

    private float dpToPx(int dpValue) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 
dpValue, getResources().getDisplayMetrics());
    }

    public void addRadius(int radius){
        mCircleRadius = radius;
    }

    public void addSpeed(int speed){
        mCircleSpeed = speed;
    }

    public void addColor(int color){
        mColor = color;
    }

    Runnable mUpdateCircleRunnable = () -> setmUpdateCircleRunnable();

    public void setmUpdateCircleRunnable() {
        if (mCircleX < getWidth()-mCircleRadius && mCircleDirection == 1 || 
mCircleX <= mCircleRadius)
            mCircleDirection = 1;
        else if (mCircleX >= getWidth()-mCircleRadius)
            mCircleDirection = 0;
        if (mCircleDirection == 1)
            mCircleX = mCircleX + 5;
        else
            mCircleX = mCircleX - 5;
        invalidate();
        handler.postDelayed(mUpdateCircleRunnable, COUNT_INTERVAL);
    }
}

适配器类

public class CircleAnimationAdapter extends 
RecyclerView.Adapter<RecyclerView.ViewHolder> {

    Context mContext;
    private List<Circle> properties;
    View.OnClickListener mClickListener;
    CircleAnimationView circle;

    private int mSelectedPosition = Adapter.NO_SELECTION;

    public CircleAnimationAdapter(Context context, List<Circle> circleList, 
View.OnClickListener listener) {
        mContext = context;
        properties = circleList;
        mClickListener = listener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int 
 viewType) {
        View view = View.inflate(mContext, 
 R.layout.fragment_circleanimation, null);
        RecyclerViewHolder holder = new RecyclerViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int 
 position) {
        RecyclerViewHolder viewHolder = (RecyclerViewHolder) holder;
        Canvas canvas = new Canvas();
        ((RecyclerViewHolder) holder).circle.draw(canvas);

        circle = new CircleAnimationView(mContext);
        circle.addRadius(properties.get(position).getRadius());
        circle.addSpeed(properties.get(position).getSpeed());
        circle.addColor(properties.get(position).getColor());

        viewHolder.itemView.setOnClickListener(mClickListener);
        viewHolder.itemView.setTag(position);
    }

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

    public void setSelectedPosition(int position) {
        mSelectedPosition =  position ==  mSelectedPosition ? 
 Adapter.NO_SELECTION : position;
        notifyDataSetChanged();
    }

    public int getSelectedPosition() {
        return mSelectedPosition;
    }

    public boolean isPositionSelected() {
        return mSelectedPosition != Adapter.NO_SELECTION;
    }

    private class RecyclerViewHolder extends RecyclerView.ViewHolder {

        CircleAnimationView circle;

        RecyclerViewHolder(View view) {
            super(view);
            circle = new CircleAnimationView(mContext);
        }

        public void bindCircle(Circle objCircle) {
            //circle.draw();
            circle.invalidate();
        }
    }
}

片段类

public class CircleAnimationFragment extends Fragment {

    RecyclerView mRecyclerView;
    CircleAnimationAdapter mAdapter;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        configureListView();
        mRecyclerView.setLayoutManager(new 
LinearLayoutManager(getActivity()));
        mRecyclerView.setAdapter(mAdapter);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_circleanimation_list, 
container, false);
        mRecyclerView = view.findViewById(R.id.list);

        return view;
    }

    private void configureListView() {
        List<CircleAnimationView> circles = new ArrayList<>(1);
        circles.add(new CircleAnimationView(getActivity(),null));
    }
}

XML main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/example"
    android:id="@+id/exampleHead"
    android:gravity="center"
    android:layout_centerHorizontal="true"/>

<com.mdadi.circleanimation.CircleAnimationView
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:layout_below="@+id/exampleHead"
    android:id="@+id/example"/>

<TextView
    android:id="@+id/customHead"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/example"
    android:layout_centerHorizontal="true"
    android:gravity="center"
    android:text="@string/custom" />

<android.support.v7.widget.RecyclerView 
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/list"
    android:name="com.mdadi.circleanimation.CircleAnimationFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    app:layoutManager="LinearLayoutManager"
    tools:context=".CircleAnimationFragment"
    tools:listitem="@layout/fragment_circleanimation"
    android:layout_below="@+id/customHead"
    android:layout_centerHorizontal="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/radius"
    android:labelFor="@+id/radius_input"
    android:id="@+id/radius_label"
    android:layout_alignParentStart="true"
    android:layout_alignBottom="@+id/speed_label"/>

<EditText
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:id="@+id/radius_input"
    android:layout_toEndOf="@+id/radius_label"
    android:layout_alignBottom="@+id/speed_label"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/speed"
    android:labelFor="@+id/speed_input"
    android:id="@+id/speed_label"
    android:layout_above="@+id/btn_add"
    android:layout_toStartOf="@+id/speed_input"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/color"
    android:layout_alignParentBottom="true"
    android:id="@+id/color_label"
    android:layout_marginBottom="5dp"/>

<Button
    android:layout_width="25dp"
    android:layout_height="25dp"
    android:id="@+id/red"
    android:background="@color/red"
    android:layout_centerInParent="true"
    android:layout_toEndOf="@+id/color_label"
    android:layout_alignParentBottom="true"
    android:layout_marginStart="10dp"
    android:layout_marginBottom="5dp"/>

<Button
    android:layout_width="25dp"
    android:layout_height="25dp"
    android:id="@+id/orange"
    android:background="@color/orange"
    android:layout_centerInParent="true"
    android:layout_toEndOf="@+id/red"
    android:layout_alignParentBottom="true"
    android:layout_marginStart="10dp"
    android:layout_marginBottom="5dp"/>

<Button
    android:layout_width="25dp"
    android:layout_height="25dp"
    android:id="@+id/green"
    android:background="@color/green"
    android:layout_centerInParent="true"
    android:layout_toEndOf="@+id/orange"
    android:layout_alignParentBottom="true"
    android:layout_marginStart="10dp"
    android:layout_marginBottom="5dp"/>

<Button
    android:layout_width="25dp"
    android:layout_height="25dp"
    android:id="@+id/blue"
    android:background="@color/blue"
    android:layout_centerInParent="true"
    android:layout_toEndOf="@+id/green"
    android:layout_alignParentBottom="true"
    android:layout_marginStart="10dp"
    android:layout_marginBottom="5dp"/>

<Button
    android:layout_width="25dp"
    android:layout_height="25dp"
    android:id="@+id/yellow"
    android:background="@color/yellow"
    android:layout_centerInParent="true"
    android:layout_toEndOf="@+id/blue"
    android:layout_alignParentBottom="true"
    android:layout_marginStart="10dp"
    android:layout_marginBottom="5dp"/>

<EditText
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:id="@+id/speed_input"
    android:layout_alignParentEnd="true"
    android:layout_above="@+id/btn_add"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/btn_add"
    android:text="@string/add"
    android:layout_alignParentEnd="true"
    android:layout_alignParentBottom="true"/>

<!--adder for circle speed-->

</RelativeLayout>

任何帮助,在第一次执行此类程序时,都将不胜感激……

0 个答案:

没有答案