2015-06-18 60 views
0

我已經跑了baoyz'庫的樣本,一切都很完美。現在我並沒有把所有的代碼都放在腦海裏。在「簡單活動」類中,找到菜單。 (欲瞭解更多信息,請看這裏的回購:https://github.com/baoyongzhang/SwipeMenuListView) 現在我想爲它設置一個敬酒當我點擊「打開」。默認情況下,當我點擊打開時,沒有任何反應。我想要它做的是顯示一條消息「工作」,以便我可以知道並進行下一步構建。 SimpleActivity.java: package com.baoyz.swipemenulistviewsample;如何將SwipeMenuListView中的Toast添加到「打開」項目中?

import java.util.List; 

import android.app.Activity; 
import android.content.ComponentName; 
import android.content.Intent; 
import android.content.pm.ApplicationInfo; 
import android.content.pm.ResolveInfo; 
import android.graphics.Color; 
import android.graphics.drawable.ColorDrawable; 
import android.net.Uri; 
import android.os.Bundle; 
import android.util.TypedValue; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.AdapterView.OnItemLongClickListener; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.baoyz.swipemenulistview.SwipeMenu; 
import com.baoyz.swipemenulistview.SwipeMenuCreator; 
import com.baoyz.swipemenulistview.SwipeMenuItem; 
import com.baoyz.swipemenulistview.SwipeMenuLayout; 
import com.baoyz.swipemenulistview.SwipeMenuListView; 
import com.baoyz.swipemenulistview.SwipeMenuListView.OnMenuItemClickListener; 
import com.baoyz.swipemenulistview.SwipeMenuListView.OnSwipeListener; 

public class SimpleActivity extends Activity { 

    private List<ApplicationInfo> mAppList; 
    private AppAdapter mAdapter; 
    private SwipeMenuListView mListView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_list); 

     mAppList = getPackageManager().getInstalledApplications(0); 

     mListView = (SwipeMenuListView) findViewById(R.id.listView); 
     mAdapter = new AppAdapter(); 
     mListView.setAdapter(mAdapter); 

     // step 1. create a MenuCreator 
     SwipeMenuCreator creator = new SwipeMenuCreator() { 

      @Override 
      public void create(SwipeMenu menu) { 
       // create "open" item 
       SwipeMenuItem openItem = new SwipeMenuItem(
         getApplicationContext()); 
       // set item background 
       openItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9, 
         0xCE))); 
       // set item width 
       openItem.setWidth(dp2px(90)); 
       // set item title 
       openItem.setTitle("Open"); 
       // set item title fontsize 
       openItem.setTitleSize(18); 
       // set item title font color 
       openItem.setTitleColor(Color.WHITE); 
       // add to menu 
       menu.addMenuItem(openItem); 

       // create "delete" item 
       SwipeMenuItem deleteItem = new SwipeMenuItem(
         getApplicationContext()); 
       // set item background 
       deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9, 
         0x3F, 0x25))); 
       // set item width 
       deleteItem.setWidth(dp2px(90)); 
       // set a icon 
       deleteItem.setIcon(R.drawable.ic_delete); 
       // add to menu 
       menu.addMenuItem(deleteItem); 

      } 
     }; 
     // set creator 
     mListView.setMenuCreator(creator); 

     // step 2. listener item click event 
     mListView.setOnMenuItemClickListener(new OnMenuItemClickListener() { 
      @Override 
      public boolean onMenuItemClick(int position, SwipeMenu menu, int index) { 
       ApplicationInfo item = mAppList.get(position); 
       switch (index) { 
       case 0: 
        // open 
        open(item); 
        break; 
        mAppList. 
       case 1: 
        // delete 
//     delete(item); 
        mAppList.remove(position); 
        mAdapter.notifyDataSetChanged(); 
        break; 
       } 
       return false; 
      } 
     }); 

     // set SwipeListener 
     mListView.setOnSwipeListener(new OnSwipeListener() { 

      @Override 
      public void onSwipeStart(int position) { 
       // swipe start 
      } 

      @Override 
      public void onSwipeEnd(int position) { 
       // swipe end 
      } 
     }); 

     // other setting 
//  listView.setCloseInterpolator(new BounceInterpolator()); 

     // test item long click 
     mListView.setOnItemLongClickListener(new OnItemLongClickListener() { 

      @Override 
      public boolean onItemLongClick(AdapterView<?> parent, View view, 
        int position, long id) { 
       Toast.makeText(getApplicationContext(), position + " long click", 0).show(); 
       return false; 
      } 
     }); 

    } 

    private void delete(ApplicationInfo item) { 
     // delete app 
     try { 
      Intent intent = new Intent(Intent.ACTION_DELETE); 
      intent.setData(Uri.fromParts("package", item.packageName, null)); 
      startActivity(intent); 
     } catch (Exception e) { 
     } 
    } 

    private void open(ApplicationInfo item) { 
     // open app 
     Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null); 
     resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER); 
     resolveIntent.setPackage(item.packageName); 
     List<ResolveInfo> resolveInfoList = getPackageManager() 
       .queryIntentActivities(resolveIntent, 0); 
     if (resolveInfoList != null && resolveInfoList.size() > 0) { 
      ResolveInfo resolveInfo = resolveInfoList.get(0); 
      String activityPackageName = resolveInfo.activityInfo.packageName; 
      String className = resolveInfo.activityInfo.name; 

      Intent intent = new Intent(Intent.ACTION_MAIN); 
      intent.addCategory(Intent.CATEGORY_LAUNCHER); 
      ComponentName componentName = new ComponentName(
        activityPackageName, className); 

      intent.setComponent(componentName); 
      startActivity(intent); 


      ) 
     } 
    } 

    class AppAdapter extends BaseAdapter { 

     @Override 
     public int getCount() { 
      return mAppList.size(); 
     } 

     @Override 
     public ApplicationInfo getItem(int position) { 
      return mAppList.get(position); 
     } 

     @Override 
     public long getItemId(int position) { 
      return position; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      if (convertView == null) { 
       convertView = View.inflate(getApplicationContext(), 
         R.layout.item_list_app, null); 
       new ViewHolder(convertView); 
      } 
      ViewHolder holder = (ViewHolder) convertView.getTag(); 
      ApplicationInfo item = getItem(position); 
      holder.iv_icon.setImageDrawable(item.loadIcon(getPackageManager())); 
      holder.tv_name.setText(item.loadLabel(getPackageManager())); 
      return convertView; 
     } 

     class ViewHolder { 
      ImageView iv_icon; 
      TextView tv_name; 

      public ViewHolder(View view) { 
       iv_icon = (ImageView) view.findViewById(R.id.iv_icon); 
       tv_name = (TextView) view.findViewById(R.id.tv_name); 
       view.setTag(this); 
      } 
     } 
    } 

    private int dp2px(int dp) { 
     return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, 
       getResources().getDisplayMetrics()); 
    } 

} 

我還沒有做任何事情呢,我只是想知道在哪裏實施敬酒行動。 感謝

+2

只能用於調試嗎?爲什麼不使用logcat呢? – shkschneider

回答

0

設置一個OnClickListeneropenItem

openItem.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     Toast.makeText(getActivity(), "Worked", Toast.LENGTH_LONG).show(); 
    } 
}); 

正如評論所說,使用logcat的調試,如果你不執行一個動作的onClick只是還沒有。

相關問題