Wednesday, April 9, 2014

Solving ah: ListView selected item to change the background color, the next roll that have been changed


For example, ListView on my phone screen can display a total of 15 lines, if I select the first line, then the background color of the first line is changed, but the first 16 rows have been changed, solving ah, I change the background code as follows:
private View oldLinearLayout;
listView.setOnItemClickListener (new AdapterView.OnItemClickListener () {
@ Override
public void onItemClick (AdapterView paramAdapterView,
View paramView, int paramInt, long paramLong) {

if (beforeLinearLayout! = null) {
oldLinearLayout.setBackgroundDrawable (null);
}

paramView.setBackgroundColor (Color.GREEN);
oldLinearLayout = paramView;
}
});

<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
getview time. . With a view object is re-used. . Add to judge the difference between what you can.
Reply:
cited a floor wyb2dan reply:
getview time. . With a view object is re-used. . Add to judge the difference between what you can.

How this judgment ah, solution
Reply:
Nobody knows yet
Reply:
reference to the third floor u010364932 reply:
Nobody knows yet


http://bbs.csdn.net/topics/390431893
Reply:
references, 4th Floor ConnectionUtils reply:
reference to the third floor u010364932 reply: Nobody knows yet

http://bbs.csdn.net/topics/390431893

That did not understand, my method does not work with you, above me wrong, if there is oldLinearLayout! = Null
Reply:
Record position and color contrast when taking the view position because this is the reason baseAdapter
Reply:
reference to the 6th floor shen332401890 reply:
recording position and color contrast position when taking the view that because of the reasons baseAdapter
< br />You can write code
Reply:
Probably thinking:

1. Saved choose the current selection, then notify adapter to refresh the view

2 and then to fill the current View
based on the current selection in getView
 
/ / Adapter in
public void setSelected (int selected) {
this.selected = selected;
notifyDataSetChanged ();
}


/ /
When OnItemClickadapter.setSelected (position);

Reply:
reference to the 8th floor Eyebrows_cs reply:
probably thinking:

1. Saved choose the current selection, then notify adapter to refresh the view

2 and then to fill the current View
based on the current selection in getView
Java code? 123456789 / / Adapter in the public void setSelected (int selected) {this.sel ......

this.selected what this is, ah, ah, I did not inherit Adapter, nor setSelected this method ah
This is what I wrote:
SimpleAdapter listItemAdapter = new SimpleAdapter (this, listdata, R.layout.list_salesck,
new String [] {"billdate", "state", "zhaiyao", "billname", "userfullname"},
new int [] {R.id.billdate, R.id.textView14, R.id.zhaiyao, R.id.billtype, R.id.userfullname});

listView.setAdapter (listItemAdapter);
listView.setOnItemClickListener (new AdapterView.OnItemClickListener () {
public void onItemClick (AdapterView paramAdapterView,
View paramView, int paramInt, long paramLong) {
if (oldLinearLayout! = null) {
oldLinearLayout.setBackgroundDrawable (null);
}
/ / Set the background color of a member of the Grey
paramView.setBackgroundColor (Color.GRAY);
nowClick = paramInt;
oldLinearLayout = paramView;
}
});

How I should add, ah, beg specific code

Reply:
reference to the 9th floor u010364932 reply:
reference to the 8th floor Eyebrows_cs reply: probably thinking:

1. Saved choose the current selection, then notify adapter to refresh the view

2 and then to fill the current View
based on the current selection in getView
Java code? 123456789 / / Adapter in the public void setSelected (int sele ......


 
/ / Adapter in
private int selected;


public void setSelected (int selected) {
this.selected = selected;
notifyDataSetChanged ();
}

/ / In getView determine whether the current position is selected in
if (position == selected) {
/ / Push background
} Else {
/ / Normal background
}


/ /
In the Activity page callback OnItemClickListeneradapter.setSelected (position);


Reply:
reference to the 10th floor Eyebrows_cs reply:
reference to the 9th floor u010364932 reply: Quote, 8th Floor Eyebrows_cs reply: probably thinking:

1. Saved choose the current selection, then notify adapter to refresh the view

2 and then to fill the current View
based on the current selection in getView
Java code? 123456789 / / Adapter in public voi ......

Where Adapter is, ah, ah, I do not have this class
Reply:
ListItem added directly to the background like a xml, the program to achieve too much trouble, the effect is not good.
Reply:
reference to the 12th floor ueryueryuery reply:
direct enough background to add a ListItem xml, the program to achieve too much trouble, the effect is not good.

There plus background, tap disappeared
Reply:
ListView misplaced
rolling processWith ViewHolder to solve
For example:
getView ......
{
ViewHolder holder;
if (null == convertView) {
convertView = LayoutInflater.from (RewardActivity.this). inflate (
R.layout.reward_cions_item, null);
convertView = inflater.inflate (R.layout.reward_cions_item, null);
holder = new ViewHolder ();
holder.time = (TextView) convertView.findViewById (R.id.time);
convertView.setTag (holder);
} Else {
holder = (ViewHolder) convertView.getTag ();
}
holder.time.setText ("11");
return convertView;
}

class ViewHolder
{
TextView time;
......
}
Reply:
reference to the 13th floor u010364932 reply:
reference to the 12th floor ueryueryuery reply: ListItem added directly to the background of an xml just fine, the program to achieve too much trouble, the effect is not good.
There plus background, tap disappear


Do you want to tap the background does not disappear, it would see the upstairs bar :)
Reply:
reference to the 14th floor leidetianshiyj reply:
ListView dislocation in the process of rolling the
With ViewHolder to solve
For example:
getView ......
{
ViewHolder holder;
if (null == convertView) {
convertView = LayoutInflater.from (RewardActivity.this). inflate (
R.layou ......


1 Use ViewHolder mode initialization function is to avoid duplication of View, of course, a more appropriate approach is to do if the currently selected
within ViewHolder
Judgment, do background replacement,

2. LZ for Adapter adapter class is not very understanding, it is recommended to go to first understand BaseAdapter under this category, the above code in the adapter,

BaseAdapter is a specific instance of a subclass of the class, you should go to achieve this Adapter instance of the class, as a data source to provide data to the layout

I posted some of my code you can refer to solve this problem

 
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ishow.funnymap.bean.Menu;
import com.ishow.funnymap.ui.R;

public class MenuGridViewAdapter extends BaseAdapter {

private Context context;
private List menuGridViewList;
private int selected;

public MenuGridViewAdapter (Context context, List menuGridViewList) {
this.context = context;
this.menuGridViewList = menuGridViewList;
}

/ / Set the current selection state
public void setSelected (int selected) {
this.selected = selected;
notifyDataSetChanged ();
}

@ Override
public int getCount () {
return menuGridViewList.size ();
}

@ Override
public Object getItem (int position) {
return menuGridViewList.get (position);
}

@ Override
public long getItemId (int position) {
return 0;
}

@ Override
public View getView (int position, View convertView, ViewGroup parent) {

ViewHolder viewHolder = null;
if (convertView == null) {
convertView = inflateView ();
}
viewHolder = (ViewHolder) convertView.getTag ();
viewHolder.init (convertView);
viewHolder.fillFromItem (position, selected == position);
return convertView;
}

private View inflateView () {
LayoutInflater inflater = (LayoutInflater) context
. GetSystemService (Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate (R.layout.switch_pull_down_menu_item, null);
view.setTag (new ViewHolder ());
return view;
}

class ViewHolder {
ImageView imageView;
TextView textView;

public void init (View view) {
imageView = (ImageView) view.findViewById (R.id.menu_ico);
textView = (TextView) view.findViewById (R.id.menu_description);
}

public void fillFromItem (int position, boolean isCurrent) {
if (isCurrent) {
imageView.setBackgroundResource (menuGridViewList.get (position)
. GetImageResNormal ());
} Else {
imageView.setBackgroundResource (menuGridViewList.get (position)
. GetImageResPressed ());
}
textView.setText (menuGridViewList.get (position) getTextName ().);
}

}
}


OnItemClick Event
 
menuGridView.setOnItemClickListener (new OnItemClickListener () {

@ Override
public void onItemClick (AdapterView parent, View view,
int position, long id) {
onMenuSelectedListener.onMenuSelected (menuGridViewAdapter
. GetItem (position));
menuGridViewAdapter.setSelected (position);
/ / Dismiss ();
}
});

Reply:
Eyebrows_cs code is very clear, to learn the

No comments:

Post a Comment