HorizontalScrollView or Carrousel?

后端 未结 2 808
暖寄归人
暖寄归人 2020-12-09 06:47

I want to create a HorizontalScrollView, but with some \"effects\" in it, like this example, or this other example. The thing is that I don\'t know how many ite

2条回答
  •  心在旅途
    2020-12-09 07:09

    In your case definitely you can use ViewPager, but if i were you i would go for RecyclerView with a LinearLayoutManager with its orientation set to Horizontal, so you will not need an HorizontalScrollView, and using RecyclerView you will also get the adapter thing you are looking for..

    Now in order to scale or show some other effect on click to differentiate it from others, you can Animate that particular view,

    I have written some demo code for that , posting here the required files , let me know if this is what you want,

    Activity

    /**
      * Created by Satyen on 10/27/15.
      **/
    public class SlidingDrawerActivity extends Activity {
    
       RecyclerView rcyList;
    
       @Override
       protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout_scroll_list);
    
            rcyList = (RecyclerView) findViewById(R.id.rcyList);
    
            LinearLayoutManager layoutManager = new LinearLayoutManager(this);
            layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            rcyList.setLayoutManager(layoutManager);
    
           /* rcyList.addItemDecoration(
                    new DividerItemDecoration(this, null));*/
            MyRecyclerViewAdapter myRecyclerAdapter = new MyRecyclerViewAdapter(this);
            rcyList.setAdapter(myRecyclerAdapter);
    
        }
    
    
    }
    

    Activity Layout

    
    
    
    
    
    
    
        
    
    
    
    

    Adapter

    public class MyRecyclerViewAdapter extends RecyclerView.Adapter {
    
    private Context mContext;
    View animatedView = null;
    
    public MyRecyclerViewAdapter(Context context) {
        this.mContext = context;
    }
    
    @Override
    public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, final int i) {
        final View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_row, null);
    
    
        CustomViewHolder viewHolder = new CustomViewHolder(view);
        /*final Animation a = AnimationUtils.loadAnimation(mContext, R.anim.scale_up);*/
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
                // You can tweak with the effects here
                if (animatedView == null) {
                    animatedView = view;
                } else {
                    animatedView.setAnimation(null);
                    animatedView = view;
                }
                ScaleAnimation fade_in = new ScaleAnimation(1f, 1.3f, 1f, 1.3f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                fade_in.setDuration(100);     // animation duration in milliseconds
                fade_in.setFillAfter(true);    // If fillAfter is true, the transformation that this animation performed will persist when it is finished.
                view.startAnimation(fade_in);
            }
        });
        return viewHolder;
    }
    
    @Override
    public void onBindViewHolder(CustomViewHolder customViewHolder, int i) {
        //Setting text view title
        customViewHolder.textView.setText("Data No. " + i);
    }
    
    @Override
    public int getItemCount() {
        return 10;
    }
    
    public class CustomViewHolder extends RecyclerView.ViewHolder {
        protected ImageView imageView;
        protected TextView textView;
    
        public CustomViewHolder(View view) {
            super(view);
            this.imageView = (ImageView) view.findViewById(R.id.thumbnail);
            this.textView = (TextView) view.findViewById(R.id.title);
        }
    }
    }
    

    Adapter Row Layout

    
    
    
    
    
    
        
    
        
    
    
    
    
    

    Other than you can also use TwoWayView that gives the functionality of implementing HorizontalListView,

    Above is just some demo code which may require some tweaks, let me know if this helps or ask further ...

    Also adding the screenshots of the output ..

提交回复
热议问题