Explore the solution of ListView multiplexing in Android to cause layout confusion _android

Source: Internet
Author: User

First, what are the specific requirements:

As shown in the figure, this has the ABCD four option topics, when clicked on a option, if a is the correct answer, it becomes the pattern of the hook, if the wrong answer, then become the wrong pattern, here at the time when writing feel very simple, as long as the click of the time I click on the option and the correct answer is the same, Is the same as the picture to replace the correct style, if not the same as the wrong style, so I wrote the following code (only posted the core adapter in the code)

Package Com.fizzer.anbangproject_dahuo_test. 
Adapter; 
Import Android.annotation.TargetApi; 
Import Android.content.Context; 
Import android.graphics.drawable.Drawable; 
Import Android.os.Build; 
Import Android.text.TextUtils; 
Import Android.view.View; 
Import Android.view.ViewGroup; 
Import Android.widget.BaseAdapter; 
Import Android.widget.TextView; Import Com.fizzer.anbangproject_dahuo_test. 
Model.convertmodel; Import Com.fizzer.anbangproject_dahuo_test. 
R 
Import java.util.List; 
/** * Created by Fizzer on 2016/10/8.  * email:doraemonmqq@sina.com/public class Convertviewadapter extends Baseadapter {private list<convertmodel> 
List 
Private context Mcontext; 
Public Convertviewadapter (context context, list<convertmodel> List) {mcontext = context; 
This.list = list; 
@Override public int GetCount () {if (list = = null) {return 0; 
else {return list.size (); @Override public View getview (int position, View Convertview, ViewGroup parent) {VieWholder Mviewholder; 
if (Convertview = = null) {Convertview = View.inflate (mcontext, r.layout.view_upgradepartnet_topic_layout, NULL); 
Mviewholder = new Viewholder (); 
Mviewholder.tvtitle = (TextView) Convertview.findviewbyid (r.id.tvtitle); 
Mviewholder.tvselecta = (TextView) Convertview.findviewbyid (R.ID.TVSELECTA); 
MVIEWHOLDER.TVSELECTB = (TextView) Convertview.findviewbyid (R.ID.TVSELECTB); 
MVIEWHOLDER.TVSELECTC = (TextView) Convertview.findviewbyid (R.ID.TVSELECTC); 
MVIEWHOLDER.TVSELECTD = (TextView) Convertview.findviewbyid (R.ID.TVSELECTD); 
Convertview.settag (Mviewholder); 
else {Mviewholder = (Viewholder) convertview.gettag (); 
} Convertmodel module = list.get (position); 
MViewHolder.tvTitle.setText ("Q" + (position + 1) + ":" + module.title); 
MViewHolder.tvSelectA.setText (Module.optiona); 
MViewHolder.tvSelectB.setText (MODULE.OPTIONB); 
MViewHolder.tvSelectC.setText (Module.optionc); 
MViewHolder.tvSelectD.setText (Module.optiond); Initlistener (Mviewholder, Module.rightOption, position, module); 
return convertview; 
@Override public Object getitem (int position) {return null; 
@Override public long getitemid (int position) {return 0; } private void Initlistener (final Viewholder mviewholder, final String Select, Final int position, final Convertmodel MoD Ule) {MViewHolder.tvSelectA.setOnClickListener (new View.onclicklistener () {@Override public void OnClick (View v) {J 
Udgeselect (Mviewholder, Mviewholder.tvselecta, "A", select, position); 
} 
}); MViewHolder.tvSelectB.setOnClickListener (New View.onclicklistener () {@Override public void OnClick (View v) {Judgesele 
CT (Mviewholder, MVIEWHOLDER.TVSELECTB, "B", select, position); 
} 
}); MViewHolder.tvSelectC.setOnClickListener (New View.onclicklistener () {@Override public void OnClick (View v) {Judgesele 
CT (Mviewholder, MVIEWHOLDER.TVSELECTC, "C", select, position); 
} 
}); MViewHolder.tvSelectD.setOnClickListener (New View.onclicklistener () {@Override public void OnClick (View V) {Judgeselect (Mviewholder, MVIEWHOLDER.TVSELECTD, "D", select, position); 
} 
}); } private void Clearselectstate (Viewholder mviewholder) {mViewHolder.tvSelectA.setCompoundDrawables ( 
Getdrawableresource (R.DRAWABLE.IC_SELECT_A), NULL, NULL, NULL); 
MViewHolder.tvSelectB.setCompoundDrawables (Getdrawableresource (r.drawable.ic_select_b), NULL, NULL, NULL); 
MViewHolder.tvSelectC.setCompoundDrawables (Getdrawableresource (r.drawable.ic_select_c), NULL, NULL, NULL); 
MViewHolder.tvSelectD.setCompoundDrawables (Getdrawableresource (r.drawable.ic_select_d), NULL, NULL, NULL); } private void Judgeselect (Viewholder viewholder, TextView text, string Select, string rightselect, int position) {//clear 
Before the state clearselectstate (Viewholder); if (Select.equals (Rightselect)) {Text.setcompounddrawables (Getdrawableresource (r.drawable.ic_select_right), NULL, 
NULL, NULL); 
else {text.setcompounddrawables (Getdrawableresource (r.drawable.ic_select_error), NULL, NULL, NULL); }} @TargetApi (Build.versIon_codes. 
Lollipop) Private drawable getdrawableresource (int res) {drawable drawable = mcontext.getdrawable (res); 
Drawable.setbounds (0, 0, drawable.getminimumwidth (), Drawable.getminimumheight ()); 
return drawable; 
Class Viewholder {TextView tvtitle; 
TextView tvselecta; 
TextView TVSELECTB; 
TextView TVSELECTC; 
TextView TVSELECTD; } 
}

Finished writing this code full of confidence, feel no problem, but on the phone on a run, found that the problem, the effect is as follows:

Yes, because of the ListView layout reuse mechanism, the items that are not selected below also have options selected for reuse

In fact, the solution is very simple, that is, the selected entry and the corresponding model of the entry related to the specific, how to do it, the following careful analysis and analysis,

First, add a default field when you create the model, this field is the option you choose, of course, the initial value is not, in the getview of the layout of the time, to determine whether the field has a value, and the value of how much, if there is a value, to judge whether the value is correct or wrong, Replace the correct picture if it is correct, and if it is an error, replace it with the wrong picture, and if there are no values, display the original ABCD four initialization pictures, so the problem is solved.

The following is a complete code, in fact, the code is similar to the above, but in the model to add a number of the field to copy and judge

Package Com.fizzer.anbangproject_dahuo_test. 
Adapter; 
Import Android.annotation.TargetApi; 
Import Android.content.Context; 
Import android.graphics.drawable.Drawable; 
Import Android.os.Build; 
Import Android.text.TextUtils; 
Import Android.view.View; 
Import Android.view.ViewGroup; 
Import Android.widget.BaseAdapter; 
Import Android.widget.TextView; Import Com.fizzer.anbangproject_dahuo_test. 
Model.convertmodel; Import Com.fizzer.anbangproject_dahuo_test. 
R 
Import java.util.List; 
/** * Created by Fizzer on 2016/10/8.  * email:doraemonmqq@sina.com/public class Convertviewadapter extends Baseadapter {private list<convertmodel> 
List 
Private context Mcontext; 
Public Convertviewadapter (context context, list<convertmodel> List) {mcontext = context; 
This.list = list; 
@Override public int GetCount () {if (list = = null) {return 0; 
else {return list.size (); @Override public View getview (int position, View Convertview, ViewGroup parent) {VieWholder Mviewholder; 
if (Convertview = = null) {Convertview = View.inflate (mcontext, r.layout.view_upgradepartnet_topic_layout, NULL); 
Mviewholder = new Viewholder (); 
Mviewholder.tvtitle = (TextView) Convertview.findviewbyid (r.id.tvtitle); 
Mviewholder.tvselecta = (TextView) Convertview.findviewbyid (R.ID.TVSELECTA); 
MVIEWHOLDER.TVSELECTB = (TextView) Convertview.findviewbyid (R.ID.TVSELECTB); 
MVIEWHOLDER.TVSELECTC = (TextView) Convertview.findviewbyid (R.ID.TVSELECTC); 
MVIEWHOLDER.TVSELECTD = (TextView) Convertview.findviewbyid (R.ID.TVSELECTD); 
Convertview.settag (Mviewholder); 
else {Mviewholder = (Viewholder) convertview.gettag (); 
} Convertmodel module = list.get (position); 
MViewHolder.tvTitle.setText ("Q" + (position + 1) + ":" + module.title); 
MViewHolder.tvSelectA.setText (Module.optiona); 
MViewHolder.tvSelectB.setText (MODULE.OPTIONB); 
MViewHolder.tvSelectC.setText (Module.optionc); 
MViewHolder.tvSelectD.setText (Module.optiond); Initlistener (Mviewholder, Module.rightOption, position, module); <span style= "color: #cc0000;" 
>if (Textutils.isempty (Module.check)) {clearselectstate (Mviewholder); else {judgeselect (Mviewholder, Getchecktextview (Mviewholder, Module.check), Module.check, Module.rightoption, 
position); 
}</span> return convertview; 
@Override public Object getitem (int position) {return null; 
@Override public long getitemid (int position) {return 0; } private void Initlistener (final Viewholder mviewholder, final String Select, Final int position, final Convertmodel MoD Ule) {MViewHolder.tvSelectA.setOnClickListener (new View.onclicklistener () {@Override public void OnClick (View v) {& Lt;span style= "color: #cc0000;" 
>module.check = "A";</span> judgeselect (Mviewholder, Mviewholder.tvselecta, "a", select, position); 
} 
}); MViewHolder.tvSelectB.setOnClickListener (New View.onclicklistener () {@Override public void OnClick (View v) {<span style= "COLOR: #cc0000;" >module.check = "B"; &LT;/SPAN&GT 
Judgeselect (Mviewholder, MVIEWHOLDER.TVSELECTB, "B", select, position); 
} 
}); MViewHolder.tvSelectC.setOnClickListener (New View.onclicklistener () {@Override public void OnClick (View v) {<span style= "COLOR: #cc0000;" 
>module.check = "C"; 
</span> Judgeselect (Mviewholder, MVIEWHOLDER.TVSELECTC, "C", select, position); 
} 
}); MViewHolder.tvSelectD.setOnClickListener (New View.onclicklistener () {@Override public void OnClick (View v) {<span style= "COLOR: #cc0000;" 
>module.check = "D";</span> judgeselect (Mviewholder, MVIEWHOLDER.TVSELECTD, "D", select, position); 
} 
}); } private void Clearselectstate (Viewholder mviewholder) {mViewHolder.tvSelectA.setCompoundDrawables ( 
Getdrawableresource (R.DRAWABLE.IC_SELECT_A), NULL, NULL, NULL); 
MViewHolder.tvSelectB.setCompoundDrawables (Getdrawableresource (r.drawable.ic_select_b), NULL, NULL, NULL); 
MViewHolder.tvSelectC.setCompoundDrawables (Getdrawableresource (r.drawable.ic_select_c), NULL, NULL, NULL); MvieWHolder.tvSelectD.setCompoundDrawables (Getdrawableresource (r.drawable.ic_select_d), NULL, NULL, NULL); } private void Judgeselect (Viewholder viewholder, TextView text, string Select, string rightselect, int position) {//clear 
Before the state clearselectstate (Viewholder); if (Select.equals (Rightselect)) {Text.setcompounddrawables (Getdrawableresource (r.drawable.ic_select_right), NULL, 
NULL, NULL); 
else {text.setcompounddrawables (Getdrawableresource (r.drawable.ic_select_error), NULL, NULL, NULL); }} @TargetApi (Build.version_codes. 
Lollipop) Private drawable getdrawableresource (int res) {drawable drawable = mcontext.getdrawable (res); 
Drawable.setbounds (0, 0, drawable.getminimumwidth (), Drawable.getminimumheight ()); 
return drawable; } <span style= "color: #cc0000;" >private TextView Getchecktextview (Viewholder mviewholder, String rightselect) {if ("A". Equals (Rightselect)) {retur 
n Mviewholder.tvselecta; 
else if ("B". Equals (Rightselect)) {return MVIEWHOLDER.TVSELECTB; }else if ("C". Equals (Rightselect)) {return MVIEWHOLDER.TVSELECTC; 
else if ("D". Equals (Rightselect)) {return mviewholder.tvselectd; 
return null; 
}</span> class Viewholder {TextView tvtitle; 
TextView tvselecta; 
TextView TVSELECTB; 
TextView TVSELECTC; 
TextView TVSELECTD; } 
}

The red is the new code, plus these, the problem is solved, look at the solution after the operation of the code:

Summarize:

Finally, to sum up the solution to this problem:

The first is to add a checked field to the entity class that the shim corresponds to. In the GetView operation, according to the check field to do the appropriate action, such as the value, then set to the corresponding style, if there is no value, then set to no worthy style, of course, When the user clicks on, the field should be assigned in a timely manner, similar to a checkbox in the ListView can be solved by the same method.

The above is a small set to introduce you to explore the Android ListView reuse lead to the layout of the confusion of the solution, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.