【发布时间】:2018-02-05 14:38:56
【问题描述】:
我正在制作一个具有三个选项卡的应用程序(类似于 WhatsApp 布局)。这三个选项卡是朋友、家人和联系人。每个选项卡中都有一个列表。每个列表项还具有以下视图:左侧的名称、名称下方的描述和右侧的具有可绘制背景的按钮,与名称在同一行。 (我之前已经发布过,但现在重新发布了,因为我已经能够找出主要错误并减少我需要放在这里的代码)
每个列表视图项都包含一个类(朋友、家人、联系人)。这三个类都有一个标志(canViewYouOnlineFlag)。如果此标志设置为 true,则相应的联系人、家人或朋友将知道您在线,反之亦然。如果 flag 设置为 true,则按钮的颜色必须为蓝色,如果 flag 设置为 false,则按钮的颜色必须为黑色。
我面临的问题是打开选项卡时按钮无法正确呈现。通常,必须根据 Listview 项中包含的 Object 的 CanViewYouOnlineFlag 设置按钮的颜色。但是,当在下一个列表视图项上调用 getView 函数时,当前列表视图项的按钮颜色会被下一个列表视图项的对象的 CanViewYouOnlineFlag 更改。例如:
listview 中的 3 个项目,每个项目都有一个对象,这三个项目的 CanViewYouOnlineflags 如下:Listview item 1:true,Listview item 2:false,Listview item 3:false。
列表视图项的按钮颜色在渲染时必须是蓝色、黑色、黑色。然而,事实证明,一切都是黑色的。
同样,如果 listview item 3 的对象的标志设置为“TRUE”,那么所有按钮都是蓝色的。基本上,按钮的颜色由最后一个 Listview 项目中存在的对象决定。列表视图项中包含的对象的实际标志不是问题,我也验证了这一点(即对象标志为真,但按钮仍呈现黑色,因为最后一个列表视图项的按钮为黑色)。请帮我解决这个问题。 以下是main_lp_view_item布局的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:id="@+id/main_lp_text"
android:textSize="18sp"
android:textColor="@color/colorPrimaryDark"
android:focusable="false" />
<TextView
android:id="@+id/main_lp_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/main_lp_text"
android:textSize="13sp"
android:focusable="false" />
<Button
android:id="@+id/can_view_you_online"
android:layout_width="33sp"
android:layout_height="29sp"
android:layout_above="@+id/main_lp_subtitle"
android:layout_marginTop="10dp"
android:textStyle="bold"
android:background="@drawable/lp_list_button"
android:layout_alignParentEnd="true"
android:focusable="false"
/>
</RelativeLayout>
下面是启动Listviews的LPListAdapter类的代码
public class LPListItemAdapter<T> extends BaseAdapter{ //Class for rendering each ListItem
Context context;
List<T> rowItems;
public LPListItemAdapter(Context context, List<T> rowItems)
{
this.context=context;
this.rowItems=rowItems;
}
@Override
public int getCount()
{
return rowItems.size();
}
@Override
public Object getItem(int position)
{
return rowItems.get(position);
}
@Override
public long getItemId(int position)
{
return rowItems.indexOf(getItem(position));
}
private class ViewHolder{
TextView main_text; //Display Name
TextView subtitle; //Display Description
Button can_view_you_online; //Button to set and display status of CanViewYouOnline flag of the class
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder=null;
LayoutInflater mInflater= (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
holder = new ViewHolder();
if(convertView==null)
{
convertView=mInflater.inflate(R.layout.main_lp_view_item, null);
holder.main_text= (TextView) convertView.findViewById(R.id.main_lp_text);
holder.subtitle= (TextView) convertView.findViewById(R.id.main_lp_subtitle);
holder.canViewYouOnline= (Button) convertView.findViewById(R.id.can_view_you_online);
convertView.setTag(holder);
}
else{
holder = (ViewHolder) convertView.getTag();
}
final T rowItem= rowItems.get(position);
String main_text;
String subtitle;
if(rowItem instanceof Friends) //If Row Item is a Friend class
{
main_text= ((Friends) rowItem).getName();
subtitle= ((Friends) rowItem).getDescription();
}
else if(rowItem instanceof Family) //If Row Item is a Family class
{
main_text= ((Family) rowItem).getName();
subtitle= ((Family) rowItem).getDescription();
}
else if(rowItem instanceof Contact) //If Row Item is a Contact class
{
main_text=((Contact) rowItem).getName();
subtitle=((Contact) rowItem).getDescription();
}
else
{
main_text="NA";
subtitle="";
}
holder.main_text.setText(main_text);
holder.subtitle.setText(subtitle);
if(getflagstatus(rowItem)) //If rowItem's canViewYouOnline flag is set true, then let color of button be default (i.e. blue)
holder.canViewYouOnline.getBackground().clearColorFilter();
else //Else (if it false), make it black
holder.canViewYouOnline.getBackground().setColorFilter(Color.argb(255, 0, 0, 0), PorterDuff.Mode.SRC_ATOP);
//This part of the function is the problem because the color filter of the drawable of the button in the current list item gets overriden when
//this same code is executed for the next listview item.
holder.canViewYouOnline.setOnClickListener(new View.OnClickListener() {
boolean buttonClickFlag;
@Override
public void onClick(View v) { //The Onclick function allows one to click the button on the list item and set/reset the canViewYouOnline flag. It is working fine.
buttonClickFlag=getflagstatus(rowItem); //get buttonClickFlag from canViewYouOnline flag status
if(buttonClickFlag==false) //If buttonClickFlag is false
{
v.getBackground().clearColorFilter(); //set color of button drawable to default (which is blue)
buttonClickFlag=true; //set buttonClickflag to true
}
else //if buttonClickFlag is true
{
v.getBackground().setColorFilter(Color.argb(255,0,0,0), PorterDuff.Mode.SRC_ATOP); //set color of button drawable to black
buttonClickFlag=false; //set buttonClickflag to false
}
//update the canViewYouOnline flag in the rowItem by calling the onlineStatusUpdateButtonOnClickActivity function.
//Function is working perfectly so is not included in the stack overflow entry due to space constraints
onlineStatusUpdateButtonOnClickActivity(rowItem, buttonClickFlag,v);
}
});
return convertView;
}
public boolean getflagstatus(T rowItem)
{
//Function to get the CanViewYouOnlineFlag() from the underlying class in the rowItem
boolean flagStatus=false;
if(rowItem instanceof Friends)
flagStatus=((Friends) rowItem).getCanViewYouOnlineFlag();
else if(rowItem instanceof Family)
flagStatus=((Family) rowItem).getCanViewYouOnlineFlag();
else if(rowItem instanceof Contact)
flagStatus= ((Contact) rowItem).getCanViewYouOnlineFlag();
return flagStatus;
}
public void onlineStatusUpdateButtonOnClickActivity(T rowItem, boolean mLBflag, View convertView) {//WORKS FINE SO NOT PUTTING IT HERE DUE TO SPACE CONSTRAINT}
}
【问题讨论】:
标签: android android-layout listview android-drawable android-button