我有一个n个项目的recyclerView。每一项都可以扩展。我希望我的商品能够扩展并点击顶部。假设我单击第三项,则它应移至第一项位置,然后将其展开并停止滚动。 我设法用动画扩展了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();
}
}
}
如何将我单击的项目移到最高位置。我在做什么错了?
答案 0 :(得分:1)
您可以这样操作:在适配器中创建一个方法,如下所示。
public void swapeItem(int fromPosition,int toPosition){
Collections.swap(arrayList, fromPosition, toPosition);
notifyItemMoved(fromPosition, toPosition);
}
现在单击项目,您可以像这样使用。
swapeItem(9,0); // here 9 is a clicked item position and 0 means at top of the list.
答案 1 :(得分:1)
要移至顶部?当然! ?♀️
这会用Kotlin创建一个漂亮的动画,使滚动看不到眼睛:
在适配器中:
Collections.swap(list, fromPosition, 0)
notifyItemMoved(fromPosition, 0)
// TODO: Callback to the fragment through listener or ViewModel
在片段中:
recyclerView.layoutManager?.scrollToPosition(0)
干杯!