I have a ListView with Custom Adapter. I have seen this thread where people asked if the items in the custom view had a clickable item. And Yes, I have a clickable ImageView in the listrow. So clicking anywhere else(other than that ImageView) should perform some other action. I gave an onItemClickListener to the ListView. However, it doesn't work on first click and works on two-three clicks.
Update:
In my adapter's getView method, I set onClick of ImageView like this:
holder.chatImageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Stuff here
}
});
It works fine, and in my activity, I gave onItemClickListener to listView likw this:
onlineListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView parent, View view,
int position, long id) {
//Stuff here
}
});
PS: I didn't explicitly give any focus to anything.
Some time ago I've faced the same problem but with a CheckBox, I've resolve it by creating a onClickListener as member of the Custom Adapter. For example
public MyAdapter extends BaseAdapter{
// ... ..
// ... ...
private OnClickListener imageClickListener = new OnClickListener()
{
#Override
public void onClick(View v)
{
/// your data .getTag()
// process onClickListener for image
}
};
}
And then, in your getView:
if (convertView == null){
//Create your views and register onClickListener
holder.chatImageView.setOnClickListener(imageClickListener);
}
Add android:focusable="false" to your image in xml.
Hope it helps, Here you have the entire example:
Related
I have recyclerview inside an activity. I have some View inside each item. Suppose a textview, I am implementing a click listener on that particular view.
However, there is a case where i have to call the clicklistener of a particular position of that textview.
Since there is no function to call the onclick where i can pass the position.
I thought holder.itemview.callonclick() would work. But it is not working.
is there any way to call the itemview's textview of a particular position of a recyclerview.
You can create custom click listener using interface like below.
public interface ClickInterface {
public void recyclerviewOnItemClick(int position);
}
make sure you implement this interface to class where you want your code to be executed and implement it's method.Then you can pass this listerer's refrence to adapter like this.
TestAdapter adapetr = new TestAdapter (this);
listview.setAdapter(adapter);
In adapter :
private ClickInterface clickInterface;
public TestAdapter (ClickInterface clickInterface) {
this.clickInterface = clickInterface;
}
viewHolder.txtview.setOnClickListener(v ->
clickInterface.recyclerviewOnItemClick(position));
-You will get implemented method call when textview is being clicked.
You can try like this in Holder class,
mTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (getAdapterPosition()== YOUR_POS)
{
code..
}
}
});
or in onBindViewHolder ,`
if(i==YOUR_POS){ holder.mTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
code..
}
});}
I am implementing an app that display list of data on a RecycleView. After swipe a recycleview item it display a specific LinearLayout view. In that view I have implemented an onClickListner. But it's not calling on click.
You must create an OnItemClickListener() interface.
Then, put this in your onBindViewHolder method:
holder.bind(items.get(position), listener);
Next time, put the bind method inside of your Adapter:
itemView.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
listener.onItemClick(item);
}
});
And finally, in your MainActivity inside of your adapter declaration open the listener like this:
recycler.setAdapter(new ContentAdapter(items, new ContentAdapter.OnItemClickListener() {
#Override public void onItemClick(ContentItem item) {
Toast.makeText(getContext(), "Item Clicked", Toast.LENGTH_LONG).show();
}
}));
If you want to read more check out this documentation
I'm new in Android development and I don't understand something, even if is a simple thing: I have a ListView that each item contains a textview and a button. When clicked on a button it fires setOnClickListener inside getView method. Also, I have a setOnItemClickListener adapter on this ListView but that method does not fire when the label is clicked. Why is this happening?
UPDATE
This is how the button click fires:
ImageButton btnToMap = view.findViewById(R.id.btnToMap);
btnToMap.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Coordinates coordinate = MainActivity.coordinates.get(position);
Toast.makeText(getApplicationContext(), "coordonata apasata: " + coordinate.latitude + ";"
+ coordinate.longitude, Toast.LENGTH_SHORT).show();
}
});
This is kind of View focus problem everyone newbie suffers from ;)
Simple solution:
Make all the children of your row_layout to
focusable=false, focusableInTouchMode = false and add android:descendantFocusability="blocksDescendants" to your parent.
Use focusable or requestfocus() as per your requirement.
In your xml file where you defined your button add:
android:focusable="false"
You can override onClick method in your Adapter class and check which view was clicked.
#Override
public void onClick(View v) {
if (v.getId() == R.id.button)
mButtonListener.buttonOnClick();
else
mItemListener.itemOnClick();
}
mButtonListener and mItemListener are listeners passed to Adapter in constructor.
private final OnButtonListener mButtonListener;
private final OnItemListener mItemListener;
public MyAdapter(OnButtonListener buttonListener, OnItemListener itemListener) {
mButtonListener = buttonListener;
mItemListener = itemListener;
}
public interface OnButtonListener {
void buttonOnClick();
}
public interface OnItemListener {
void itemOnClick();
}
I used this in my Inventory App you can find source code here
I have populated a list view using the custom cursor adapter, That listview consists of a checkbox and button in the below manner.
checkbox1 button1
checkbox2 button2
.
.
.
Now I want to navigate to another activity when click on the button in list view now my issue here is when I click on the button1 then I want to see the data in checkbox1 and similarly for other rows as well.
Now I am confused on how to maintain a sync between the checkbox and button and how to achieve the click functionality. How can I achieve this?
You can do the following on the Button's onClickListener if the Button and CheckBox share the same parent
boldButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View view)
{
View parentView = (View) view.getParent();
CheckBox checkBox = (CheckBox) parentView.findViewById(R.id.check);
}
});
This will give you the CheckBox corresponding to the clicked Button
You will need to find your corresponding view in the list view implementing the onclickListener
For eg
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Button b = (Button)v.findViewById(R.id.button);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(this,SecondActivity.Class)
startActivity(i);
}
});
//Similarly for the checkbox
}
});
Use a ViewHolder class in adapter class and use that in getView(), Then within getView
viewHolder.yourButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(viewHolder.checkbox.isChecked())
// toggle the check and do your work here
}
});
You can learn ViewHolder pattern here its very simple
Another example link
This Selectable ListView Tutorial link should help in your solution.
Check this post might help.
You have to preserve the state of row in listview so row can be populated with its state.
Android how to identify item in listview with checkbox
I’ve a custom ListView containing 4 TextViews. Now, the TextViews have to be Linkifiable. Since Linkify wasn’t working in ListView, I made a callIntent function to see if the link is clickable or not. But if there’s no clickable link, I want to start a new Activity. I use the following code:
lvMembersList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
final TextView txtLine1 = (TextView) arg1.findViewById(R.id.tv_member_details_line1);
final TextView txtLine2 = (TextView) arg1.findViewById(R.id.tv_member_details_line2);
final TextView txtLine3 = (TextView) arg1.findViewById(R.id.tv_member_details_line3);
final TextView txtLine4 = (TextView) arg1.findViewById(R.id.tv_member_details_line4);
txtLine1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
callIntent(1);
}
});
txtLine2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
callIntent(2);
}
});
txtLine3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
callIntent(3);
}
});
txtLine4.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
callIntent(4);
}
});
}}
Now, when I click on an item for the first time, the TextView listeners are getting set. The individual clicks only works after the second time. What should I do in such a case?
Put this code in the getView() method in the adapter. And in your code onItemClick is called when you click on a row of the listview.
Add the following attribute for each of the textviews(The four textviews in the layout which going to use as the custom list item)
android:onClick="onTextView1_click"
- You have to do this for all the 4 textviews to track the click event seperately-
Then in the code add the following method to trigger when you click the textview1
public void onTextView1_click(View view)
{
final int position = (Integer) view.getTag();
//implementation (Which needs to be done when someone click textview1)
}
Also most importantly You have to add the position as a tag from the getViewMethod in adapter class. this will help to track the clicked item in the listview
holder.textView1.setTag(position);
and you can access this value as I have indicated in the "onTextView1_click(View view)" method "Position variable"
In the List view --->
android:focusable="false"
In the code add the "setItemsCanFocus(true)" soon after initialize list view. This indicates focusable items contains within each item in list view.
lvMembersList = (ListView) findViewById(R.id.NameOfListTView);
lvMembersList.setItemsCanFocus(true); // <---------[X]