我正在开发一个用于显示项目列表的Android应用程序,这是我的代码:
public class CategoryAdapter extends
RecyclerView.Adapter<CategoryAdapter.CategoryAdapterViewHolder> {
private String[] categoriesData;
private int numberOfCategories;
private CategoriesListListener cOnClickListener;
public interface CategoriesListListener {
void onCategoryItemClick(int categoryItemIndex, int height, int width,
View view);
}
public CategoryAdapter(int numberOfCategories, CategoriesListListener listener) {
this.numberOfCategories = numberOfCategories;
cOnClickListener = listener;
}
public class CategoryAdapterViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener{
public final TextView categoryTextView;
public int adapterCounter;
public CategoryAdapter categoryAdapter;
public void setCounter(int counter){
adapterCounter = counter;
}
public void setCategoryAdapter(CategoryAdapter categoryAdapter){
this.categoryAdapter = categoryAdapter;
}
public CategoryAdapterViewHolder(View view) {
super(view);
categoryTextView = (TextView) view.findViewById(R.id.category_name);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view){
int clickedPosition = getAdapterPosition();
int height = view.getHeight();
int width = view.getWidth();
cOnClickListener.onCategoryItemClick(clickedPosition, height, width, view);
}
}
@Override
public CategoryAdapterViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
Context context = viewGroup.getContext();
int layoutIdForListItem = R.layout.category_item;
LayoutInflater inflater = LayoutInflater.from(context);
boolean shouldAttachToParentImmediately = false;
View view = inflater.inflate(layoutIdForListItem, viewGroup, shouldAttachToParentImmediately);
CategoryAdapterViewHolder categoryAdapterViewHolder = new CategoryAdapterViewHolder(view);
categoryAdapterViewHolder.setCounter(getItemCount());
categoryAdapterViewHolder.setCategoryAdapter(this);
return categoryAdapterViewHolder;
}
@Override
public void onBindViewHolder(CategoryAdapterViewHolder forecastAdapterViewHolder, int position) {
String currentCategory = mCategoriesData[position];
forecastAdapterViewHolder.categoryTextView.setText(currentCategory);
}
@Override
public int getItemCount() {
if (null == mCategoriesData) return 0;
return mCategoriesData.length;
}
public void setCategoriesData(String[] categoriesData) {
this.mCategoriesData = categoriesData;
notifyDataSetChanged();
}
}
当单击一个Item时,将调用Listener的回调,这个Listener由Fragment
实现,它将自身的实例传递给上面的CategoryAdapter
public class CategoriesFragment extends Fragment implements
CategoryAdapter.CategoriesListListener {
//some code here
public interface OnCategoryClickListener{
public void onCategoryClick(int position, String name, String
description,int height, int width, View view);
}
//some code here
@Override
public void onCategoryItemClick(int categoryItemIndex,int height, int
width, View view) {
cCallback.onCategoryClick(categoryItemIndex,
categoriesName[categoryItemIndex],
categories[categoryItemIndex], height, width, view);
}
}
然后Activity
在interface
Fragment
public class MainActivity extends AppCompatActivity implements
CategoriesFragment.OnCategoryClickListener{
//some code here
@Override
public void onCategoryClick(int position, String name, String
description, int height, int width, View view){
//some code here
int finalRaduis = (int)Math.hypot(height/2, width/2);
Animator animator =
ViewAnimationUtils.createCircularReveal(view, width/2, height/2, 0,
finalRaduis);
view.setBackgroundColor(ContextCompat.getColor(getApplicationContext(),
R.color.colorAccent2));
animator.start();
//some code here
}
然后在ManActivity
点击view
后,我成功更改了视图的背景颜色,但在此情况下单击新视图时出现问题,之前点击的{{1}背景颜色没有变为默认颜色。
问题 我应该如何修改我的代码,以便在单击新视图时,之前单击的一种颜色变为默认颜色?
答案 0 :(得分:0)
您可以在适配器类中创建一个成员变量clickedPosition,其中deafult值为-1。在onBindViewHolder方法中,您可以检查clikedPostion == current postion然后将选择颜色设置为默认颜色。调用notififydataset从onclick方法更改方法以刷新视图。在适配器类
中添加以下代码> curl localhost:8000
{ worker: 5, message: 'msg' }
{ worker: 5, message: 'msg' }
{ worker: 8, message: 'msg' }
{ worker: 8, message: 'msg' }
{ worker: 7, message: 'msg' }
{ worker: 7, message: 'msg' }
{ worker: 6, message: 'msg' }
{ worker: 6, message: 'msg' }
答案 1 :(得分:0)
尝试在 MainActivity 中添加一些代码的以下解决方案: -
public class MainActivity extends AppCompatActivity implements
CategoriesFragment.OnCategoryClickListener{
//Define
int prevClickPos = -1;
View prevView = null;
//some code here
@Override
public void onCategoryClick(int position, String name, String
description, int height, int width, View view){
//some code here
int finalRaduis = (int)Math.hypot(height/2, width/2);
Animator animator =
ViewAnimationUtils.createCircularReveal(view, width/2, height/2, 0,
finalRaduis);
view.setBackgroundColor(ContextCompat.getColor(getApplicationContext(),
R.color.colorAccent2));
animator.start();
//Change Background Default
if(prevClickPos != -1 && prevClickPos != position) {
prevView.setBackgroundColor(ContextCompat.getColor(getApplicationContext(),
R.color.defaultColor)); //Apply Default color
}
prevClickPos = position;
prevView = view;
//some code here
}