如何在特定的recyclerView项(cardView)中为视图设置动画?

时间:2018-02-04 22:26:28

标签: android android-recyclerview android-animation android-cardview

新手在这里。当长按卡(列表项)时,我在单个列表项上实现动画时遇到困难。当我长按一张卡片时,所有卡片都会生动。并且有人可以解释为什么只有在我禁用了recyclerView的horizo​​ntalScroll时才会出现此问题。

主要活动:

public class MainActivity extends AppCompatActivity {

RecyclerView recyclerView;
TextView score;
ArrayList words = new ArrayList();
wordCardsList wc;
static int scored = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    vibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE);
    final int initialDatasetSize = words.size();

    score = findViewById(R.id.score);
    score.setText("" + 0 + "/" + initialDatasetSize);
    recyclerView = findViewById(R.id.recyclerView);

    wc = new wordCardsList(this, words);
    final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false){
        @Override
        public boolean canScrollHorizontally() {
            return false;
        }
    };

    final GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 1, GridLayoutManager.HORIZONTAL, false);

    recyclerView.setLayoutManager(linearLayoutManager);
    recyclerView.setAdapter(wc);


    new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.UP) {
        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
            return false;
        }

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
            mediaPlayer.start();
            vibrator.vibrate(6);
            if(words.size()!=1) {
                int pos = viewHolder.getAdapterPosition();
                words.remove(pos);

                System.out.println(words.size());
                wc.notifyDataSetChanged();
            }

            score.setText("" + ++scored + "/" + initialDatasetSize);

        }
    }).attachToRecyclerView(recyclerView);
}
}

Recycler View Adapter:

public class wordCardsList extends RecyclerView.Adapter<wordCardsList.PersonViewHolder>{

private ArrayList words;
private Context context;
Converter converter = new Converter();
int longPressCount = 0;


public wordCardsList(Context context, ArrayList words) {

    this.words = words;
    this.context = context;

}

@Override
public PersonViewHolder onCreateViewHolder(ViewGroup parent, final int viewType) {
    View view = LayoutInflater.from(context).inflate(R.layout.card, parent, false);
    final PersonViewHolder personViewHolder = new PersonViewHolder(view);
    longPressCount = 1;

    return personViewHolder;
}


@Override
public void onBindViewHolder(wordCardsList.PersonViewHolder holder, int position) {
    final String word = words.get(position).toString();
    holder.words.setText(word);
    holder.cardView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            //Working: System.out.println("Long Pressed on" + personViewHolder.getAdapterPosition());
            if(longPressCount == 1) {
                float pixels = converter.convertDpToPixel(190f, context);

                view.findViewById(R.id.cardLayout).animate().translationY(-pixels).setDuration(100).start();
                view.findViewById(R.id.exampleHeading).animate().alpha(1f).setDuration(400).start();
                view.findViewById(R.id.example1).animate().alpha(1f).setDuration(400).start();
                view.findViewById(R.id.example2).animate().alpha(1f).setDuration(400).start();
                view.findViewById(R.id.definitionHeading).animate().alpha(1f).setDuration(400).start();
                view.findViewById(R.id.definition1).animate().alpha(1f).setDuration(400).start();
                vibrator.vibrate(6);
                scored--;
                longPressCount=0;
            }

            return true;
        }
    });

}

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


class PersonViewHolder extends RecyclerView.ViewHolder{

    public TextView words;
    public CardView cardView;

    public PersonViewHolder(View view) {

        super(view);
        words = view.findViewById(R.id.wordName);
        cardView = view.findViewById(R.id.card);

    }

}
}

1 个答案:

答案 0 :(得分:0)

你不想在java程序中做什么只是在你的XML卡上做一些改变

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_margin="@dimen/size_5"
        android:elevation="3dp"
        card_view:cardCornerRadius="@dimen/size_5">

        <LinearLayout
            android:id="@+id/list_header"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/size_20"
            android:layout_marginRight="@dimen/size_20"
            android:background="?attr/selectableItemBackgroundBorderless"
            android:orientation="vertical"
            android:paddingBottom="@dimen/font_10"
            android:paddingTop="@dimen/font_10">

        </LinearLayout>

    </android.support.v7.widget.CardView>

</LinearLayout>

主要更改位于您的LinearLayout内,添加此行

机器人:背景=&#34; ATTR / selectableItemBackgroundBorderless&#34;