将RecyclerView单击的项目移到顶部 [英] Move RecyclerView clicked item to the top

查看:84
本文介绍了将RecyclerView单击的项目移到顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个n个项目的recyclerView.每一项都可以扩展.我希望我的商品能够扩展并在点击时移至顶部.假设我单击第三项,则它应移至第一项位置,然后将其展开并停止滚动. 我设法用动画扩展了RecyclerView,但是它没有移动到最高位置.我也尝试了scrollToPosition,但是没有用. 下面是我的Activity类:

I have a recyclerView with n items. Each item is expandable onCLick of it. I want my item to expand as well as move to the top onCLick. Suppose if I click third item then it should move to the first item position and then it will expand and scroll should stop. I have manage to expand the RecyclerView with animation, but it's not moving at the top position. I also tried scrollToPosition, but it didn't work. Below is my Activity class:

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


}

如何将我单击的项目移到最高位置.我在做什么错了?

How can I shift my clicked item to the top position. What I am doing wrong?

推荐答案

您可以这样操作:在适配器中创建一个方法,如下所示.

You can do like this way: Create one method in adaptor as below.

 public void swapeItem(int fromPosition,int toPosition){
     Collections.swap(arrayList, fromPosition, toPosition);
     notifyItemMoved(fromPosition, toPosition);
 }

现在单击项目,您可以像这样使用.

now on item click you can use like this way.

swapeItem(9,0); // here 9 is a clicked item position and 0 means at top of the list.

这篇关于将RecyclerView单击的项目移到顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆