【问题标题】:How to animate views inside a specific recyclerView item(cardView)?如何为特定 recyclerView 项目(cardView)中的视图设置动画?
【发布时间】:2018-02-04 22:26:27
【问题描述】:

这里是新手。长按卡片(列表项)时,我很难在单个列表项上实现动画。当我长按一张卡片时,所有卡片都会动画。有人可以解释为什么这个问题只有在我禁用了 recyclerView 的水平滚动时才会出现。

主要活动:

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

回收者视图适配器:

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

    }

}
}

【问题讨论】:

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


    【解决方案1】:

    你不想在 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 里面添加这一行就可以了

    android:background="attr/selectableItemBackgroundBorderless"

    【讨论】:

    • 你能解释一下这个:android:background="attr/selectableItemBackgroundBorderless" 的作用吗?
    • 您好,可以阅读此developer.android.com/training/material/animations.html,它将帮助您达到显示效果
    • 问题仍然存在,动画正在所有列表项上发生,而我希望它只发生在我与之交互的列表项上。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-06
    • 1970-01-01
    相关资源
    最近更新 更多