我点击它的任何项目只是改变recyclerview textview中最后一项的颜色而且图像也在改变最后添加的项目 但我希望该项目的图像和文字颜色应该改变我点击
这是我的适配器类 公共类MyAdapter扩展了RecyclerView.Adapter {
private static final int TYPE_HEADER = 0; // Declaring Variable to Understand which View is being worked on
// IF the view under inflation and population is header or Item
private static final int TYPE_ITEM = 1;
private String mNavTitles[]; // String Array to store the passed titles Value from MainActivity.java
private int mIcons[]; // Int Array to store the passed icons resource value from MainActivity.java
// static TextView text;
// static ImageView image;
private String name; //String Resource for header View Name
private int profile; //int Resource for header view profile picture
private String email; //String Resource for header view email
static Context context;
//static int ICONS[] = {R.drawable.find,R.drawable.profile,R.drawable.applied,R.drawable.applied,R.drawable.resume,R.drawable.news,R.drawable.support,R.drawable.signout,R.drawable.search};
static int ICONS[] = {R.drawable.menu_inactivefind,R.drawable.menu_inactiveprofile,R.drawable.menu_inactiveapplied,R.drawable.menu_inactiveapplied,R.drawable.menu_inactiveresume,R.drawable.menu_inactivenews,R.drawable.menu_inactivesupport,R.drawable.menu_inactivesignout,R.drawable.menu_inactivesearch};
static int ICONS2[] = {R.drawable.findactive,R.drawable.profileactive,R.drawable.appliedactive,R.drawable.appliedactive,R.drawable.resumeactive,R.drawable.newsactive,R.drawable.support1,R.drawable.signout,R.drawable.search};
// Creating a ViewHolder which extends the RecyclerView View Holder
// ViewHolder are used to to store the inflated views in order to recycle them
public static class ViewHolder1 extends RecyclerView.ViewHolder implements View.OnClickListener{
int Holderid;
static TextView textView;
static ImageView imageView;
ImageView profile;
TextView Name;
TextView email;
public ViewHolder1(View itemView,int ViewType,Context c) { // Creating ViewHolder Constructor with View and viewType As a parameter
super(itemView);
context=c;
// Here we set the appropriate view in accordance with the the view type as passed when the holder object is created
if(ViewType == TYPE_ITEM) {
textView = (TextView) itemView.findViewById(R.id.rowText); // Creating TextView object with the id of textView from item_row.xml
imageView = (ImageView) itemView.findViewById(R.id.rowIcon);// Creating ImageView object with the id of ImageView from item_row.xml
Holderid = 1;
// setting holder id as 1 as the object being populated are of type item row
// TextView im= ((ViewHolder) itemView.getTag()).textView;
Log.i("view",String.valueOf(textView));
}
else{
Name = (TextView) itemView.findViewById(R.id.name); // Creating Text View object from header.xml for name
email = (TextView) itemView.findViewById(R.id.email); // Creating Text View object from header.xml for email
profile = (ImageView) itemView.findViewById(R.id.circleView);// Creating Image view object from header.xml for profile pic
Holderid = 0; // Setting holder id = 0 as the object being populated are of type header view
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context,"The Item Clicked is: "+getPosition(),Toast.LENGTH_SHORT).show();
// imageViewz = ((ViewHolder1) v.getTag()).imageView;
// textViewz = ((ViewHolder1) v.getTag()).textView;
// imageViewz.setImageResource(ICONS2[getPosition()]);
// textViewz.setTextColor(Color.parseColor("#000000"));
Log.i("inside",String.valueOf(textView));
}
}
MyAdapter(String Titles[],int Icons[],String Name,String Email, int Profile) { // MyAdapter Constructor with titles and icons parameter
// titles, icons, name, email, profile pic are passed from the main activity as we
mNavTitles = Titles; //have seen earlier
mIcons = Icons;
name = Name;
email = Email;
profile = Profile; //here we assign those passed v alues to the values we declared here
//in adapter
// Log.i("myad cons",text.toString());
}
//Below first we ovverride the method onCreateViewHolder which is called when the ViewHolder is
//Created, In this method we inflate the item_row.xml layout if the viewType is Type_ITEM or else we inflate header.xml
// if the viewType is TYPE_HEADER
// and pass it to the view holder
@Override
public MyAdapter.ViewHolder1 onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_ITEM) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row,parent,false); //Inflating the layout
ViewHolder1 vhItem = new ViewHolder1(v,viewType, context); //Creating ViewHolder and passing the object of type view
return vhItem; // Returning the created object
//inflate your layout and pass it to view holder
} else if (viewType == TYPE_HEADER) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.header,parent,false); //Inflating the layout
ViewHolder1 vhHeader = new ViewHolder1(v,viewType,context); //Creating ViewHolder and passing the object of type view
return vhHeader; //returning the object created
}
return null;
}
//Next we override a method which is called when the item in a row is needed to be displayed, here the int position
// Tells us item at which position is being constructed to be displayed and the holder id of the holder object tell us
// which view type is being created 1 for item row
@Override
public void onBindViewHolder(MyAdapter.ViewHolder1 holder, int position) {
if(holder.Holderid ==1) { // as the list view is going to be called after the header view so we decrement the
// position by 1 and pass it to the holder while setting the text and image
holder.textView.setText(mNavTitles[position - 1]); // Setting the Text with the array of our Titles
holder.imageView.setImageResource(mIcons[position -1]);// Settimg the image with array of our icons
//text=holder.textView;
//image=holder.imageView;
// Log.i("bind",text.toString());
}
else{
// holder.profile.setImageResource(profile); // Similarly we set the resources for header view
holder.Name.setText(name);
holder.email.setText(email);
}
}
// This method returns the number of items present in the list
@Override
public int getItemCount() {
return mNavTitles.length+1; // the number of items in the list will be +1 the titles including the header view.
}
// Witht the following method we check what type of view is being passed
@Override
public int getItemViewType(int position) {
if (isPositionHeader(position))
return TYPE_HEADER;
return TYPE_ITEM;
}
private boolean isPositionHeader(int position) {
return position == 0;
}
}
这是主要活动中的听众
mRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
@Override
public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {
View child = recyclerView.findChildViewUnder(motionEvent.getX(),motionEvent.getY());
if(child!=null && mGestureDetector.onTouchEvent(motionEvent)){
//Drawer.closeDrawers();
Toast.makeText(home.this,"The Item Clicked is: "+recyclerView.getChildPosition(child),Toast.LENGTH_SHORT).show();
// ImageView imageView1 = imageView;
//TextView textView1 = textView;
im = ((ViewHolder1) recyclerView.getTag()).imageView;
te = ((ViewHolder1) recyclerView.getTag()).textView;
im.setImageResource(ICONS2[(recyclerView.getChildPosition(child))-1]);
te.setTextColor(Color.parseColor("#000000"));
Log.i("text",String.valueOf(te.toString()));
return true;
}
return false;
}
我已经搜索了很多并在stackoverflow上进行了搜索,但是没有任何工作。请帮助我,因为一些事情而被困在我身上。
答案 0 :(得分:2)
尝试将根视图添加到viewHolder并将onclick侦听器附加到它。
public class ViewHolder extends RecyclerView.ViewHolder {
public View root;
public ViewHolder ( View itemView ) {
super ( itemView );
this.root = itemView;
}
}
在onBindViewHolder中设置onClickListener,如此
holder.root.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.imageView.setImageResource("whatever image");
holder.textView.setTextColor(Color.parseColor("#000000"));
}
});
编辑:
尝试将根视图添加到ViewHolder,如下所示:
答案 1 :(得分:1)
使用xml id从视图中获取Imageview,因为您没有将标记设置为ViewHolder
ImageView img = (ImageView )child.findViewById(R.id.youXmlName);