【发布时间】:2019-02-19 09:50:37
【问题描述】:
我有一个包含 n 个项目的 recyclerView。每个项目都可以在单击时展开。我希望我的项目扩大并移动到顶部 onCLick。假设如果我单击第三个项目,那么它应该移动到第一个项目的位置,然后它会展开并且滚动应该停止。 我已经设法用动画扩展 RecyclerView,但它没有在顶部位置移动。我也试过scrollToPosition,但没有用。 下面是我的 Activity 类:
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
ArrayList<Integer> catImg=new ArrayList<>();
private CategoryAdapter mAdapter;
RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
catImg.add(R.drawable.eyebrowposition_wide);
catImg.add(R.drawable.eyebrowthickness_question);
catImg.add(R.drawable.eyebrowthickness_thick);
catImg.add(R.drawable.eyebrowthickness_thin);
mAdapter = new CategoryAdapter(this,catImg);
recyclerView.setHasFixedSize(true);
// vertical RecyclerView
// keep movie_list_row.xml width to `match_parent`
mLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false);
// horizontal RecyclerView
// keep movie_list_row.xml width to `wrap_content`
// RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(mLayoutManager);
// adding inbuilt divider line
recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
// adding custom divider line with padding 16dp
// recyclerView.addItemDecoration(new MyDividerItemDecoration(this, LinearLayoutManager.VERTICAL, 16));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
}
public void setRecyclerViewScroll(final int rowDistance) {
new Handler().postDelayed(new Runnable() {
public void run() {
// do something...
mLayoutManager.smoothScrollToPosition(recyclerView, null, 1);
}
}, 100);
}
}
适配器类:
public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.MyViewHolder>{
private ArrayList<Integer> catList;
Context mctx;
private int originalHeight = 0;
public CategoryAdapter(Context mctx, ArrayList<Integer> catList) {
this.catList = catList;
this.mctx = mctx;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.custom_row, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.img_cat.setBackgroundResource(catList.get(position));
}
@Override
public int getItemCount() {
return catList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView title, year, genre;
private boolean mIsViewExpanded = false;
public ImageView img_cat;
public LinearLayout lin2;
public MyViewHolder(View view) {
super(view);
img_cat = view.findViewById(R.id.img_cat);
lin2= view.findViewById(R.id.lin2);
if (mIsViewExpanded == false) {
// Set Views to View.GONE and .setEnabled(false)
lin2.setVisibility(View.GONE);
lin2.setEnabled(false);
}
img_cat.setOnClickListener(this);
}
@Override
public void onClick(final View view) {
// If the originalHeight is 0 then find the height of the View being used
// This would be the height of the cardview
if (originalHeight == 0) {
originalHeight = view.getHeight();
}
// Declare a ValueAnimator object
ValueAnimator valueAnimator;
if (!mIsViewExpanded) {
lin2.setVisibility(View.VISIBLE);
lin2.setEnabled(true);
mIsViewExpanded = true;
valueAnimator = ValueAnimator.ofInt(originalHeight, 600); // These values in this method can be changed to expand however much you like
int rowHeightFromStart = (getAdapterPosition()) *40;
((MainActivity)mctx).setRecyclerViewScroll(rowHeightFromStart);
} else {
mIsViewExpanded = false;
valueAnimator = ValueAnimator.ofInt(originalHeight + (int) (originalHeight * 2.0), originalHeight);
Animation a = new AlphaAnimation(1.00f, 0.00f); // Fade out
a.setDuration(200);
// Set a listener to the animation and configure onAnimationEnd
a.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
lin2.setVisibility(View.GONE);
lin2.setEnabled(false);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
// Set the animation on the custom view
lin2.startAnimation(a);
}
valueAnimator.setDuration(200);
valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
Integer value = (Integer) animation.getAnimatedValue();
lin2.getLayoutParams().height = value.intValue();
lin2.requestLayout();
}
});
valueAnimator.start();
}
}
}
如何将我点击的项目移动到顶部位置。我做错了什么?
【问题讨论】:
-
通过简单的arraylist add/delete将列表
catList中的选中项移动到第0位并调用notifydatasetchanged -
@gaurav tiwari 表示在您的回收站视图中单击特定项目时,它将被移动到顶部位置,对吧?
-
@HemantN.Karmur 是的
-
@gauravtiwari 好的,知道了。
-
@gauravtiwari 给你答案。
标签: android android-recyclerview