【发布时间】:2015-11-05 20:35:22
【问题描述】:
我用RecyclerView 数据实现了HorizontalScrollView,问题是在我的Adapter 代码中,我实现了一个逻辑,当点击一个项目时它会缩放。问题出在video 上,我不知道发生了什么 - 我用boolean 或int 的类测试了所有内容,说这个项目被点击,然后在onBindViewHolder 上询问这个项目如果它被点击,则再次缩放,如果没有,则缩放。
我知道这很令人困惑,但视频有助于解释。
我的list_row.xml 是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/RLimage"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
>
<ImageView
android:layout_centerInParent="true"
android:id="@+id/thumbnail"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/ic_launcher"
/>
</RelativeLayout>
<RelativeLayout
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/RLimage"
>
<TextView
android:layout_marginTop="14dp"
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="#222"
android:textSize="12sp"/>
</RelativeLayout>
</RelativeLayout>
我有这个RecyclerView 的片段是这样的:
<?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="wrap_content"
android:orientation="vertical">
<FrameLayout
android:layout_width="wrap_content"
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_marginBottom="-20dp"
android:paddingLeft="8dp"
android:paddingRight="8dp" />
</FrameLayout>
这是我的onCreateView(),来自我的Fragment
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
this.mContext = getActivity();
View rootView = inflater.inflate(R.layout.fragment_carta, container, false);
rv = (RecyclerView) rootView.findViewById(R.id.rcyList);
CustomLinearLayoutManager layoutManager = new CustomLinearLayoutManager(mContext);
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
rv.setLayoutManager(layoutManager);
// Adding code here
dataModelList = new ArrayList<dataModel>();
dataModelList.add(new dataModel(ContextCompat.getDrawable(mContext, R.mipmap.ic_launcher),"1234"));
dataModelList.add(new dataModel(ContextCompat.getDrawable(mContext, R.mipmap.ic_launcher),"1234"));
dataModelList.add(new dataModel(ContextCompat.getDrawable(mContext, R.mipmap.ic_launcher),"1234"));
dataModelList.add(new dataModel(ContextCompat.getDrawable(mContext, R.mipmap.ic_launcher),"1234"));
dataModelList.add(new dataModel(ContextCompat.getDrawable(mContext, R.mipmap.ic_launcher),"1234"));
dataModelList.add(new dataModel(ContextCompat.getDrawable(mContext, R.mipmap.ic_launcher),"1234"));
dataModelList.add(new dataModel(ContextCompat.getDrawable(mContext, R.mipmap.ic_launcher),"1234"));
dataModelList.add(new dataModel(ContextCompat.getDrawable(mContext, R.mipmap.ic_launcher),"1234"));
dataModelList.add(new dataModel(ContextCompat.getDrawable(mContext, R.mipmap.ic_launcher),"1234"));
adapter = new MyRecyclerViewAdapter(mContext, dataModelList);
rv.setAdapter(adapter);
return rootView;
}
adapter 是这样的:
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.CustomViewHolder> {
private Context mContext;
View animatedView = null;
private List<dataModel> dataModelList;
int animatedIndex = -1; // Initially no view is clicked so -1
//private PopulateListView populateListview;
public MyRecyclerViewAdapter(Context context, List<dataModel> items) {
this.dataModelList = items;
this.mContext = context;
//this.populateListview = populateListview;
}
@Override
public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, final int i) {
//View per each row
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.2f, 1f, 1.2f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
fade_in.setDuration(200); // 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(final CustomViewHolder customViewHolder, final int i) {
//Setting text view title and drawable
dataModel dataModel = dataModelList.get(i);
customViewHolder.imageView.setImageDrawable(dataModel.icon);
customViewHolder.textView.setText(dataModel.title);
if(animatedIndex == i){
ScaleAnimation fade_in = new ScaleAnimation(1f, 1.2f, 1f, 1.2f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
fade_in.setDuration(200); // animation duration in milliseconds
fade_in.setFillAfter(true); // If fillAfter is true, the transformation that this animation performed will persist when it is finished.
customViewHolder.itemView.startAnimation(fade_in);
}
customViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animatedIndex = i;
if (animatedView == null) {
animatedView = customViewHolder.itemView;
} else {
animatedView.setAnimation(null);
animatedView = customViewHolder.itemView;
}
//populateListview.PopulateListView(String.valueOf(i));
ScaleAnimation fade_in = new ScaleAnimation(1f, 1.2f, 1f, 1.2f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
fade_in.setDuration(200); // animation duration in milliseconds
fade_in.setFillAfter(true); // If fillAfter is true, the transformation that this animation performed will persist when it is finished.
customViewHolder.itemView.startAnimation(fade_in);
}
});
}
@Override
public int getItemCount() {
return dataModelList.size();
}
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);
}
}
注意:如果RecyclerView 上没有滚动,我也使用this class 来避免滚动
TL;DR
主要问题是,使用这段代码我可以缩放一个项目但是当我滚动(向左或向右)这个项目时,有时会失去缩放,我不知道为什么.视频中显示的错误是我猜的关键错误......
【问题讨论】:
-
到底是什么问题?
-
我不能让点击的项目“永远”保持放大状态,当我滚动时它会恢复正常
-
这是因为该项目在视图中不可见时会刷新。您需要在数据模型上设置一个字段 isClicked 以设置状态,一旦单击该项目,您就将该字段设置为 true。然后在您的 bindviewholder 检查 dataModel 是否已单击,然后如果 true 缩放该项目,则将其置于正常状态。
-
试过这种方式并为每个项目添加一个 Id,但没有结果可能我把代码弄错了:S
-
@ville101 知道如何解决它吗?
标签: android android-recyclerview horizontalscrollview