android - Horizo​​ntalScrollView 还是 Carrousel?

标签 android android-viewpager android-animation android-imageview

我想创建一个Horizo​​ntalScrollView,但是里面有一些“效果”,像这样example ,或者这个 other example .问题是我不知道我必须使用多少元素;我从 API 获取 ImageView,所以它应该是动态的。如果选中它,它会随着下面的 TextView 变大,我怎样才能做到这一点?我在 SO 上找到的最接近的例子是 here .这正是我所需要的,但我已经测试了给出的答案并且它对我不起作用......但是问题中的图像正是我想要的。谁能指导我这样做?

假设我将使用 5 个 ImageView ONLY FOR TESTING 对其进行测试,那么如何动态添加这些 ImageView 的示例将是对我也有好处。

另一个例子是 Snapchat APP但不同的是,我想对所选内容添加一些效果,例如使其变大或其他。

编辑

我想举一个例子来说明如何像带有布局(适配器)的自定义 Horizo​​ntalScrollView 那样做,最重要的是,如果可能的话,可以为单击的效果添加效果,我想要适配器,因为我需要当然要点击该项目。正如@UncaughtException 对我说的那样,我认为我应该使用 RecycleView,因为我不知道我会得到多少图像,我必须把它放在我的应用程序上,所以我认为这就是解决方案。

这将是 Snapchat HoritzontalScrollView 之间的混合和 This image from a SO question

最佳答案

在您的情况下,您绝对可以使用 ViewPager,但如果我是您,我会选择带有方向设置的 LinearLayoutManagerRecyclerViewHorizo​​ntal,所以你不需要 Horizo​​ntalScrollView,使用 RecyclerView 你也会得到你需要的 adapter正在寻找..

现在,为了缩放或在点击上显示一些其他效果以区别于其他 View ,您可以动画该特定 View ,

我已经为此编写了一些演示代码,在这里发布了所需的文件,如果这是您想要的,请告诉我,

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_scroll_list.xml -->

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:layout_weight="1">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rcyList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="@android:color/holo_blue_dark"
        android:paddingLeft="8dp"
        android:paddingRight="8dp" />

</FrameLayout>
</LinearLayout>

适配器

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.CustomViewHolder> {

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

适配器行布局

<!-- list_row.xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="vertical">

<RelativeLayout
    android:layout_width="80dp"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/thumbnail"
        android:layout_width="100dp"
        android:layout_height="80dp"
        android:layout_alignParentLeft="true"
        android:layout_centerInParent="true"
        android:scaleType="centerCrop"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/thumbnail"
        android:layout_centerHorizontal="true"
        android:text="dafdafda"
        android:textColor="#222"
        android:textSize="12sp" />

</RelativeLayout>

</LinearLayout>

除此之外,您还可以使用 TwoWayView 来实现 Horizo​​ntalListView 的功能,

以上只是一些演示代码,可能需要一些调整,让我知道这是否有帮助或进一步询问...

同时添加输出的屏幕截图..

Layout when none of the item is clicked

Layout when second item is clicked

关于android - Horizo​​ntalScrollView 还是 Carrousel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33219942/

相关文章:

android - 对等方未通过身份验证-Android Studio

android - ImageView onClick() 和 fling() 手势冲突

java - 让 InfiniteViewPager 发挥作用

android - 滑动图表引擎 fragment 有点滞后

android - 未调用自定义 ScaleAnimation/applytransformation

android - 在使用 Android 模拟器启动应用程序时 react native 错误

Android NFC 启动屏幕

android - 使用新内容 : 刷新 ViewPager 的奇怪问题

android - 为什么缩放动画会改变按钮大小?

Android 弯曲 ListView