如何在listview中的备用listitem中添加按钮?

时间:2015-07-11 11:20:00

标签: android listview

我正在开发一个我正在使用ListView的应用程序现在正在尝试的是我想在每个备用列表项中添加一个Button

像这样的东西

listitem1
按钮 listitem2
listitem3
按钮 listitem4

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >



    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="180dp"
        android:id="@+id/id"
        android:background="@drawable/heads">
        <RelativeLayout
            android:layout_height="40dp"
            android:id="@+id/rl"
            android:layout_width="match_parent"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@android:color/white"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="5dp"
                android:padding="2dp"
                       android:text="abd"
                android:id="@+id/txt_allproductsname"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@android:color/white"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:layout_marginTop="5dp"
                android:padding="2dp"
                android:text="abddd"
                android:id="@+id/txt_allproductsquty"
                android:layout_alignParentRight="true"
                />
        </RelativeLayout>
    </RelativeLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btntest"
        android:text="test"
        />

</LinearLayout>

代码

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        holder = new ViewHolder();
        convertView = LayoutInflater.from(getActivity()).inflate(R.layout.list_item_homefrags, null);
        // holder.propic = (ImageView) convertView.findViewById(R.id.propicaccept);
        holder.txtproname = (TextView) convertView.findViewById(R.id.txt_allproductsquty);
        // holder.txtproid = (TextView) convertView.findViewById(R.id.txtproidacptedlist);
        holder.txtprofilecast = (TextView) convertView.findViewById(R.id.txt_allproductsname);

        holder.testbtn=(Button)convertView.findViewById(R.id.btntest);
        // holder.txtprofileage = (TextView) convertView.findViewById(R.id.txtprofileageacptedlist);
        // holder.txtprofileplace = (TextView) convertView.findViewById(R.id.txtprofileplaceacptedlist);


        if (position % 1 == 0) {
            holder.testbtn.setVisibility(View.VISIBLE);
        } else {
            holder.testbtn.setVisibility(View.GONE);
        }

        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }
}

3 个答案:

答案 0 :(得分:2)

position % 1 总是 0.您应该使用position % 2 == 1来获得您描述的结果。

Java中的'%'表示模运算。模运算返回无法除分的整数除法的分数。例如,如果你将10除以5得到2,其余为0.但是当你将13除以5时你会得到2而其余为3,因此12%5会返回3

答案 1 :(得分:1)

您可以向ListView的项目布局添加按钮,并设置适配器getView方法的可见性。您需要为此类ListView设置自定义适配器。

您需要输入以下代码行

if (position % 2 == 0) {
    holder.testbtn.setVisibility(View.VISIBLE);
} else {
    holder.testbtn.setVisibility(View.GONE);
}
return陈述之前

答案 2 :(得分:1)

尝试这样做

当您在

中编写显示/隐藏代码时
if(convertView==null){
 // only executes if convertview is null
}else{
  // get cached element
}

所以在else

之后移动显示/隐藏逻辑
@Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(getActivity()).inflate(R.layout.list_item_homefrags, null);
               // holder.propic = (ImageView) convertView.findViewById(R.id.propicaccept);
                holder.txtproname = (TextView) convertView.findViewById(R.id.txt_allproductsquty);
               // holder.txtproid = (TextView) convertView.findViewById(R.id.txtproidacptedlist);
                holder.txtprofilecast = (TextView) convertView.findViewById(R.id.txt_allproductsname);

                holder.testbtn=(Button)convertView.findViewById(R.id.btntest);
               // holder.txtprofileage = (TextView) convertView.findViewById(R.id.txtprofileageacptedlist);
               // holder.txtprofileplace = (TextView) convertView.findViewById(R.id.txtprofileplaceacptedlist);




                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }

                if (position % 2 == 0) {

                    holder.testbtn.setVisibility(View.VISIBLE);

                } else {
                    holder.testbtn.setVisibility(View.GONE);
                }