Catching onClick events in child view of a list item - android

How can I catch onClick events in multiple child views of a list item in a ListFragment with custom Adapter?
I want the onClick event to return the item id and the child view id.
My list item layout is.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:id="#+id/event_status" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="7dp"
android:nestedScrollingEnabled="false">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/event_members" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/event_date"/>
</LinearLayout>
</LinearLayout>
I want to catch clicks on event_status and event_members separately.

I personnaly use android left side menu, which manages different fragments by clicking on a custom adapter
https://developer.android.com/training/implementing-navigation/nav-drawer.html
I think the interesting part for you is
myList.setOnItemClickListener(new myItemClickListener());
then
private class myItemClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// your code
}
}
Hope this can help

You can do it like this
private class YourAdapter extends ArrayAdapter<...>
{
TextView event_date = (TextView) convertView.findViewById(R.id.event_date);
event_date.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
TextView event_members = (TextView) convertView.findViewById(R.id.event_members );
event_date.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
What might also work, but I think there is no benefit in doing it like this and seems not clean:
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
// the same two handler here
TextView event_members = (TextView) convertView.findViewById(R.id.event_members );
event_date.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}

Related

How to remove items (randomly) from a list and maintaining the ListView

There is a ListView which displays the items of an Arraylist, which are dynamically added by the user (via an EditText and a confirm button). In each item of the Listview, there is a remove ImageView, which removes the respective item from the List (and hence from the ListView).
The goal is to make the remove button (ImageView) on the ListView work correctly.
Currently I am adding an onClickListener to each of the items, when they are added to the List. The onClickListener executes a method which uses the list.remove(int i) function and then notifyDataSetChanged() to update the ListView.
[[MAIN ACTIVITY]]
public class MainActivity extends AppCompatActivity {
// setting up member variables
ArrayList<String> list = new ArrayList<>();
ArrayAdapter<String> arrayAdapter;
TextInputLayout textInputLayout;
ListView listView;
Button addNewListItem;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
}
private void initialize() {
// binding listView, arrayadapter & button to the ones from the layout, setting up the array adapter
// Listview & adapted
listView = findViewById(R.id.listView_Items);
arrayAdapter = new ArrayAdapter<>(this, R.layout.listview_item, R.id.listView_item_text, list);
listView.setAdapter(arrayAdapter);
// buttons
addNewListItem = findViewById(R.id.button_addItem);
// setting up the onclick listener for addItem, de/increasePeople, calculate
addNewListItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addItem();
}
});
// setting up the onclick listener for the ok button in the soft keyboard for the
// edit text field when adding new items
textInputLayout = findViewById(R.id.TextInputLayout_itemToAdd);
textInputLayout.getEditText().setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
addItem();
return true;
}
return false;
}
});
}
private void addItem() {
textInputLayout = findViewById(R.id.TextInputLayout_itemToAdd);
String inputItem = textInputLayout.getEditText().getText().toString().trim();
// checking, if text was entered in the edit text and then adding it to the list
if (inputItem.isEmpty()) {
textInputLayout.setError("Can't be empty");
} else {
textInputLayout.setError(null);
list.add(inputItem);
// Adding an onClickListener to an item of the listview requires the listView to
// finish redrawing. OnLayoutChangeListener fires as soon as the listview has finished
// redrawing the listview.
// The layoutchangelistener is immediately removed and the onclicklistener is added.
listView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
#Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
listView.removeOnLayoutChangeListener(this);
// getting the child count of the listview to get the index of the item, that
// just has been added
int addedItemId = listView.getChildCount();
Toast.makeText(MainActivity.this,"addedItemid: " + addedItemId,Toast.LENGTH_SHORT).show();
View addedItemChildView = listView.getChildAt(addedItemId-1);
// initializing the imageview to which the onclicklistener has to be added
ImageView removeImage = addedItemChildView.findViewById(R.id.ImageView_removeItem);
// onclicklistener is an anonymous class and therefore requires
// the index to be final. initializing a final int with the index
final int itemId = addedItemId-1;
removeImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("Logging","itemId: " + itemId);
removeListItem(itemId);
}
});
}
});
arrayAdapter.notifyDataSetChanged();
}
}
private void removeListItem(int listItemId) {
list.remove(listItemId);
arrayAdapter.notifyDataSetChanged();
}
}
[[ACTIVITY_MAIN]]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/linearLayout_mainLayout"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.design.widget.TextInputLayout
android:id="#+id/TextInputLayout_itemToAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:errorEnabled="true">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/hint_enterItem"
android:inputType="text" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/button_addItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/button_addItem" />
<ListView
android:id="#+id/listView_Items"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
[[LISTVIEW_ITEM]]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp">
<ImageView
android:id="#+id/ImageView_removeItem"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="4"
android:clickable="true"
app:srcCompat="#android:drawable/ic_menu_delete" />
<TextView
android:id="#+id/listView_item_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_weight="2"
android:gravity="center_vertical"
tools:text="test" />
<android.support.design.widget.TextInputLayout
android:id="#+id/listView_item_edit"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_weight="3"
android:gravity="center"
app:errorEnabled="false">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Value"
android:inputType="numberDecimal" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
As you can see, when the onClickListener is being added to the ImageView the itemid is added in accordance with the current amount of items in the list (minus 1).
If we have 3 items: "i0", "i1" and "i2" and "i1" will be removed, the onClickListener with the remove method on "i2" has the old index. "i2"'s index has been automatically decreased in the ArrayList but the remove-function parameter remains the same.
How can this problem be solved?
Thank you :)
(i hope i didnt remove any essential code as i tried to leave only the important parts)
The views inside a ListView item can be access by override getView() of the standard adapter. Try this [Use setTag() and getTag()]:
arrayAdapter = new ArrayAdapter(this, R.layout.listview_item, R.id.listView_item_text, list){
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = super.getView(position, convertView, parent);
ImageView removeImage = itemView.findViewById(R.id.ImageView_removeItem);
removeImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int pos = (int)v.getTag();
Log.d("Logging","itemId: " + pos);
remove(pos);
notifyDataSetChanged();
}
});
removeImage.setTag(position);
return itemView;
}
};
Hope that helps!
Another answer [Use final]:
arrayAdapter = new ArrayAdapter(this, R.layout.listview_item, R.id.listView_item_text, list){
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View itemView = super.getView(position, convertView, parent);
ImageView removeImage = itemView.findViewById(R.id.ImageView_removeItem);
removeImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//int pos = (int)v.getTag();
Log.d("Logging","itemId: " + position);
remove(position);
notifyDataSetChanged();
}
});
//removeImage.setTag(position);
return itemView;
}
};

How to add Different OnClickListeners on views of ListView item to perform different actions

Are there any possibilities to check what view inside ListView Item was clicked?
In other words, when you click on different Views inside ListView item app should perform a different action.
In details, I have a simple Book.java class that contains some book description.
Then I create ListView<Book> using BooksAdapter.class:
public class BooksAdapter extends ArrayAdapter<Book> {
public BooksAdapter (Context context, List<Book> books) {
super(context,0,books);
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
// ConstraintLayout constraintLayout = new ConstraintLayout();
// constraintLayout.setVisibility();
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.books_list_item, parent, false);
}
Book currentBook = getItem(position);
ImageView coverView = (ImageView) listItemView.findViewById(R.id.preview_image_view);
if (currentBook.getImage() == "No cover") {
coverView.setImageResource(R.drawable.no_book_cover);
} else {
Picasso.get().load(currentBook.getImage()).into(coverView);
}
TextView authorTextView = (TextView)listItemView.findViewById(R.id.autor_text);
authorTextView.setText(formatAuthor(currentBook.getAuthor(),currentBook.getDate()));
TextView titleTextView = (TextView)listItemView.findViewById(R.id.title_text);
titleTextView.setText(currentBook.getTitle());
//TextView descrTextView = (TextView)listItemView.findViewById(R.id.description_text);
//descrTextView.setText(currentBook.getDescription());
return listItemView;
}
private String formatAuthor (String name,String date ) {
name = name.substring(2,name.length()-2);
date = date.substring(0,4);
String fullString = name + ", " + date;
return(fullString);
}
}
books_list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_item"
android:layout_width="match_parent"
android:layout_height="120dp"
android:orientation="horizontal"
android:paddingEnd="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingStart="16dp"
android:paddingBottom="16dp"
android:paddingTop="8dp">
<ImageView
android:id="#+id/preview_image_view"
android:layout_width="80dp"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/autor_text"
android:layout_width="match_parent"
android:layout_height="40dp"
android:maxLines="1"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="8dp"
android:textColor="#color/textColorPrimary"
android:textSize="16sp" />
<TextView
android:id="#+id/title_text"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_gravity="center_vertical"
android:maxLines="2"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="0dp"
android:textColor="#color/textColorLight"
android:textSize="16sp" />
</LinearLayout>
<ImageView
android:layout_width="40dp"
android:layout_height="match_parent"
android:src="#drawable/ic_info_black_24dp"
android:scaleType="center"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#android:color/darker_gray"/>
</LinearLayout>
In MainActivity.java create mAdapter and override setOnItemClickListener:
ListView booksListView = (ListView) findViewById(R.id.list);
mAdapter = new BooksAdapter(this, new ArrayList<Book>());
booksListView.setAdapter(mAdapter);
booksListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Book book = mAdapter.getItem(position);
ad = new AlertDialog.Builder(BooksListActivity.this);
ad.setTitle("Book description");
ad.setMessage(book.getDescription());
AlertDialog alert = ad.create();
alert.show();
//ad.create();
}
});
And here are some general question can we set onClickListeners on different Views inside Item (#+id/autor_text and #+id/title_text for example) and perform different actions in these cases?
I'm reading about this several places but doesn't find any helpful things.Thanks for any help.
Inside your getView() method set onClickListener to all your different Views and perform respective action.
By implementing this code:-
authorTextView.setOnClickListener(this);
titleTextView.setOnClickListener(this);
public void onClick(View v) {
switch(v.getId()){
case R.id.authorTextView:
//code to be written to handle the click event
break;
case R.id.titleTextView:
//code to be written to handle the click event
break;
}
}
};
Inside your getView
authorTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Do your action
}
});
titleTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Do your action
}
});

Android: Exit full screen and return to main activity

Main activity contains a spinner and a button. MainActivity.java code is as follows.
public class MainActivity extends AppCompatActivity implements LoadNew.VCallback {
public Spinner dropdown;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dropdown = (Spinner) findViewById(R.id.spinner1);
//Create a list of items for the spinner.
String[] items = new String[]{"select topic", "topic1", "topic2"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items);
dropdown.setAdapter(adapter);
dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// TODO Auto-generated method stub
String sp1 = String.valueOf(dropdown.getSelectedItem());
if (sp1.contentEquals("select topic")) {
loadFun("");
}
if (sp1.contentEquals("topic1")) {
loadFun("topic1");
}
if (sp1.contentEquals("topic2")) {
loadFun("topic2");
}
}
#Override
public void onNothingSelected(AdapterView<?> parentView){
// TODO Auto-generated method stub
}
});
Button x = (Button) findViewById(R.id.full_screen);
x.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loadFun("topic2", 0);
}
});
}
public void loadFun(String topicName, int i) {
LoadNew st = new LoadNew(this);
st.display(topicName, i);
}
}
The xml layout for main activity is as follows (activity_main.xml).
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/btn_dropdown"
android:spinnerMode="dropdown" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/full_screen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Full Screen"
style="?android:attr/borderlessButtonStyle"/>
</LinearLayout>
<LinearLayout
android:id="#+id/full_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
</RelativeLayout>
There is another java class LoadNew.java as follows.
public class LoadNew {
public interface VCallback {
//To access methods of MainActivity class
}
public Activity activity;
public LoadNew(Activity _activity) {
this.activity = _activity;
VCallback callerActivity = (VCallback) activity;
}
//Display PDF
public void display(String topicName, int i) {
LinearLayout fullScreen = (LinearLayout) this.activity.findViewById(R.id.full_view);
fullScreen.removeAllViews();//Clear field
LayoutInflater inflater = (LayoutInflater) this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.activity_load, null);
if(i ==0) {
this.activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.activity.setContentView(view);
}
TextView tv= (TextView) this.activity.findViewById(R.id.tv1);
tv.setText(topicName);
tv.setTextSize(100);
}
}
The activity_load.xml file is as follows.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout> xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/tv1"
android:layout_below="#+id/bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
When an item is selected in the spinner, the corresponding text is displayed in the textview. Upon pressing the button, the textview appears in full screen mode.
Now I want to return to the previous view (by pressing back button or by single tab on screen), i.e., the view before the button was clicked. How can I achieve this? Thanks in advance for the patience and the help.
Simplest would be to have the spinner and the non-fullscreen TextView on one activity and open a second activity with the fullscreen mode on button click.

add click listener to image button in Recycle view row

I using recycle view and i have in each row textview and image button.
In on bind i adding to the image button click listener but when i try to click on the image button it clicks on the row and react only when i long click on the image button.
Any suggestions?
#Override
public void onBindViewHolder(final FolderViewHolder holder, final int position) {
if (isFolder(position)) {
holder.folderName.setText(folders.get(position));
}else{
ImageButton edit = (ImageButton) holder.view.findViewById(R.id.browser_bookmark_row_edit);
edit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println(" ######### EDIT CLICKED");
mListener.switchToEditBookmarkFragmentFolderAdapter(bookmarks.get(position - folders.size()));
}
});
holder.folderName.setText(bookmarks.get(position - folders.size() ).getTitle());
}
}
public class FolderViewHolder extends RecyclerView.ViewHolder {
MuseoSansTextView folderName;
View view;
public FolderViewHolder(View itemView, MuseoSansTextView museoSansTextView) {
super(itemView);
view = itemView;
folderName = museoSansTextView; //(MuseoSansTextView) itemView.findViewById(R.id.browser_bookmark_folder_name);
}
}
<android.support.v7.widget.CardView
android:id="#+id/card_view_contact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
card_view:cardBackgroundColor="#color/transparent"
card_view:cardCornerRadius="#dimen/card_corner_radius"
card_view:cardUseCompatPadding="false"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/activity_horizontal_margin">
<com.osnewhorizon.pommmanager.ui.MuseoSansTextView
android:id="#+id/browser_bookmark_row_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/browser_bookmark_row_edit"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:singleLine="true"
android:textColor="#color/white"
android:text="test"
android:textSize="#dimen/contact_row_title_height" />
<ImageButton
android:id="#+id/browser_bookmark_row_edit"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="5dp"
android:src="#drawable/edit"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
First, you create an interface like :
public interface OnItemClickListener{
void onItemClick(View view, int position)
}
Declare on global :
private OnItemClickListener mOnItemClickListener;
Next, you need a contructor like :
public MyAdapter(Context context, List data, OnItemClickListener mOnItemClickListener){
//...
this.mOnItemClickListener= mOnItemClickListener;
}
And in onBindViewHolder
#Override
public void onBindViewHolder(final FolderViewHolder holder, final int position) {
//....
holder.image.setOnClickListener(new View.OnItemClickListener){
#Override
public void onClick(View v) {
mOnItemClickListener.onItemClick(v,position);
}
});
}
Finally, you can implement Callback in class use adapter like :
public class A extends AppCombatActivity implements MyAdapter.OnItemClickListener{
//exist code
#Override
onItemClick(View view, int position){
// Do something with view + position
}
//
}
P.s : With solution this, you can catch multi events child view in row :)
Why are you using imageButton , use imageView instead
Try adding android:clickable="true" in your xml layout which was inflated as a row.
I suggest that you should add android:descendantFocusability="blocksDescendants" to your root view in xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
...
android:descendantFocusability="blocksDescendants">
<Button
...
/>
</RelativeLayout>
Or you can use ImageView instead of ImageButton.
please find imageButton xml view in Constructor like and
Set your ImageButton click listener in FolderViewHolder Constructor below.
public FolderViewHolder(View itemView) {
super(itemView);
imageButton=(ImageButton)itemView.findViewById(R.id.browser_bookmark_row_edit)
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println(" ######### EDIT CLICKED");
mListener.switchToEditBookmarkFragmentFolderAdapter(bookmarks.get(getAdapterPosition()) - folders.size()));
}
});
}
Ok, found the problem. Because my recycle view have in addition touch listener in the fragment itself, he catches the click event.
In RecyclerView.OnItemTouchListener, onSingleTapUp() returned true. It should be false. So click event executed that will present in holder view in onBindViewHolder().

ListView selection problem with rating control?

friends,
i have created very simple custom listview adapter with ratingBar in it.
now i have noticed one thing i cannot rate those rating bars in listview because
when i click on listview that row particular row gets selected.
any one guide me how to select individual items in android listview?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/android:list"
/>
</LinearLayout>
and listview_item design
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="265dip"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/text1"
android:textSize="25dip"
android:text="This is text1"/>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/text2"
android:text="This is text2"/>
<RatingBar android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/ratingBarStyleSmall"
android:id="#+id/star"
android:numStars="10"
android:stepSize="0.1"
android:isIndicator="true"
/>
</LinearLayout>
</LinearLayout>
any help would be appreciated.
Dear UMAR
In order to implement the listeners to the each element to the listview(Single row) use Custom adapter and in the getview(....) method of the custom adapter ,implements the listeners which u want.. sample code `public class FindFriendsListAdapter extends BaseAdapter {
private ArrayList<SingleElementDetails> allElementDetails;
private LayoutInflater mInflater;
private Context context;
private String userid1;
private int usersno1;
private DBAdapter db;
public FindFriendsListAdapter(Context context, ArrayList<SingleElementDetails> results,String userid,int usersno) {
this.context=context;
this.userid1=userid;
this.usersno1=usersno;
allElementDetails = results;
mInflater = LayoutInflater.from(context);
db=new DBAdapter(context);
db.open();
}
public int getCount() {
return allElementDetails.size();
}
public Object getItem(int position) {
return allElementDetails.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent)
{
convertView = mInflater.inflate(R.layout.friendlisthelper, null);
ImageView imageview = (ImageView) convertView.findViewById(R.id.friendimageview);
TextView textview = (TextView) convertView.findViewById(R.id.friendtextview);
Button button=(Button)convertView.findViewById(R.id.friendbutton);
convertView.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
//do what u want
}
});
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// do what u want
}
});
return convertView;
}
}
`
android:isIndicator="false" in order to rate it. and listview selection will be gone automatically and control will be transferred to that rating control.
Try this
ListView lv_party;
lv_party.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, final int position,long id)
{
// TODO Auto-generated method stub
view.findViewById(R.id.btn_del).setOnClickListener(new View.OnClickListener(){
//your code
}

Categories

Resources