I am making an android gallery. I want to be able to extend it's basic behavior so the view that its centered to be most visible (it's alpha value to be 1.0) and the views that are from the left and to the right to have some minimum visibility which will increase if the user scrolls and positions a particular view in the center.
I am wondering if someone has done this before, or has an idea how I should try and do this.
I haven't done this, but did something similar utilizing data from the central item in the gallery. What I did was simply create my own adapter
YourAdapter extends BaseAdapter{...}
Now you can set a externel listener or have the adapter listen to the gallery
//listen for user events on Gallery
gallery.setOnItemSelectedListener(YourAdapter);
in the adapter create these functions
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
selectedItem = position;
}
public View getView(int position, View convertView, ViewGroup parent) {
//change according if selected or not
if(position == selectedItem)
//set the alpha for selected
else
//set the alpha for other items
}
Related
I have a ListView of items which when you click on will change its background color (and will keep the background color even after restarting the app). This is to indicate that the item has been read (the expanded view opens in VieWPager). My problem is, since I have been able to change the background color of ListView items (for those that have been clicked), when i long press to bring up the Context Menu, when selecting on an item, the background color of that item no longer greys out like it usually does. As a result, i cannot "see" what i am selecting in context mode. I really have no idea how to fix this problem. Please can someone advise?
The code that I used to change the color of background items in ListView:
public void onListItemClick(ListView l, View v, int postion, long id) {
Article a = ((toReadListAdapter) getListAdapter()).getItem(postion);
// Clicked on items are flagged.
a.setRead(true);
saveToReadList(toReadList);
..........
Custom adapter:
// Defining custom adapter
private class toReadListAdapter extends ArrayAdapter<Article> {
public toReadListAdapter(ArrayList<Article> listToRead) {
super(getActivity(), 0, listToRead);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getActivity().getLayoutInflater().inflate(R.layout.new_articlelistfragment, null);
}
Article en = getItem(position);
.....
//if the article has been clicked on, then read value is true. Background is set to green.
if(en.isRead()){
convertView.setBackgroundColor(Color.parseColor("#C8E6C9"));
}else{
convertView.setBackgroundColor((Color.parseColor("#ffffff")));
}
return convertView;
I am trying to figure out the solution for the following listed problem. I have a Listview generated using Simpleadapter. When I click on a row in the listview I want to make a Layout with an id colorful as visible. I am able to do this. But my problem starts here. When I click on another row say row number 5 the colorful layout is visible, but the layout is also visible for the previously clicked row too. What I want to do is make the layout colorful visible for only the clicked row (i.e it should be visible for only one row at a time i.e is currently clicked row and hidden for all the remaining rows) and the layout should get invisible for the previously clicked rows. I tried doing with viewholder but it doesn't help. My code snippet is below. Guide me step by step as I am very new to Android.
final BaseAdapter k=new SimpleAdapter(getActivity(),val,R.layout.mytaskdata,new String[]{"sname","heading","desc","id","path","receiver","sender"},new int[]{R.id.textView1,R.id.textView2,R.id.textView3,R.id.hide1,R.id.hide2,R.id.hide3,R.id.hide4})
{
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final View v = super.getView(position, convertView, parent);
TextView myname=(TextView)v.findViewById(R.id.textView1);
TextView mydes=(TextView)v.findViewById(R.id.textView2);
TextView mytopic=(TextView)v.findViewById(R.id.textView3);
ImageView edit=(ImageView)v.findViewById(R.id.ImageView03);
sent.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
RelativeLayout r=(RelativeLayout)arg1.findViewById(R.id.colorful);
// r.setVisibility(arg1.VISIBLE);
int temp=sent.getCheckedItemPosition();
Log.i("itemposition",""+temp);
Toast.makeText(getActivity(),"pos"+arg2+"hii"+positionThatSaysHi,1000).show();
if(arg2!=positionThatSaysHi)
{
r.setVisibility(arg1.VISIBLE);
positionThatSaysHi = arg2;
notifyDataSetChanged();
}
else
{
r.setVisibility(arg1.GONE);
notifyDataSetChanged();
}
});
I would suggest modifying the OnClickListener to just record the selected row, then call notifyDataSetChanged(). This will cause the ListView to redraw its items by calling the adapter. Therefore, you just need to check this value in getView() to determine whether the "colorful" view should be visible or not.
So the updated code would be something like:
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
final View v = super.getView(position, convertView, parent);
TextView myname=(TextView)v.findViewById(R.id.textView1);
TextView mydes=(TextView)v.findViewById(R.id.textView2);
TextView mytopic=(TextView)v.findViewById(R.id.textView3);
ImageView edit=(ImageView)v.findViewById(R.id.ImageView03);
RelativeLayout r = (RelativeLayout)v.findViewById(R.id.colorful)
r.setVisibility(position == positionThatSaysHi ? View.VISIBLE : View.GONE);
sent.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
positionThatSaysHi = arg2;
notifyDataSetChanged();
}
});
}
That way, you ensure that only one view will be highlighted (while also simplifying your code).
Take advantage of choice mode in ListView ( see setChoiceMode()), and set it to CHOICE_MODE_SINGLE.
Use the selection state to toggle the visibility of your colorful layout. If possible, the easiest way to do this is to have the colorful bits in a selector drawable, with selected=true. That way it will show automatically, and you don't have to worry about hiding and showing views.
I have got a Gridview in my app. I then added a OnItemLongClickListener to that GridView. Check out this code:
myGridView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
view.setBackgroundResource(R.drawable.image_border);
return true;
}
});
Now, all I want to do is set a border to an image in my gridview. The problem with my code is that it sets the border to an image but when I start scrolling up and down, suddenly an other image has got the border although it was never clicked.
How come that the border gets set to other images as well. Any idea how I can fix this?
Your Adapter class does this internally to reduce memory consumption and to efficiently use device resources.
In your Adapter class you should do
public View getView (int position, View convertView, ViewGroup parent){
// convertView is reused from previously scrolled out view to reduce memory consumption
if( convertView == null ){
//We must create a View:
convertView = inflater.inflate(R.layout.my_list_item, parent, false);
}
// Set border to the image if it is selected otherwise set it to default
// keep in mind when you add any condition here, else should also be addressed
return convertView;
}
I´m using SimpleAdapter in my ListActivity and I want when user select an item highlight this item. I tried extends SimpleAdapter and override getView() method:
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
if (position == mItemIndex) {
convertView.setSelected(true);
convertView.setPressed(true);
convertView.setBackgroundColor(Color.parseColor("#FF9912"));
}
return view;
}
but this solution don´t work properly. It set background color to more then one list row.
Can sameone help me?
That's because the convertView is being reused and you do not update the selected state for both cases (selected / not selected). You need to call setSelected(false) when it is not the item you want selected and reset the background color. Also, the call to setPressed is not needed.
You also should checkout the ColorStateList which will allow you to define the colors for various states. Then you could just use the built in support for single item selection.
So instead of (e.g.) opening a new Activity you want to highlight the selected list item by changing its colors if the user clicks on it?
In your ListActivity you need to override onListItemClick()
#Override
protected void onListItemClick(ListView list, View view, int position, long id) {
super.onListItemClick(list, view, position, id);
TextView tv = (TextView)view;
tv.setTextColor(Color.RED);
tv.setBackgroundColor(Color.BLUE);
}
This changes the color of the selected entry to red (text) and blue (background).
Thats the first step. But the first clicked entry stays this way, even if you click another one. So you need to change that (somehow).
Can someone explain this issue to me ?
I have a listview that holds more rows than the screen can show, so scrolling.
If I click on one item, I replace an icon that is part of each row. That all works.
The issue I have is that when I click on lets say the first item, I change the icon for that first row. When I now scroll down I see that the first row outside the visible viewport also changed the icon.
Why is that happening and how can I avoid this issue ?
Thanks in advance,
Mozzak
Just to make sure, you are using a class that implements ListAdapter or extends some other sort of adapter right?
When using an adapter, you will have to keep in mind that the views in the ListView are recycled to save memory. Because of this, you will need to store the state in a separate variable.
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
{
LayoutInflater inflator = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflator.inflate(R.layout.listitem, null);
}
// Retreive my image that may or may not change
ImageView myIcon = (ImageView) convertView.findViewById(R.id.iconView);
// Checking my stored boolean for this position to see if I need to use icon2 or icon
if (myItem[position].needsIconChanged)
{
// I have set my boolean, so use icon2
myIcon.setImageResource(R.drawable.icon2);
}
else
{
// I have not set my boolean, or set it to false so set it to icon
myIcon.setImageResource(R.drawable.icon);
}
return convertView;
}
You will also have to remember to set that boolean in your onItemCLick
public void onItemClick(AdapterView<?> myAdapter, View myView, int position, long arg3) {
// Retreive your item and set a boolean or icon state (depending on what you do)
myAdapter.getItemAtPosition(position).needsIconChanged = true;
}