I'm using Roboletric and I would like to test the onClick into the "viewHolder.cardView" element.
This is my custom adapter (that has two elements). I already build my list and adapter, but how I can test the onClick event with roboletric?
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_TIME_FORMAT, Locale.getDefault());
if (position < mMessageList.size()) {
ItemHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(getContext())
.inflate(R.layout.item_list, parent, false);
viewHolder = new ItemHolder();
viewHolder.cardView = (RelativeLayout) convertView.findViewById(R.id.clickable_area);
viewHolder.txtDate = (TextView) convertView.findViewById(R.id.message_date);
convertView.setTag(viewHolder);
} else {
viewHolder = (ItemHolder) convertView.getTag();
}
final ItemModel item = mMessageList.get(position);
if (item != null) {
viewHolder.id = item.getId();
if (item.getTimestamp() != null){
viewHolder.txtDate.setText(dateFormat.format(item.getTimestamp()));
}
viewHolder.cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
addSelection();
}
});
viewHolder.cardView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
return true;
}
});
viewHolder.cardView.setSelected(item.isSelected());
}
}
return convertView;
}
static class ItemHolder {
protected RelativeLayout cardView;
protected TextView txtDate;
}
Here is what we are doing similar:
SiteAdapter.ViewHolder viewHolder = adapter.onCreateViewHolder(viewGroup, -1);
adapter.setSites(SITES);
adapter.bindViewHolder(viewHolder, 0);
viewHolder.rowLayout.performClick();
verify(busMock).send(selectedEventCaptor.capture());
assertThat(selectedEventCaptor.getValue().getSite()).isEqualTo(SITES.get(0));
where:
#Captor
private ArgumentCaptor<SiteSelectedEvent> selectedEventCaptor;
#Mock
private EventBus busMock;
private static final List<Site> SITES =
Arrays.asList(new Site("1", "Test 1", new Links("one", "two")),
new Site("2", "Test 2", new Links("three", "four")));
Related
I dont know why my output of listview always in wrong order after I update the data in my activity.
This is how I initialize the adapter in my activity
inboxTradeAdapter=new InboxTradeAdapter(InboxTradeActivity.this, R.layout.activity_inbox_trade, tradeArrayList,loginUserId);
tradeList.setAdapter(inboxTradeAdapter);
I call
inboxTradeAdapter.notifyDataSetChanged();
after tradeArrayList is updated
This is my Adapter
public class InboxTradeAdapter extends ArrayAdapter<Trade> {
private List<Trade> tradeList;
private Context context;
private String loginUserId;
public InboxTradeAdapter(Context context, int resource, List<Trade> tradeList, String loginUserId) {
super(context, resource, tradeList);
this.context = context;
this.tradeList = tradeList;
this.loginUserId=loginUserId;
}
#Override
public boolean isEnabled(int position) {
return true;
}
#Override
public int getCount() {
return ((null != tradeList) ?
tradeList.size() : 0);
}
#Override
public Trade getItem(int position) {
return ((null != tradeList) ?
tradeList.get(position) : null);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (null == view) {
view = layoutInflater.inflate(R.layout.grid_category, null);
}
final Trade trade = tradeList.get(position);
if (trade != null) {
final CardView tradeLayout = (CardView) view.findViewById(R.id.category_gridlayout);
final TextView tradeName = (TextView) view.findViewById(R.id.category_name);
final ImageView tradeImage = (ImageView) view.findViewById(R.id.category_icon);
tradeName.setText(trade.getNameOwn());
tradeName.setSelected(true);
tradeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Toast.makeText(context, category.getId()+" "+category.getName(), Toast.LENGTH_LONG).show();
Intent intent = new Intent(context, InboxActivity.class);
intent.putExtra("loginUserId", loginUserId);
intent.putExtra("tradeId",String.valueOf(trade.getId()));
context.startActivity(intent);
}
});
}
return view;
}
}
I can confirm that the tradeArrayList order is correct in my activity but the listview display the trade in random order. I cannot find the bug, please help
I noticed your not using a holder pattern. Always utilize it for optimization.
To the issue at hand, try refactoring your code using the holder pattern. You can learn more about it online.
There is no need overriding the getItem() method too.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
Trade trade = getItem(position);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.grid_category, parent, false);
holder = new ViewHolder();
holder.tradeName = (TextView) convertView.findViewById(R.id.category_name);
holder.tradeLayout = (CardView) convertView.findViewById(R.id.category_gridlayout);
holder.tradeImage = (ImageView) convertView.findViewById(R.id.category_icon);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tradeName.setText(trade.getNameOwn());
holder.tradeName.setSelected(true);
holder.tradeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Toast.makeText(context, category.getId()+" "+category.getName(), Toast.LENGTH_LONG).show();
Intent intent = new Intent(context, InboxActivity.class);
intent.putExtra("loginUserId", loginUserId);
intent.putExtra("tradeId",String.valueOf(trade.getId()));
context.startActivity(intent);
}
});
}
return convertView;
}
}
Your viewholder class code:
static class ViewHolder {
private TextView tradeName;
private TextView tradeLayout;
private ImageView tradeImage;
}
I have found the solution, create a view holder in adapter and create a setter to set the tradeList before call the notifyDataSetChanged()
This is my answer
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
final Trade trade = tradeList.get(position);
Log.d("worked",position+": "+trade.getId());
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.list_trade, parent, false);
holder = new ViewHolder();
holder.tradeName = (TextView) convertView.findViewById(R.id.trade_name);
holder.tradeLayout = (CardView) convertView.findViewById(R.id.trade_listlayout);
holder.tradeImage = (ImageView) convertView.findViewById(R.id.trade_icon);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (trade != null) {
holder.tradeName.setText(trade.getNameOwn());
holder.tradeName.setSelected(true);
try {
Picasso.with(context)
.load(trade.getImageOwnOne())
.error(R.drawable.ic_menu_camera)
.resize(200, 200)
.into(holder.tradeImage);
}catch(Exception e){
holder.tradeImage.setImageResource(R.drawable.ic_menu_camera);
}
holder.tradeImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Toast.makeText(context, category.getId()+" "+category.getName(), Toast.LENGTH_LONG).show();
Intent intent = new Intent(context, DisplayTradeActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("trade", trade);
intent.putExtras(bundle);
context.startActivity(intent);
}
});
holder.tradeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Toast.makeText(context, category.getId()+" "+category.getName(), Toast.LENGTH_LONG).show();
Intent intent = new Intent(context, InboxActivity.class);
intent.putExtra("loginUserId", loginUserId);
intent.putExtra("tradeId",String.valueOf(trade.getId()));
context.startActivity(intent);
}
});
}
return convertView;
}
public void setTradeList(ArrayList<Trade> tl) {
this.tradeList = tl;
}
static class ViewHolder {
private TextView tradeName;
private CardView tradeLayout;
private ImageView tradeImage;
}
Description:
I'm currently implementing a custom ListViewAdapter using the ViewHolder design pattern. The adapter is using a layout with a button that toggles the visibility of a RelativeLayout.
Problem:
When I click the button and the RelativeLayout is shown, everything seems to work fine until I scroll down. When I scroll down, I see that other listitems have been effected by the previous button click, so the RelativeLayout is visible on other (wrong) items. The item that is effected by the button click seems to be the one that is recycled when I scroll down. Please note that everything else works correct, texts etc. are set at the correct position.
Code:
CustomListAdapter
public class ItemsListAdapter extends ArrayAdapter<Item> {
private Context mContext;
private List<Item> mItems;
public ItemsListAdapter(Context context, int resource, List<Item> objects) {
super(context, resource);
mContext = context;
mItems = objects;
}
static class ViewHolder {
View mView;
boolean mExpanded;
public ViewHolder(View view, boolean expanded)
{
mView = view;
mExpanded = expanded;
}
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
final Item item = getItem(position);
final ViewHolder holder;
if (v == null) {
LayoutInflater mInflater = (LayoutInflater)
mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
v = mInflater.inflate(R.layout.custom_list_item, null);
holder = new ViewHolder(v, false);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
if(item != null)
{
TextView tvName = (TextView) holder.mView.findViewById(R.id.tv_name);
tvName.setText(item.getName());
RelativeLayout rlExpand = (RelativeLayout) holder.mView.findViewById(R.id.rl_expand);
if(holder.mExpanded) {
rlExpand.setVisibility(View.VISIBLE);
}
else {
rlExpand.setVisibility(View.GONE);
}
TextView tvDescription = (TextView) holder.mView.findViewById(R.id.tv_description);
if(tvDescription != null)
tvDescription.setText(item.getDescription());
final ImageButton ibExpand = (ImageButton) holder.mView.findViewById(R.id.ib_expand);
ibExpand.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RelativeLayout rlExpand = (RelativeLayout) holder.mView.findViewById(R.id.rl_expand);
if (rlExpand.getVisibility() == View.GONE) {
v.setVisibility(View.INVISIBLE);
rlExpand.setVisibility(View.VISIBLE);
holder.mExpanded = true;
}
}
});
ImageButton ibCollapse = (ImageButton) holder.mView.findViewById(R.id.ib_collapse);
ibCollapse.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RelativeLayout rlExpand = (RelativeLayout) holder.mView.findViewById(R.id.rl_expand);
ibExpand.setVisibility(View.VISIBLE);
rlExpand.setVisibility(View.GONE);
holder.mExpanded = false;
}
});
}
return v;
}
#Override
public int getCount() {
return mItems.size();
}
#Override
public Truism getItem(int position) {
return mItems.get(position);
}
Any help is highly appreciated! Thank you in advance!
Try to change:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
final Item item = getItem(position);
final ViewHolder holder;
if (v == null) {
LayoutInflater mInflater = (LayoutInflater)
mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
v = mInflater.inflate(R.layout.custom_list_item, null);
holder = new ViewHolder(v, false);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
if(item != null)
{
TextView tvName = (TextView) holder.mView.findViewById(R.id.tv_name);
tvName.setText(item.getName());
RelativeLayout rlExpand = (RelativeLayout) holder.mView.findViewById(R.id.rl_expand);
if(item.isExpanded) {
rlExpand.setVisibility(View.VISIBLE);
}
else {
rlExpand.setVisibility(View.GONE);
}
TextView tvDescription = (TextView) holder.mView.findViewById(R.id.tv_description);
if(tvDescription != null)
tvDescription.setText(item.getDescription());
[...]
}
return v;
}
Change holder.mExpanded to item.isExpanded
I need your help. My problem is that I need to highlight the selected listview item. Note that it only needs to set single item, not multiple.
Here's what I've tried so far:
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if(convertView == null) {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_video_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.background = (LinearLayout) convertView.findViewById(R.id.background);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
result = new HashMap<>();
result = data.get(position);
if(firstTimeStartup && position == 0) { // default selected
result = data.get(0);
highlightCurrentRow(viewHolder.background);
} else {
unhighlightCurrentRow(viewHolder.background);
}
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result = data.get(position);
if (firstTimeStartup) { // first time highlight first row
firstTimeStartup = false;
result = data.get(0);
unhighlight(viewHolder.background, 0);
}
highlightCurrentRow(viewHolder.background);
});
return convertView;
}
private void unhighlightCurrentRow(View rowView) {
rowView.setBackgroundResource(android.R.color.transparent);
}
private void highlightCurrentRow(View rowView) {
rowView.setBackgroundResource(R.color.main_blue);
}
private Boolean firstTimeStartup = true;
But the problem is I couldn't unhighlight the item when I clicked the convertView. Also the default (first) item is not unhighlighting when I tried to click other items. Would appreciate any help. Thanks!
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result = data.get(position);
for(int i=0;i<data.size();i++){
if(i == position){
highlightCurrentRow(viewHolder.background);
}
else{
unhighlightCurrentRow(viewHolder.background);
}
}
});
create a method like this :
CustomAdapter extends BaseAdapter {
int selectedPosition=0;
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if(position==selectedPosition){
highlightCurrentRow(viewHolder.background);
}else{
unhighlightCurrentRow(viewHolder.background);
}
}
void highlightUnhighlight(int position){
seslectedPostion= postion;
notifyDatasetChanged
}
}
Call highlightUnhighlight(postion) in onItemClickListener.
I edited your code ,please check this code may be your problem will solve. I used this code in my project.
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if(convertView == null) {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_video_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.background = (LinearLayout) convertView.findViewById(R.id.background);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
result = new HashMap<>();
result = data.get(position);
if(firstTimeStartup[position]) { // default selected
result = data.get(0);
highlightCurrentRow(viewHolder.background);
} else {
unhighlightCurrentRow(viewHolder.background);
}
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result = data.get(position);
if (firstTimeStartup[position]) {
Arrays.fill(firstTimeStartup, Boolean.FALSE);
firstTimeStartup[position] = false;
result = data.get(0);
unhighlight(viewHolder.background, 0);
}else{
Arrays.fill(firstTimeStartup, Boolean.FALSE);
firstTimeStartup[position] = true;
highlightCurrentRow(viewHolder.background);
}
notifyDataSetChanged();
});
return convertView;
}
private void unhighlightCurrentRow(View rowView) {
rowView.setBackgroundResource(android.R.color.transparent);
}
private void highlightCurrentRow(View rowView) {
rowView.setBackgroundResource(R.color.main_blue);
}
private Boolean firstTimeStartup[] = new Boolean[data.size()];
firstTimeStartup[0]=true;
I want to set the visibility to true of btnProductDetailMinus to true on click of btnProductDetailAddToCart. This is my code.
public class ProductVariantAdapter extends BaseAdapter {
private Context context;
private ArrayList<ProductVariant> productVariants = new ArrayList<>();
/*private view holder class*/
private class ViewHolder {
TextView productVariantName;
TextView productVariantMrp;
TextView productVariantSellPrice;
Button btnProductDetailAddToCart, btnProductDetailPlus, btnProductDetailQty, btnProductDetailMinus;
}
public ProductVariantAdapter(Context context, ArrayList<ProductVariant> productVariants) {
this.context = context;
this.productVariants = productVariants;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listview_product_variant, null);
holder = new ViewHolder();
holder.productVariantName = (TextView) convertView.findViewById(R.id.productVariantName);
holder.productVariantMrp = (TextView) convertView.findViewById(R.id.productVariantMrp);
holder.productVariantSellPrice = (TextView) convertView.findViewById(R.id.productVariantSellPrice);
holder.btnProductDetailAddToCart = (Button) convertView.findViewById(R.id.btnProductDetailAddToCart);
holder.btnProductDetailPlus = (Button)convertView.findViewById(R.id.btnProductDetailPlus);
holder.btnProductDetailQty = (Button)convertView.findViewById(R.id.btnProductDetailQty);
holder.btnProductDetailMinus = (Button)convertView.findViewById(R.id.btnProductDetailMinus);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ProductVariant productVariant = (ProductVariant) getItem(position);
if (productVariant != null) {
holder.productVariantName.setText(productVariant.getVariant().getVariantName());
holder.productVariantMrp.setText(productVariant.getMrp().toString());
holder.productVariantSellPrice.setText(productVariant.getSellPrice().toString());
holder.btnProductDetailAddToCart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Utility.displayToast("wonderful");
holder.btnProductDetailMinus.setVisibility(View.VISIBLE);
}
});
} else {
Toast.makeText(context, "product variant is null", Toast.LENGTH_SHORT).show();
}
return convertView;
}
#Override
public int getCount() {
return productVariants.size();
}
#Override
public Object getItem(int position) {
return productVariants.get(position);
}
#Override
public long getItemId(int position) {
return productVariants.indexOf(getItem(position));
}
}
But its giving error "Variable holder is accessed from within inner class. needs to declared final"
This is the line giving error.
holder.btnProductDetailMinus.setVisibility(View.VISIBLE);
How to fix this?
Assign holder tot a final variabele:
final ViewHolder fHolder = holder;
Then in onClick:
fHolder.btnProductDetailMinus.setVisibility(View.VISIBLE);
Move this line out of the if (convertView == null)
holder = new ViewHolder();
You can declare final ViewHolder holder = null; instead of ViewHolder holder = null;
You can just make it a Global variable.
set public static class ViewHolder instead of private class ViewHolder
One of the approach that I follow is create a Map or List in Adapter class and onClick set value and that collection, and in adapter getView method check for the collection value. .
Checkout the below code.
public class ProductVariantAdapter extends BaseAdapter {
private Context context;
private ArrayList<ProductVariant> productVariants = new ArrayList<>();
private Map<Integer, Boolean> visibilityMap = new HashMap<>();
/*private view holder class*/
private class ViewHolder {
TextView productVariantName;
TextView productVariantMrp;
TextView productVariantSellPrice;
Button btnProductDetailAddToCart, btnProductDetailPlus, btnProductDetailQty, btnProductDetailMinus;
}
public ProductVariantAdapter(Context context, ArrayList<ProductVariant> productVariants) {
this.context = context;
this.productVariants = productVariants;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listview_product_variant, null);
holder = new ViewHolder();
holder.productVariantName = (TextView) convertView.findViewById(R.id.productVariantName);
holder.productVariantMrp = (TextView) convertView.findViewById(R.id.productVariantMrp);
holder.productVariantSellPrice = (TextView) convertView.findViewById(R.id.productVariantSellPrice);
holder.btnProductDetailAddToCart = (Button) convertView.findViewById(R.id.btnProductDetailAddToCart);
holder.btnProductDetailPlus = (Button)convertView.findViewById(R.id.btnProductDetailPlus);
holder.btnProductDetailQty = (Button)convertView.findViewById(R.id.btnProductDetailQty);
holder.btnProductDetailMinus = (Button)convertView.findViewById(R.id.btnProductDetailMinus);
holder.btnProductDetailAddToCart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = (int)v.getTag();
Utility.displayToast("wonderful");
visibilityMap.put(position, true);
notifyDataSetChanged();
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.btnProductDetailAddToCart.setTag(position);
ProductVariant productVariant = (ProductVariant) getItem(position);
if (productVariant != null) {
holder.productVariantName.setText(productVariant.getVariant().getVariantName());
holder.productVariantMrp.setText(productVariant.getMrp().toString());
holder.productVariantSellPrice.setText(productVariant.getSellPrice().toString());
} else {
Toast.makeText(context, "product variant is null", Toast.LENGTH_SHORT).show();
}
if(visibilityMap.containsKey(position) && visibilityMap.get(position)){
holder.btnProductDetailMinus.setVisibility(View.VISIBLE);
}else{
holder.btnProductDetailMinus.setVisibility(View.GONE);
}
return convertView;
}
#Override
public int getCount() {
return productVariants.size();
}
#Override
public Object getItem(int position) {
return productVariants.get(position);
}
#Override
public long getItemId(int position) {
return productVariants.indexOf(getItem(position));
}
}
I have got a simple navigation drawer with a header (see picture).
What I want to do now is whenever the header (listview-item with position 0) is clicked to either change the down-arrow-icon (bottom right corner of header) to an up-arrow-icon or to rotate it so the arrow points up. How can i achieve this with my adapter?
My List-Adapter:
public class MyAdapter extends BaseAdapter {
private String[] _navTitles;
private int[] _navIcons;
private String _nameHeader;
private int _profileHeader;
private String _emailHeader;
public int _arrowHeader;
public MyAdapter(String[] navTitles, int[] navIcons, String nameHeader, String emailHeader, int profileHeader, int arrowHeader) {
_navTitles = navTitles;
_navIcons = navIcons;
_nameHeader = nameHeader;
_emailHeader = emailHeader;
_profileHeader = profileHeader;
_arrowHeader = arrowHeader;
}
#Override
public int getCount() {
return _navTitles.length;
}
#Override
public Object getItem(int position) {
return _navTitles[position];
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder viewHolder = null;
if (view == null) {
if (position == 0) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.header, parent, false);
viewHolder = new ViewHolder();
viewHolder.profileView = (CircleImageView) view.findViewById(R.id.circleView);
viewHolder.nameView = (TextView) view.findViewById(R.id.name);
viewHolder.emailView = (TextView) view.findViewById(R.id.email);
viewHolder.arrowView = (ImageView) view.findViewById(R.id.arrow);
viewHolder.holderID = 0;
view.setTag(viewHolder);
} else {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row, parent, false);
viewHolder = new ViewHolder();
viewHolder.textView = (TextView) view.findViewById(R.id.rowText);
viewHolder.imageView = (ImageView) view.findViewById(R.id.rowIcon);
viewHolder.holderID = 1;
view.setTag(viewHolder);
}
} else {
viewHolder = (ViewHolder) view.getTag();
}
if (_navTitles.length > position && position != 0) {
viewHolder.textView.setText(_navTitles[position]);
viewHolder.textView.setTag(_navTitles[position]);
viewHolder.imageView.setImageResource(_navIcons[position]);
viewHolder.imageView.setTag(_navIcons[position]);
}
if (position == 0) {
viewHolder.profileView.setImageResource(_profileHeader);
viewHolder.nameView.setText(_nameHeader);
viewHolder.emailView.setText(_emailHeader);
viewHolder.arrowView.setImageResource(_arrowHeader);
}
return view;
}
public static class ViewHolder {
int holderID;
TextView textView;
ImageView imageView;
CircleImageView profileView;
TextView nameView;
TextView emailView;
ImageView arrowView;
}
}
Thanks for any help!
I solved it by myself. If anybody needs it:
final ViewHolder finalViewHolder = viewHolder;
viewHolder.arrowView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (finalViewHolder.arrowView.getTag().equals("down")) {
finalViewHolder.arrowView.setImageResource(R.drawable.ic_arrow_drop_up_white_48dp);
finalViewHolder.arrowView.setTag("up");
} else if (finalViewHolder.arrowView.getTag().equals("up")) {
finalViewHolder.arrowView.setImageResource(R.drawable.ic_arrow_drop_down_white_48dp);
finalViewHolder.arrowView.setTag("down");
}
}
});