在您的情况下,您肯定可以使用
ViewPager
,但如果我是您,我会选择使用
RecyclerView
,并使用其方向设置为
Horizontal
的
LinearLayoutManager
,这样您将不需要
HorizontalScrollView
,并且使用
RecyclerView
,您还将获得所需的
adapter
。
现在,为了对某些视图进行缩放或显示其他效果以使其与其他视图区分开来,您可以对该特定视图进行
Animate
。
我已经为此编写了一些演示代码,并在此发布所需文件,请告诉我这是否符合您的要求。
Activity
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);
MyRecyclerViewAdapter myRecyclerAdapter = new MyRecyclerViewAdapter(this);
rcyList.setAdapter(myRecyclerAdapter);
}
}
活动布局
<?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>
Adapter
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);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
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);
fade_in.setFillAfter(true);
view.startAnimation(fade_in);
}
});
return viewHolder;
}
@Override
public void onBindViewHolder(CustomViewHolder customViewHolder, int i) {
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);
}
}
}
适配器行布局
<?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
,它提供了实现HorizontalListView
的功能。
上面只是一些演示代码,可能需要进行一些微调,请让我知道这是否有所帮助或者需要进一步解释...
同时添加输出的截图...
![Layout when none of the item is clicked](https://istack.dev59.com/aDO0p.webp)
![Layout when second item is clicked](https://istack.dev59.com/zLqjG.webp)