将RecyClerview单击的项目移至顶部



我有一个带有n个项目的recyclerview。每个项目都是可扩展的。我希望我的物品可以扩展并移至顶级onclick。假设如果我单击第三个项目,则应移至第一个项目位置,然后它将展开并滚动应停止。我设法通过动画扩展了回收范围,但是它并没有在最高位置移动。我还尝试了卷轴置术,但它不起作用。以下是我的活动课:

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

}

如何将单击的项目转移到顶部位置。我做错了什么?

您可以这样做:在适配器中创建一种方法如下。

 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.

移至顶部?当然!🙋‍♀️

这与Kotlin创建了一个不错的动画,使眼睛看不见滚动:

在适配器中:

Collections.swap(list, fromPosition, 0)
notifyItemMoved(fromPosition, 0)
// TODO: Callback to the fragment through listener or ViewModel

在片段中: recyclerView.layoutManager?.scrollToPosition(0)

欢呼!