Onitemclick listener not triggered in Listview - android

I have a listview, and I am using OnItemClickListener for that listview. At the same time, in getview method of my custom adapter I am using onclicklistener for the entire row view.And I figured out that when using Onclicklisnter in custom adapter, my OnItemClickListener of listview is not triggered. I want to use both these listeners at the same time. Can anyone help me to do it? Thanks in advance.
My listview listener,
list_specialists.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
HashMap<String, String> trends_data= new HashMap<String, String>();
trends_data = specobj.get(position);
specialist.setText(trends_data.get(KEY_SPEC_TITLE));
new GetTime(BookingActivity.this).execute();
}
});
And my getview method inside custom adapter,
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = convertView;
final ViewHolder holder;
if (rowView == null) {
rowView = inflater.inflate(R.layout.row_timeslot, parent, false);
holder = new ViewHolder();
holder.title = (TextView) rowView.findViewById(R.id.textView2);
holder.layout = (RelativeLayout) rowView
.findViewById(R.id.relativeLayout1);
holder.check = (org.holoeverywhere.widget.CheckBox) rowView
.findViewById(R.id.checkBox1);
rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
}
HashMap<String, String> trends_data = new HashMap<String, String>();
trends_data = trendsobj.get(position);
holder.check.setId(position);
holder.check.setChecked(checkarry[position]);
// for managing the state of the boolean
// array according to the state of the
// CheckBox
holder.check.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
for (int i = 0; i < checkarry.length; i++) {
if (position == i) {
checkarry[i] = true;
} else {
checkarry[i] = false;
}
RefreshList();
}
}
});
holder.layout.setOnClickListener(new View.OnClickListener() { // Here I am using onclicklistener for rows's parent layout.
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
for (int i = 0; i < checkarry.length; i++) {
if (position == i) {
checkarry[i] = true;
} else {
checkarry[i] = false;
}
RefreshList();
}
}
});
holder.title.setText(trends_data.get(BookingActivity.KEY_TIME_TITLE));
return rowView;
}
rowlayout xml file...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:focusable="false"
android:background="#drawable/list_selector"
android:orientation="vertical"
android:padding="16dp" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/activity_googlecards_card_imageview"
android:text="Rate "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#757572" />
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/textView3"
android:text="AED 200.00"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/mainservice" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/activity_googlecards_card_imageview"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/activity_googlecards_card_imageview"
android:gravity="center"
android:text="Specialist Name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#6945B2"
android:textStyle="bold" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/activity_googlecards_card_imageview"
android:layout_alignParentRight="true"
android:focusable="false"
android:focusableInTouchMode="false" />
<ImageView
android:id="#+id/activity_googlecards_card_imageview"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:scaleType="centerCrop"
android:src="#drawable/list_dummy" />
</RelativeLayout>*

If there is any clickalbe item in the list row, whose click() action will be taking place instead of onItemClickListener. Here you have a checkbox in the row item, and if you still want to use the onItemClickListener, you must make the CheckBox not focusable in the layout as follows:
android:focusable="false"
android:focusableInTouchMode="false"

Related

On clicking the button in listview item, change occurs in some others listview items too

I am making an app in which there are posts with comments and description. There are three buttons on each post. 1 description 2 comments and third is like button. I set a button click listener in getview method of custom adapter. When i click on description button the description of that post should be displayed under the button. But when i clicked the description button, the description of some other listview items also displayed. I just want to show the description of that post whose description button is clicked. Here is my code:
getview code:
public View getView(int position, View convertView, ViewGroup parent) {
a = getItem(position);
View view = convertView;
try
{
if (convertView == null) {
v = new ViewHolder();
convertView = LayoutInflater.from(getContext()).inflate(R.layout.custom_list_item, parent, false);
v.imgView = (ImageView) convertView.findViewById(R.id.iv_list_image);
v.desc = (Button) convertView.findViewById(R.id.btn_desc);
v.des = (TextView) convertView.findViewById(R.id.tv_list_desc);
v.ll = (LinearLayout) convertView.findViewById(R.id.ll_desc);
convertView.setTag(v);
}
else {
v = (ViewHolder) convertView.getTag();
}
v.desc.setTag(position);
//Picasso.with(getContext()).load(a.getFile()).fit().into(v.imgView);
Glide.with(context).load(a.getFile()).centerCrop().placeholder(R.drawable.dualring).into(v.imgView);
v.desc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v1) {
Toast.makeText(getContext(), ""+v.desc.getTag(), Toast.LENGTH_SHORT).show();
v.des.setText(""+a.getDescription());
v.ll.setVisibility(View.VISIBLE);
}
});
return convertView;
}catch (Exception e)
{
return null;
}
}
XML code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:paddingBottom="35dp">
<ImageView
android:id="#+id/iv_list_image"
android:layout_width="match_parent"
android:layout_height="300dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="6">
<Button
android:id="#+id/btn_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Description"
/>
<Button
android:id="#+id/btn_comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Comments"
/>
<Button
android:id="#+id/btn_likes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Likes"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/ll_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp"
android:background="#ffffff"
android:visibility="invisible"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Description:"
android:textStyle="bold"/>
<TextView
android:id="#+id/tv_list_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Default Desc"
android:textColor="#333"
/>
</LinearLayout>
</LinearLayout>
That's when you use an int flag for the list item's position as a conditional statement before updating the ListView's items.
So in your case, the Adapter class would look something like this:
...
private int mPosition = -1; // Int flag that doesn't take effect UNTIL it has been set
...
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
a = getItem(position);
View view = convertView;
try {
if (convertView == null) {
v = new ViewHolder();
convertView = LayoutInflater.from(getContext()).inflate(R.layout.custom_list_item, parent, false);
v.imgView = (ImageView) convertView.findViewById(R.id.iv_list_image);
v.desc = (Button) convertView.findViewById(R.id.btn_desc);
v.des = (TextView) convertView.findViewById(R.id.tv_list_desc);
v.ll = (LinearLayout) convertView.findViewById(R.id.ll_desc);
convertView.setTag(v);
} else {
v = (ViewHolder) convertView.getTag();
}
v.desc.setTag(position);
//Picasso.with(getContext()).load(a.getFile()).fit().into(v.imgView);
Glide.with(context).load(a.getFile()).centerCrop().placeholder(R.drawable.dualring)
.into(v.imgView);
// Runs the following functionality if the positions match. Otherwise, hides the layout.
if (mPosition == position) {
Toast.makeText(getContext(), ""+v.desc.getTag(), Toast.LENGTH_SHORT).show();
v.des.setText(""+a.getDescription());
v.ll.setVisibility(View.VISIBLE);
} else {
v.ll.setVisibility(View.INVISIBLE);
}
v.desc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v1) {
mPosition = position; // Sets the flag to the position that was clicked
notifyDataSetChanged(); // Updates the data instantly
}
});
return convertView;
} catch (Exception e) {
return null;
}
}
Let me know if that works.
The best way is to implement the on click listener in adapter and set the tag on button. After that in onclick method perform the desired task through getting tag id of button, this will definitely work.

how to hide view appear on top of previous selected row on click of another row inside GridView in android

I have a Fragment with custom Adapter extending BaseAdapter that manages a GridView.
I want to show images on each row of the GridView and onClick of each cell, I want to open a translucent view on top of that particular image which will show image details.
Problem is that when I click on the image, a translucent view opened on top of the image but when I click on next image then a translucent view opened on previous images is not hiding,it still appears over the first images..
See the following code
My Fragment
public class ProductFragment extends Fragment {
String ss = null;
GridView gridView;
ArrayList<ProductParameterBO> productlist;
GridViewCustomAdapter gridViewCustomAdapter;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (savedInstanceState!=null){
View rootView = inflater.inflate(R.layout.empty_catalog_item,null);
TextView configure = (TextView)rootView.findViewById(R.id.text_conf);
configure.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(),SplitViewActivity.class);
intent.putExtra("firstTab","1stTabs");
startActivity(intent);
}
});
TextView text_here = (TextView)rootView.findViewById(R.id.text_here);
text_here.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
FloatingActionButton fab = (FloatingActionButton)rootView.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), EditUploadActivity.class);
startActivity(intent);
}
});
return rootView;
} else {
View rootView = inflater.inflate(R.layout.fragment_product,null);
gridView =(GridView)rootView.findViewById(R.id.gridView);
productlist = new ArrayList<ProductParameterBO>();
productlist.add(new ProductParameterBO("Type","Numeric","yes","yes"));
productlist.add(new ProductParameterBO("Typess","Numericss","yesss","yesss"));
productlist.add(new ProductParameterBO("Typess","Numericss","yesss","yesss"));
productlist.add(new ProductParameterBO("Typess","Numericss","yesss","yesss"));
gridViewCustomAdapter = new GridViewCustomAdapter(getActivity(),productlist);
gridView.setAdapter(gridViewCustomAdapter);
gridViewCustomAdapter.notifyDataSetChanged();
return rootView;
}
// return inflater.inflate(R.layout.fragment_product,null);
}
}
My Adater
public class GridViewCustomAdapter extends BaseAdapter {
private List<ProductParameterBO> availList;
private LayoutInflater inflater;
Context context;
public GridViewCustomAdapter(Context ctx,List<ProductParameterBO> list){
this.context = ctx;
this.availList = list;
}
#Override
public int getCount() {
return availList.size();
}
#Override
public Object getItem(int position) {
return availList.get(position);
}
#Override
public long getItemId(int position) {
ProductParameterBO c = availList.get(position);
// long id = c.getTimeId();
return 0;
}
#Override
public View getView(int position,View convertView,final ViewGroup parent) {
View row = convertView;
final TeeTimeHolder holder;
if (row == null){
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.grid_row,parent,false);
holder = new TeeTimeHolder();
holder.myImage =(ImageView)row.findViewById(R.id.imageView10);
holder.name =(TextView)row.findViewById(R.id.textView47);
holder.edit =(TextView)row.findViewById(R.id.textView49);
holder.rl =(RelativeLayout)row.findViewById(R.id.img_ovrly);
row.setTag(holder);
} else {
holder =(TeeTimeHolder)row.getTag();
}
holder.name.setText(availList.get(position).getParameterName());
holder.myImage.setImageResource(R.drawable.toi);
holder.rl.setVisibility(View.GONE);
holder.myImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View rows = null;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rows = inflater.inflate(R.layout.image_overlay,parent,false);
// holder.rl = (RelativeLayout)rows.findViewById(R.id.img_ovrly);
holder.rl.setVisibility(View.VISIBLE);
}
});
return row;
}
static class TeeTimeHolder {
ImageView myImage;
TextView name,edit;
RelativeLayout rl;
//TextView descriptions;
/* TextView coursefee;
TextView viewdetils;*/
}
}
My XML layout grid_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView10"
android:src="#drawable/toi"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Image Name"
android:id="#+id/textView47"
android:layout_below="#+id/imageView10"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edit"
android:id="#+id/textView49"
android:layout_below="#+id/textView47"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/img_ovrly"
android:background="#color/opacity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/mail"
android:layout_marginTop="10dp"
android:id="#+id/image_i"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title :"
android:id="#+id/text_ttle"
android:layout_below="#+id/image_i"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:textColor="#ffffff"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/text_ttle"
android:text="hmmmm"
android:layout_below="#+id/image_i"
android:layout_marginTop="20dp"
android:textColor="#6ec6c5"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dimensions :"
android:id="#+id/text_dimen"
android:layout_below="#+id/text_ttle"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:textColor="#ffffff"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/text_dimen"
android:text="32*23"
android:layout_below="#+id/text_ttle"
android:layout_marginTop="10dp"
android:textColor="#6ec6c5"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Price :"
android:id="#+id/text_prce"
android:layout_below="#+id/text_dimen"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:textColor="#ffffff"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/text_prce"
android:text="32*23"
android:layout_below="#+id/text_dimen"
android:layout_marginTop="10dp"
android:textColor="#6ec6c5"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Color :"
android:id="#+id/text_clr"
android:layout_below="#+id/text_prce"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:textColor="#ffffff"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/text_clr"
android:text="red,white"
android:layout_below="#+id/text_prce"
android:layout_marginTop="10dp"
android:textColor="#6ec6c5"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Min. Quantity :"
android:id="#+id/text_minq"
android:layout_below="#+id/text_clr"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:textColor="#ffffff"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/text_minq"
android:text="1000"
android:layout_below="#+id/text_clr"
android:layout_marginTop="10dp"
android:textColor="#6ec6c5"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Type :"
android:id="#+id/text_typ"
android:layout_below="#+id/text_minq"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:textColor="#ffffff"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/text_typ"
android:text="creamic"
android:layout_below="#+id/text_minq"
android:layout_marginTop="10dp"
android:textColor="#6ec6c5"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/img_flp"
android:background="#drawable/flip"
android:layout_below="#+id/text_typ"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"/>
</RelativeLayout>
</RelativeLayout>
Am I missing something? kindly help me to resolve this issue. I want a screen something like that. Am I doing right to open tranlucent screen on top of image and how to hide or make visivility gone on click of other images inside my Adater class.Any help would be appreciated in advanced
You can do one of two things.
1. Use a flag
NOTE this code is untested.
You can create an encapsulated flag in your ProductParameterBO object to determine the visibility of your overlay view. Something like the following
boolean mInformationViewVisible = false;
public void setInformationViewVisible(boolean visible) {
mInformationViewVisible = visible;
}
public boolean isInformationViewVisible() {
return mInformationViewVisible;
}
Then use that in your getView method to set the visibility of the view.
#Override
public View getView(int position,View convertView,final ViewGroup parent) {
...
holder.myImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for(int itemPosition = 0; itemPosition < availList.size(); itemPosition++) {
if(itemPosition != position){
availList.get(itemPosition)
.setInformationViewVisible(false);
}
availList.get(itemPosition).setInformationViewVisible(true);
}
});
if (currentItem.isInformationViewVisible()) {
holder.rl.setVisibility(View.VISIBLE);
} else {
holder.rl.setVisibility(View.GONE);
}
...
}
2. Switch to RecyclerView
The second and better option is to switch to RecyclerView. This view is specifically built for individual interaction with items in your Adapter. It gives you more power over the View in terms of animations and removal/adittion of items to the adapter. You can read more on it here and here.
It is very likely that the app in the image you attached is using RecyclerView to manage their content. The code to manage the selected info view will be very similar in RecyclerView to what I posted for the ListView, the only difference would be that you would pass the OnClickListener to the RecyclerView.ViewHolder and set it to the info view in there in stead of in the getView() method. I highly recommend using this approach as it will benefit you in the future of your app.
In ProductParameterBO, you can keep a boolean member variable, and in getView() and if one image is clicked, you can set the boolean variables from that list as false, except the one you clicked. If the variable is true, it will display the image details, and if the variable is false, you can set the visibillty of the translucent view as Invisible of that particular position.
#Override
public View getView(int position,View convertView,final ViewGroup parent) {
View row = convertView;
final TeeTimeHolder holder;
if (row == null){
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.grid_row,parent,false);
holder = new TeeTimeHolder();
holder.myImage =(ImageView)row.findViewById(R.id.imageView10);
holder.name =(TextView)row.findViewById(R.id.textView47);
holder.edit =(TextView)row.findViewById(R.id.textView49);
holder.rl =(RelativeLayout)row.findViewById(R.id.img_ovrly);
row.setTag(holder);
}
else {
holder =(TeeTimeHolder)row.getTag();
}
holder.name.setText(availList.get(position).getParameterName());
holder.myImage.setImageResource(R.drawable.toi);
holder.rl.setVisibility(View.GONE);
holder.myImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View rows = null;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rows = inflater.inflate(R.layout.image_overlay,parent,false);
// holder.rl = (RelativeLayout)rows.findViewById(R.id.img_ovrly);
// holder.rl.setVisibility(View.VISIBLE);
for(int i=0;i<availList.size();i++){
if(i!=position){
availList.get(i).flag=false;
}
}
availList.get(position).flag=true;
}
});
if(availList.get(i).flag){
holder.rl.setVisibility(View.VISIBLE);
}else{
holder.rl.setVisibility(View.INVISIBLE);
}
return row;
}

Iterating the ListView and change content of some rows (ListItem)

I have a ListView whose ListItems(RelativeLayout) have
A textView and two ImageButtons inside a LinearLayout.
Here is the xml of my ListItem;
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
>
<TextView
android:id="#+id/list_item_quote"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="15dp"
android:paddingTop="15dp"
style="#style/quote_text"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lytShareLike"
android:layout_gravity="center"
android:gravity="center"
android:visibility="gone"
android:layout_margin="1dp"
android:layout_alignLeft="#id/list_item_quote"
android:layout_alignTop="#id/list_item_quote"
android:layout_alignRight="#id/list_item_quote"
android:layout_alignBottom="#id/list_item_quote">
<ImageButton
android:layout_width="30dp"
android:layout_height="30dp"
android:scaleType="centerInside"
android:id="#+id/btnShare"
android:background="#null"
android:onClick="quoteShareClick"
android:src="#drawable/share"
android:layout_marginRight="10dp"
/>
<ImageButton
android:layout_width="30dp"
android:layout_height="30dp"
android:scaleType="centerInside"
android:id="#+id/btnLike"
android:background="#null"
android:src="#drawable/heartnot"
android:onClick="quoteLikeClick"
/>
</LinearLayout>
</RelativeLayout>
I have ItemClickListener which sets the LinearLayout visible when the ListItem is clicked. There is no problem until here.
What I want to do is, after clicking a ListItem, if the user clicks another ListItem, the other unclicked Listitems' LinearLayouts should be GONE again.
Here is my onClickListener;
listQuotes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
LinearLayout lytShareLike = (LinearLayout) view.findViewById(R.id.lytShareLike);
for (int i = 0; i < listQuotes.getAdapter().getCount(); i++) {
if (position != i && i != 0) {
LinearLayout lnrTemp = (LinearLayout) listQuotes.getAdapter().getView(i, null, null).findViewById(R.id.lytShareLike);
lnrTemp.setVisibility(View.GONE);
}
}
}
});
The for iteration iterates the items and LinearLayouts as expected but the LinearLayouts are still VISIBLE
What am I missing?
you can control it by using below code:
//...
int index = -1;
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
index = position;
mAdaper..notifyDataSetChanged();
}
//...
in the adapter:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//...
Holder holder;
if(convertView == null) {
convertView = ....;
holder = new Holder(convertView);
convertView.setTag(holder);
}
holder = (Holder) convertView.getTag();
if(index == position){
holder.linearLayout.setVisibility(View.VISIBLE);
} else {
holder.linearLayout.setVisibility(View.GONE);
}
//...
return convertView;
}

Toggle a "ToggleButton" when clicking anywhere outside it

I have a list (ExpandableListView), and every item of the list has a ToggleButton that shows/hides the child items when toggled (the child is always 1 and it's a sort of toolbar with two buttons). Thanks to this tutorial I could set a custom button instead of the expandablelistview's indicator and I made it so that I can do something else than showing the toolbar when clicking on a list item. Also, I used the answer to this question to automatically close an open toolbar when opening another one.
So, I need to collapse the currently expanded toolbar when touching anything on the screen that is not its ToggleButton (I would need to collapse it even when clicking on one of the toolbar's buttons, as long as its own "onClick" is sent anyway).
here's an image of the app:
Here's the ExpandableListView:
<ExpandableListView
android:id="#+id/normalList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:groupIndicator="#drawable/toggle_button_selector" >
</ExpandableListView>
Here's the xml of the group item:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/filesListDrawerLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp" >
<ImageView
android:id="#+id/img"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="2dp"
android:contentDescription="#string/icondescription" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="29dp"
android:layout_alignTop="#+id/img"
android:layout_marginLeft="15dp"
android:layout_toRightOf="#+id/img"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/counter"
android:layout_width="wrap_content"
android:layout_height="19dp"
android:layout_alignBottom="#+id/img"
android:layout_below="#+id/text"
android:layout_marginLeft="15dp"
android:layout_toRightOf="#+id/img"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceSmall" />
<ToggleButton
android:id="#+id/toggle"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentRight="true"
android:layout_below="#+id/text"
android:layout_marginRight="15dp"
android:background="#drawable/toggle_button_selector"
android:focusable="false"
android:focusableInTouchMode="false"
android:textColorLink="#android:color/transparent"
android:textOff=""
android:textOn="" />
The child item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/details"
android:layout_width="0dp"
android:layout_height="57dip"
android:layout_weight="1"
android:clickable="true"
android:drawableTop="#drawable/ic_action_about"
android:drawablePadding="-12dp"
android:background="#drawable/toolbar_selector"
android:gravity="center"
android:text="#string/details" />
<TextView
android:id="#+id/send"
android:layout_width="0dp"
android:layout_height="57dip"
android:layout_weight="1"
android:clickable="true"
android:drawableTop="#drawable/ic_action_new_email"
android:drawablePadding="-12dp"
android:background="#drawable/toolbar_selector"
android:gravity="center"
android:text="#string/send" />
</LinearLayout>
The relevant parts of the adapter:
public class CustomList extends BaseExpandableListAdapter {
private final Activity context;
public final List<Item> names; //Item is a custom Object
private final Integer[] imageId;
private int lastExpandedGroupPosition;
public CustomList(Activity context, List<Item> names, Integer[] imageId) {
this.context = context;
this.names = names;
this.imageId = imageId;
}
public View getGroupView(final int position, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_single, null);
}
TextView txtTitle = (TextView) convertView.findViewById(R.id.text);
TextView txtData = (TextView) convertView.findViewById(R.id.counter);
ImageView imageView = (ImageView) convertView.findViewById(R.id.img);
ToggleButton toggle = (ToggleButton) convertView.findViewById(R.id.toggle);
final ExpandableListView list = (ExpandableListView) parent.findViewById(R.id.normalList);
txtTitle.setText(names.get(position).getName());
txtData.setText(names.get(position).getData());
if(/*some conditions*/) {
imageView.setImageResource(imageId[0]);
toggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
list.expandGroup(position, true);
} else {
list.collapseGroup(position);
}
}
});
} else if(/*other conditions*/) {
imageView.setImageResource(imageId[2]);
toggle.setVisibility(View.GONE);
} else {
imageView.setImageResource(imageId[1]);
toggle.setVisibility(View.GONE);
}
return convertView;
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_child, null);
}
TextView details = (TextView) convertView.findViewById(R.id.details);
TextView send = (TextView) convertView.findViewById(R.id.send);
details.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//do something
}
});
send.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//do something else
}
});
return convertView;
}
#Override
public void onGroupExpanded(int groupPosition) {
ToggleButton button = (ToggleButton) ((ExpandableListView) context
.findViewById(R.id.normalList)).getChildAt(lastExpandedGroupPosition)
.findViewById(R.id.toggle);
if(groupPosition != lastExpandedGroupPosition && button.isChecked()) {
button.performClick();
}
super.onGroupExpanded(groupPosition);
lastExpandedGroupPosition = groupPosition;
}
}
And finally the important parts in the activity:
lv = (ExpandableListView)findViewById(R.id.normalList);
lv.setGroupIndicator(null);
lv.setOnGroupClickListener(new OnGroupClickListener() {
public boolean onGroupClick(ExpandableListView parent, View v, int position, long id) {
//do some things
return true; //this tells the list that it mustn't show the child items
}
});
adapter = new CustomList(myActivity.this, list, icons);
lv.setAdapter(adapter);
P.S.: The code to hide the open toolbars doesn't work properly, but I really can't understand why. But this is another story...
--- EDIT ---
About the P.S. above: now I know why that happens.
Your list looks like ExpandableListView. Did you try to use it? It has build it second level rows, expanding, hiding, etc. Simply add your pdf rows as group headers and then add tool rows as child rows. Then you can handle row clicks to show and hide rows you would like to show/hide. See: http://developer.android.com/reference/android/widget/ExpandableListView.html
And, btw., using additional rows with hiding is a bit wrong UX design. Maybe it would be better to use drop down lists or custom dialog windows? Both of them have built in functionality do disappear when touched outside.
You can simply put all your ToggleButton whenfilling the list in an ArrayList. This way you just have to force the click on each button of list toggled when one of them is clicked.
Ok I solved the problem.
Instead of toggling the buttons with "performClick()" or "setChecked" in some methods and managing the expand/collapse action in an OnCheckedChangedListener, I decided to store the status of the togglebuttons in an array, and to expand/collapse the child views in the "getGroupView" method depending on the state of the groupview's button, whose position in the array corresponds to the groupview's position in the list. To update the list, I call notifyDataSetChanged() on the adapter.
I added a FrameLayout around the ToggleButton to increase its clickable area.
Here's the code for clarity:
The relevant parts of the MainActivity:
public class MainActivity extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
.
.
.
ExpandableListView lv = (ExpandableListView) findViewById(R.id.normalList);
CustomList adapter = new CustomList(MainActivity.this, list /*the list of Items, declared elsewhere*/, lv);
lv.setAdapter(adapter);
DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.filesListDrawerLayout);
lv.setOnGroupClickListener(new OnGroupClickListener() {
public boolean onGroupClick(ExpandableListView parent, View v, int position, long id) {
uncheckButtons(mDrawerLayout);
//do some unrelated stuff
return true;
}
});
}
public void uncheckButtons(ViewGroup vg) {
for(int i = 0; i < adapter.buttons.length; ++i)
adapter.buttons[i] = false;
adapter.lastChecked = -1;
adapter.notifyList();
}
The relevant parts of the adapter:
public class CustomList extends BaseExpandableListAdapter {
private final Activity context;
public final List<Item> names; //Item is a custom Object
public boolean[] buttons;
public int lastChecked;
private final Integer[] imageId = new Integer[] { R.drawable.image1, R.drawable.image1, R.drawable.image3 };
private ExpandableListView list;
public CustomList(Activity context, List<Item> names, boolean forUpload, ExpandableListView list) {
this.context = context;
this.names = names;
this.list = list;
this.buttons = new boolean[names.size()];
this.lastChecked = -1;
}
private static class GroupHolder {
TextView txtTitle;
TextView txtData;
ImageView imageView;
ToggleButton toggle;
FrameLayout frame;
}
public View getGroupView(final int position, boolean isExpanded, View convertView, ViewGroup parent) {
GroupHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_single, null);
holder.txtTitle = (TextView) convertView.findViewById(R.id.text);
holder.txtData = (TextView) convertView.findViewById(R.id.counter);
holder.imageView = (ImageView) convertView.findViewById(R.id.img);
holder.toggle = (ToggleButton) convertView.findViewById(R.id.toggle);
holder.frame = (FrameLayout) convertView.findViewById(R.id.frame);
convertView.setTag(holder);
} else {
holder = (GroupHolder) convertView.getTag();
}
txtTitle.setText(names.get(position).getName());
txtData.setText(names.get(position).getData());
if(/*some conditions*/) {
imageView.setImageResource(imageId[0]);
holder.frame.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
if(lastChecked != -1) {//if there's a checked button
buttons[lastChecked] = !buttons[lastChecked];//uncheck it
if(position == lastChecked)//if I clicked on the last checked button
lastChecked = -1;//there's no checked button
else {
buttons[position] = !buttons[position];//check the button
lastChecked = position;//and set it as the last checked one
}
notifyList();
}
return false;
}
});
holder.toggle.setChecked(buttons[position]);
if(holder.toggle.isChecked()) {
if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB_MR2)
list.expandGroup(position);
else
list.expandGroup(position, true);
} else {
list.collapseGroup(position);
}
} else if(/*other conditions*/) {
//do other stuff with the views that don't interest us
}
return convertView;
}
private static class ChildHolder {
TextView details;
TextView send;
TextView other;
}
public View getChildView(final int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
ChildHolder holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_child, null);
holder = new ChildHolder();
holder.details = (TextView) convertView.findViewById(R.id.details);
holder.send = (TextView) convertView.findViewById(R.id.send);
holder.other = (TextView) convertView.findViewById(R.id.other);
} else {
holder = (ChildHolder) convertView.getTag();
}
//maybe I could manage the listeners more nicely and without redundance...
holder.details.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
buttons[lastChecked] = !buttons[lastChecked];//uncheck the only checked button (it always exists at this point)
lastChecked = -1;//there's no checked button
notifyList();
//call a certain method in the main activity...
}
});
holder.send.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
buttons[lastChecked] = !buttons[lastChecked];//uncheck the only checked button (it always exists at this point)
lastChecked = -1;//there's no checked button
notifyList();
//call a certain method in the main activity...
}
});
holder.other.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
buttons[lastChecked] = !buttons[lastChecked];//uncheck the only checked button (it always exists at this point)
lastChecked = -1;//there's no checked button
notifyList();
//call a certain method in the main activity...
}
});
return convertView;
}
public void notifyList() {
this.notifyDataSetChanged();
}
//I don't need OnGroupExpanded anymore
The ExpandableListView:
<ExpandableListView
android:id="#+id/normalList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:groupIndicator="#drawable/toggle_button_selector" >
</ExpandableListView>
The xml of the group item:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/filesListDrawerLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp" >
<ImageView
android:id="#+id/img"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:contentDescription="#string/icondescription" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="29dp"
android:layout_alignTop="#+id/img"
android:layout_marginLeft="15dp"
android:layout_toRightOf="#+id/img"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black" />
<TextView
android:id="#+id/counter"
android:layout_width="wrap_content"
android:layout_height="19dp"
android:layout_alignBottom="#+id/img"
android:layout_below="#+id/text"
android:layout_marginLeft="15dp"
android:layout_toRightOf="#+id/img"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/black" />
<FrameLayout
android:id="#+id/frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/text"
android:layout_marginTop="-15dp"
android:clickable="true"
android:paddingTop="10dp"
android:paddingRight="15dp"
android:paddingLeft="10dp" >
<ToggleButton
android:id="#+id/toggle"
android:layout_width="25dp"
android:layout_height="25dp"
android:background="#drawable/toggle_button_selector"
android:clickable="false"
android:duplicateParentState="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:textColorLink="#android:color/transparent"
android:textOff=""
android:textOn="" />
</FrameLayout>
</RelativeLayout>
The xml of the child item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/details"
android:layout_width="0dp"
android:layout_height="57dip"
android:layout_weight="1"
android:clickable="true"
android:drawableTop="#drawable/ic_action_about"
android:drawablePadding="-12dp"
android:background="#drawable/toolbar_selector"
android:gravity="center"
android:text="#string/details" />
<TextView
android:id="#+id/send"
android:layout_width="0dp"
android:layout_height="57dip"
android:layout_weight="1"
android:clickable="true"
android:drawableTop="#drawable/ic_action_new_email"
android:drawablePadding="-12dp"
android:background="#drawable/toolbar_selector"
android:gravity="center"
android:text="#string/send" />
<!-- this one is new -->
<TextView
android:id="#+id/other"
android:layout_width="0dp"
android:layout_height="57dip"
android:layout_weight="1"
android:background="#drawable/toolbar_selector"
android:clickable="true"
android:drawablePadding="-12dp"
android:drawableTop="#drawable/ic_action_other"
android:gravity="center"
android:text="#string/other" />
</LinearLayout>
There. I'm happy now.

listview onItemClick not getting called while BaseAdapter

its been a hour im trying to figure out the issue. Im implementing my own Adapter,The onClick event of the listview is not getting called
THe custom layout xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="4px"
android:layout_marginRight="10px" >
</CheckBox>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="23dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/imageView1"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="CodeLearn Chapter 1"
android:textSize="16sp" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView1"
android:layout_alignLeft="#+id/textView1"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Description" />
</RelativeLayout>
The main layout xml
<?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="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/tvUserName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
/>
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Custom Adapter code
public class AssignmentAdapter extends BaseAdapter
{
public View getView(int rowNumber, View convertView, ViewGroup parent)
{
View view = null;
ViewHolder viewHolder;
final int row = rowNumber;
if (convertView == null)
{
LayoutInflater minflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
// convertView = minflater.inflate(R.layout.listitem, parent,
// false);
view = minflater.inflate(R.layout.listitem, null);
viewHolder = new ViewHolder();
viewHolder.courseTitle = (TextView) view.findViewById(R.id.textView1);
viewHolder.assignmentTitle = (TextView) view.findViewById(R.id.textView2);
viewHolder.chkBox = (CheckBox) view.findViewById(R.id.check);
// chapterDesc.setText(myAssign.assignmentTitle);
viewHolder.chkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
// Model element = (Model) viewHolder.checkbox
// .getTag();
// element.setSelected(buttonView.isChecked());
if (isChecked)
{
Log.d(tag, "i got checked"+ row);
}
}
});
view.setTag(viewHolder);
} else
{
view = convertView;
viewHolder = (ViewHolder) convertView.getTag();
}
ViewHolder holder = (ViewHolder) view.getTag();
Assignment myAssign = listAssignment.get(rowNumber);
holder.courseTitle.setText(myAssign.courseTitle);
holder.assignmentTitle.setText(myAssign.assignmentTitle);
return view;
}
The MainActivity
public class MainActivity extends Activity
{
AssignmentAdapter assignmentAdaper;
Context context = MainActivity.this;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.home_page);
assignmentAdaper = new AssignmentAdapter(this);
ListView assignmentLists = (ListView) findViewById(R.id.listView1);
assignmentLists.setAdapter(assignmentAdaper);
Assignment myAssignment = new Assignment();
assignmentLists.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
Log.d("MainActivity", "wewew");
}
});
Since, I have checkbox in the listview layout. I need to disable the focus attribute of the checkbox
<CheckBox
android:id="#+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="4px"
android:layout_marginRight="10px"
android:focusable="false"
android:focusableInTouchMode="false" >
</CheckBox>

Categories

Resources