2015-07-11 52 views
-1

我發展,我現在用ListView現在我想做一個應用程序是我想補充一個Button在每隔列表項如何在listview中添加備用listitem中的按鈕?

像這樣的事情

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(); 
    } 
} 

回答

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); 
       } 
+0

我可以問爲什麼? – Aditya

+0

它工作與否?你有檢查嗎? –

+0

是它的工作 – Aditya

1

您可以添加按鈕,ListView的項目佈局和設置適配器getView方法的可見性。您需要爲這種類型的ListView定製適配器。

你需要把下面的代碼行

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

return語句之前。

+0

告訴我http://stackoverflow.com/questions/31847086/how-to-attach-jpg-or-png-file-to-gmail-or-facebook – Aditya

2

position % 1總是 0.你應該使用position % 2 == 1來得到你描述的結果。

Java中的'%'表示模操作。模操作返回不能分割的整數部分。例如,如果您除以5,10你得到2 0的休息但當你會除以5,13,你會得到2的3休息,因此12%5將返回3

+0

not working..it shows..buton listitem1 then listitem2然後listite3..so在 – Aditya

+0

您的答案幫助我很多..我投了 – Aditya

+0

告訴我http://stackoverflow.com/questions/31847086/how-to-attach-jpg-or-png-file-to-gmail -or-facebook – Aditya

相關問題