Here i have using following code. Now my grid showing the download button on long press.If i long press another item it shows the button.But the problem is that previous button is there.I want to hide the old .(Hide the old download button on new Long press on another grid item).
How can i do ? Please check it and give me a idea to implement this part ?
bookGrid.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
final int position, long id) {
downloadImg=(ImageView) view.findViewById(R.id.download);
shareImg=(ImageView) view.findViewById(R.id.share);
downloadImg.setVisibility(View.VISIBLE);
shareImg.setVisibility(View.VISIBLE);
}}
Thanx in advance.
You can remember previous long-clicked item:
private View previousItem;
bookGrid.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
final int position, long id) {
if (previousItem != null) {
downloadImg=(ImageView) previousItem.findViewById(R.id.download);
shareImg=(ImageView) previousItem.findViewById(R.id.share);
downloadImg.setVisibility(View.GONE);
shareImg.setVisibility(View.GONE);
}
downloadImg=(ImageView) view.findViewById(R.id.download);
shareImg=(ImageView) view.findViewById(R.id.share);
downloadImg.setVisibility(View.VISIBLE);
shareImg.setVisibility(View.VISIBLE);
previousItem = view;
}
}
This may help for same activity.
grid1.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
final int position, long id) {
downloadImg1=(ImageView) view.findViewById(R.id.download1);
shareImg1=(ImageView) view.findViewById(R.id.share1);
downloadImg1.setVisibility(View.VISIBLE);
shareImg1.setVisibility(View.VISIBLE);
downloadImg2=(ImageView) view.findViewById(R.id.download2);
shareImg2=(ImageView) view.findViewById(R.id.share2);
downloadImg2.setVisibility(View.GONE);
shareImg2.setVisibility(View.GONE);
}}
grid2.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
final int position, long id) {
downloadImg1=(ImageView) view.findViewById(R.id.download1);
shareImg1=(ImageView) view.findViewById(R.id.share1);
downloadImg1.setVisibility(View.GONE);
shareImg1.setVisibility(View.GONE);
downloadImg2=(ImageView) view.findViewById(R.id.download2);
shareImg2=(ImageView) view.findViewById(R.id.share2);
downloadImg2.setVisibility(View.VISIBLE);
shareImg2.setVisibility(View.VISIBLE);
}}
If you using custom adapter. You can create in your CustomAdapter method setButtonVisible(int position). And in the getView check this setted position and show or hide Button in view. Your method onClick kwill be something like this:
public boolean onItemLongClick(AdapterView<?> parent, View view,
final int position, long id) {
adapter.setButtonVisible(position);
adapter.notifyDataSetChanged();
}
You can:
Make a field or final variable with last button. in onItemLongClick check if it's not null and then make it invisible. On the end of onItemLongClick fill in the field variable with current button.
In your case (with provided code I suggest that downloading and shareImg are final variables common for all views that could be clicked):
bookGrid.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
final int position, long id) {
if (downloading != null) {
downloadImg.setVisibility(View.INVISIBLE);
}
if (shareImg != null) {
shareImg.setVisibility(View.INVISIBLE);
}
downloadImg=(ImageView) view.findViewById(R.id.download);
shareImg=(ImageView) view.findViewById(R.id.share);
downloadImg.setVisibility(View.VISIBLE);
shareImg.setVisibility(View.VISIBLE);
}}
Make a button disappear after some time. But in this case the variable should be the method scope. E.g. :
final ImageView downloadImg=(ImageView) view.findViewById(R.id.download);
shareImg=(ImageView) view.findViewById(R.id.share);
downloadImg.setVisibility(View.VISIBLE);
shareImg.setVisibility(View.VISIBLE);
downloadImg.postDelayed(new Runnable(){
#Override
public void run() {
downloadImg.setVisibility(View.INVISIBLE);
}
}, 1000);
Related
I don't want to call onItemClick when onItemLongClick is called.
I am using SlideAndDragListView and returning true only in onItemLongClick.
matchedUsersListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
InviteMatchedUsersFragment.this.onItemLongClick(parent,view,position,id);
return true;
}
});
#Override
public void onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
ImageView view1 = (ImageView) view.findViewById(position);
matchedPassengersAdapter.onUserClick(view1, position);
}
In OnItemClick I am doing it differently
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d(LOG_TAG, "onMatchedUserSelection()");
if(matchedPassengersAdapter.getSelectedPassengersCount() != 0)
{
ImageView view1 = (ImageView) view.findViewById(position);
matchedPassengersAdapter.onUserClick(view1, position);
}
else {
//onItemClickGoesHere
}
}
#Override
public void onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
ImageView view1 = (ImageView) view.findViewById(position);
matchedPassengersAdapter.onUserClick(view1, position);
}
This May helps You
Ref: Set long click listener for listview
Your question is very similar to this one, but it looks like it's not an exact duplicate.
What you've noticed is that the ListActivity class does not have a method override specifically for this case.
In order to add this functionality as a method override, your class should implement the AdapterView.OnItemLongClickListener interface, and then you can add the onItemLongClick() method override, which acts just as the onListItemClick() method override you already have, but responds to long clicks.
Just make sure that you follow instructions from this answer, you must use android:longClickable="true" in the layout xml, or call listview.setLongClickable(true);
Ex
public class MainActivity extends ListActivity implements AdapterView.OnItemLongClickListener {
ListView listview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listview = (ListView) findViewById(R.id.list);
listview.setLongClickable(true);
}
#Override
public boolean onItemLongClick(AdapterView<?> l, View v,
final int position, long id) {
Toast.makeText(this, "long clicked pos: " + position, Toast.LENGTH_LONG).show();
return true;
}
protected void onListItemClick(ListView l, View v, final int position, long id) {
super.onListItemClick(l, v, position, id);
Toast.makeText(this, "short clicked pos: " + position, Toast.LENGTH_LONG).show();
}
//....................
I have a spinner named spinnerArray
hot can I get the selected item's ID of spinner by integer data type after click a button?
(when I have selected First one of spinner, I want to get integer 0,Second one,integer 1)
Use
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(final AdapterView<?> parent, final View view, final int position, final long id) {
// position is what you want i think
}
#Override
public void onNothingSelected(final AdapterView<?> parent) {
}
});
And to get the position when clicked on other views use spinner.getSelectedItemPosition();
You can do this in your button's onClick method:
mySpinner.getSelectedItemPosition() + 1;
getSelectedItemPosition() gives you the item position starting at 0, so you need to add 1.
i think you are looking for position in spinner
code sample
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView,
int position, long id) {
// your code here
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
Are you looking to see when the items on the spinner have been clicked? Then OnItemSelectedListener will allow you to override onItemClick(AdapterView parent, View view, int position, long id).
I have a question, Can we apply OnItemClickLisyener and OnLongItemClickListener on the same ListView?
I want to delete items of listview on long item click, listview is already handling ItemClickListener to view files.
What should I do now?
Can someOne explain how to do this?
Here is example
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//do your stuff
});
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent,
View view, int position, long id) {
//Do your stuff
}});
I have an app which use ListView,I have set onListItemClick event to view details about contact,and I want to implement onLongListItemClick to show a dialog and I don't know why it doesn't work,I mean nothing happens,I tried like this:
public void onListItemClick(ListView parent, View v, int position, long id)
{
//code
}
public void onLongListItemClick(ListView parent, View v, int position, long id)
{
showdialog();
}
Try like this :
listview.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Toast.makeText(getApplicationContext(), "Long Clicked : ", Toast.LENGTH_LONG).show();
return true;
}
});
You need to add return true; here otherwise it will transfer control to single click event listener once you release your touch.
Hope it helps you.
Thanks.
I have an Android application. In one of my Activities which is derived from ListActivity, I've implemented the OnItemLongClickListener. I want to enable a delete button within the relevant list item where the ListItem has been LongClicked. How can I do this?
OnItemLongClickListener listener = new OnItemLongClickListener(){
public boolean onItemLongClick(AdapterView<?> av, View v, int position, long id) {
Account a = null;
a = (Account) av.getItemAtPosition(position);
Toast.makeText(AccountActivity.this, "Long Clicked : " + a.getAccountName(), Toast.LENGTH_LONG).show();
//instead of the toast, I need to show/enable a button here...
}
};
getListView().setOnItemLongClickListener(listener);
.xml
<Button
android:id="#+id/imgdelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"/>
.java
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View view, int arg2, long arg3) {
view.findViewById(R.id.imgdelete).setVisibility(View.INVISIBLE);
return false;
}
});
in your get view method of adapter set unique id to your button
btn.setId(position);
then on your click listener
OnItemLongClickListener listener = new OnItemLongClickListener(){
public boolean onItemLongClick(AdapterView<?> av, View v, int position, long id) {
Button btn = (Button) v.findViewById(position);
btn.setEnabled(true);
}
};`
Suppose you had a Button inside ListView's row layout then you can make it visible true`
OnItemLongClickListener listener = new OnItemLongClickListener(){
public boolean onItemLongClick(AdapterView<?> av, View v, int position, long id) {
Account a = null;
a = (Account) av.getItemAtPosition(position);
v.findViewById(R.id.btnid).setVisiBility(View.VISIBLE);
}
};`
You can add boolean flag isDeleteVisible to Account with default false value.
Then in OnItemLongClickListener set it to true and call adapter.notifyDataSetChanged()
In adapter's getView check isDeleteVisible and show or hide delete button.