我已经在
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>
这是我的Fragment
中的onCreateView()
@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);
}
}
注意:我还使用this class来避免在RecyclerView
上不滚动的情况下进行缩放。
简而言之
主要问题是,使用此代码可以缩放项目但有时在滚动(向左或向右)时,该项目会失去缩放,我不知道为什么。视频中显示的错误是我猜想的关键错误...