3

我有一個完美工作的自定義列表視圖。 現在,我想爲列表中的每個項目提供一個下拉菜單,以便當用戶點擊列表中的任何項目時,下拉菜單顯示兩個選項 - 編輯和刪除。根據用戶的選擇,列表視圖項目被照顧。爲自定義列表中的每個項目添加下拉菜單

我想知道向每個listview項目添加下拉菜單的方法。對於名單

xml文件項 -

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" android:layout_height="120dp" 
android:background="#FFFFFF"> 
<CheckBox 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true" 
    android:id="@+id/chkitem" 
    android:layout_alignParentLeft="true" 
    /> 
<TextView android:id="@+id/textTitle" 
    android:layout_width="200dp" 
    android:layout_height="60dp" 
    android:textSize="15dp" 
    android:textColor="#000000" 
    android:textStyle="bold" 
    android:gravity="fill" 
    android:layout_toRightOf="@+id/chkitem" 
    /> 
<TextView 
    android:layout_width="200dp" 
    android:layout_height="60dp" 
    android:id="@+id/detail" 
    android:textSize="15dp" 
    android:textStyle="italic" 
    android:gravity="fill" 
    android:layout_toRightOf="@+id/chkitem" 
    android:layout_below="@+id/textTitle"/> 
    </RelativeLayout> 

適配器類

public class list_addr_adapter extends ArrayAdapter<list_addr> { 

Context context; 
int layoutResourceId; 



public list_addr_adapter(Context context, int layoutResourceId,   List<list_addr> items) { 
    super(context, layoutResourceId, items); 
    this.layoutResourceId = layoutResourceId; 
    this.context = context; 
    // this.listener=callback; 
} 

/*private view holder class*/ 
private class ViewHolder { 

    TextView txtTitle; 
    TextView txtDetail; 

} 
ViewHolder holder = null; 

public View getView(int position, View convertView, ViewGroup parent) { 
    final list_addr lists = getItem(position); 
    final int pos=position; 


    LayoutInflater mInflater = (LayoutInflater) context 
      .getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
    if (convertView == null) { 
     convertView = mInflater.inflate(R.layout.items_sav_addr2, null); 
     holder = new ViewHolder(); 
     holder.txtTitle = (TextView) convertView.findViewById(R.id.textTitle); 
     holder.txtDetail = (TextView) convertView.findViewById(R.id.detail); 

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






    holder.txtTitle.setText(lists.getTitle()); 
    holder.txtDetail.setText(lists.getDetail()); 

    return convertView; 
} 

    } 

我希望它是這樣的 -

enter image description here

回答

12

檢查:

XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="120dp" 
android:background="#FFFFFF"> 

<CheckBox 
    android:id="@+id/chkitem" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_centerVertical="true" /> 

<TextView 
    android:id="@+id/textTitle" 
    android:layout_width="200dp" 
    android:layout_height="60dp" 
    android:layout_toRightOf="@+id/chkitem" 
    android:gravity="fill" 
    android:textColor="#000000" 
    android:textSize="15dp" 
    android:textStyle="bold" /> 

<TextView 
    android:id="@+id/detail" 
    android:layout_width="200dp" 
    android:layout_height="60dp" 
    android:layout_below="@+id/textTitle" 
    android:layout_toRightOf="@+id/chkitem" 
    android:gravity="fill" 
    android:textSize="15dp" 
    android:textStyle="italic" /> 

<ImageView 
    android:id="@+id/imageview" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_centerInParent="true" 
    android:src="@mipmap/ic_launcher" />// Replace Image 

</RelativeLayout> 

適配器類別:

public class list_addr_adapter extends ArrayAdapter<list_addr> { 

Context context; 
int layoutResourceId; 



public list_addr_adapter(Context context, int layoutResourceId,   List<list_addr> items) { 
    super(context, layoutResourceId, items); 
    this.layoutResourceId = layoutResourceId; 
    this.context = context; 
    // this.listener=callback; 
} 

/*private view holder class*/ 
private class ViewHolder { 

    TextView txtTitle; 
    TextView txtDetail; 
    ImageView imageview; 

} 
ViewHolder holder = null; 

public View getView(int position, View convertView, ViewGroup parent) { 
    final list_addr lists = getItem(position); 
    final int pos=position; 


    LayoutInflater mInflater = (LayoutInflater) context 
      .getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
    if (convertView == null) { 
     convertView = mInflater.inflate(R.layout.items_sav_addr2, null); 
     holder = new ViewHolder(); 
     holder.txtTitle = (TextView) convertView.findViewById(R.id.textTitle); 
     holder.txtDetail = (TextView) convertView.findViewById(R.id.detail); 
     holder.imageview = (ImageView) convertView.findViewById(R.id.imageview); 

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

    holder.txtTitle.setText(lists.getTitle()); 
    holder.txtDetail.setText(lists.getDetail()); 

    try { 
     holder.imageview.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 


      switch (v.getId()) { 
       case R.id.imageview: 

        PopupMenu popup = new PopupMenu(getApplicationContext(), v); 
        popup.getMenuInflater().inflate(R.menu.clipboard_popup, 
          popup.getMenu()); 
        popup.show(); 
        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { 
         @Override 
         public boolean onMenuItemClick(MenuItem item) { 

          switch (item.getItemId()) { 
           case R.id.edit: 

            //Or Some other code you want to put here.. This is just an example. 
            Toast.makeText(getApplicationContext(), " Install Clicked at position " + " : " + position, Toast.LENGTH_LONG).show(); 

            break; 
           case R.id.delete: 

            Toast.makeText(getApplicationContext(), "Add to Wish List Clicked at position " + " : " + position, Toast.LENGTH_LONG).show(); 

            break; 

           default: 
            break; 
          } 

          return true; 
         } 
        }); 

        break; 

       default: 
        break; 
      } 


     } 
    }); 

} catch (Exception e) { 

    e.printStackTrace(); 
} 

return convertView; 
} 

} 

彈出menu.xml文件

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android" > 
<item 
    android:id="@+id/install" 
    android:title="Install" /> 
<item 
    android:id="@+id/addtowishlist" 
    android:title="Add to wishlist" /> 
</menu> 

推薦本:Android List view layout Similar to Google play

+0

完美地工作! Thankyou這麼多:) – TeeKay

+0

你也可以upvote我的回答:) –

+0

工作很好。謝謝... –

相關問題