【发布时间】: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