2015-09-10 63 views
0

我有一個ListView適配器如下:Android的列表視圖適配器,帶滾動

public class ListViewAdapter extends BaseAdapter { 

    // Declare Variables 
    private Context mContext; 
    private LayoutInflater inflater; 
    private ArrayList<ApplicationPojo> applicationList = null; 
    private Activity activity; 
    private Bitmap logoImg; 
    private ViewHolder holder; 
    private String logoName; 
    //private String fileName; 
    private PackageManager pm; 
    private boolean isAppInstalled; 


    public ListViewAdapter(Context context, ArrayList<ApplicationPojo> applicationList) { 
     this.mContext = context; 
     this.activity = (Activity)mContext; 
     this.applicationList = applicationList; 
     this.inflater = LayoutInflater.from(mContext); 
    } 

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

    @Override 
    public ApplicationPojo getItem(int position) { 
     return applicationList.get(position); 
    } 

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

    public View getView(final int position, View view, ViewGroup parent) 
    { 

     if (view == null) 
     { 

      holder = new ViewHolder(); 
      view = inflater.inflate(R.layout.singleapp, null); 

      holder.appName = (TextView) view.findViewById(R.id.appName); 
      holder.developer = (TextView) view.findViewById(R.id.developer); 
      holder.rate = (TextView) view.findViewById(R.id.rateApp); 
      holder.updateBtn = (ImageView) view.findViewById(R.id.updateApp); 
      holder.installBtn = (ImageView) view.findViewById(R.id.install); 
      holder.openInstalledApp = (ImageView) view.findViewById(R.id.openInstalledApp); 

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

     try 
     { 
      final View finalView = view; 

      holder.logo = (ImageView) finalView.findViewById(R.id.appLogo); 
      logoName = applicationList.get(position).getLogo(); 

      Picasso.with(activity) 
        .load(IPClass.SERVERIP + logoName) 
        .into(holder.logo); 


      if(!appInstalled(applicationList.get(position).getFileName().substring(0, applicationList.get(position).getFileName().length() - 4))) 
      { 
       if(new File(Environment.getExternalStorageDirectory() + "/download/appsaraai/" + applicationList.get(position).getFileName()).exists()) 
       { 

        holder.installBtn.setVisibility(View.VISIBLE); 
        holder.installBtn.setOnClickListener(new View.OnClickListener() 
        { 
         @Override 
         public void onClick(View v) { 
          Intent intent = new Intent(Intent.ACTION_VIEW); 
          intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/appsaraai/" + applicationList.get(position).getFileName())), "application/vnd.android.package-archive"); 
          activity.startActivity(intent); 

          for (int i = 0; i < DownloadLists.list.size(); i++) { 

           if (DownloadLists.list.get(i).getName().equals(applicationList.get(position).getName())); 
           { 
            DownloadLists.list.remove(i); 
           } 
          } 
         } 
        }); 
       } 

       else 
       { 
        holder.updateBtn.setVisibility(view.VISIBLE); 
        holder.updateBtn.setOnClickListener(new View.OnClickListener() 
        { 
         @Override 
         public void onClick(View arg0) 
         { 
          String id = applicationList.get(position).getId(); 
          int category = applicationList.get(position).getCategory(); 

          Intent rec1Intent = new Intent(activity,AppView.class); 
          activity.startActivity(rec1Intent); 

          AppView appView = new AppView(); 
          appView.setParameters(id,category); 
         } 
        }); 
       } 
      } 

      else 
      { 
       holder.updateBtn.setVisibility(View.GONE); 
       holder.openInstalledApp.setVisibility(View.VISIBLE); 
       holder.openInstalledApp.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         Context ctx = mContext; 
         Intent mIntent = ctx.getPackageManager().getLaunchIntentForPackage(applicationList.get(position).getFileName()); 
         String mainActivity = mIntent.getComponent().getClassName(); 
         Intent intent = new Intent("android.intent.category.LAUNCHER"); 
         intent.setClassName(applicationList.get(position).getFileName(), mainActivity); 
         activity.startActivity(intent); 
        } 
       }); 
      } 

      holder.appName.setText(applicationList.get(position).getName()); 
      holder.developer.setText(applicationList.get(position).getDeveloper()); 

     } 
     catch (Exception ex) 
     { 
      Log.d("Adapter Exception", ex.toString()); 
     } 
     return view; 
    } 

    public boolean appInstalled(String checkApp) 
    { 
     pm = activity.getPackageManager(); 

     try 
     { 
      pm.getPackageInfo(checkApp, PackageManager.GET_ACTIVITIES); 
      isAppInstalled = true; 
     } 
     catch (PackageManager.NameNotFoundException e) 
     { 
      isAppInstalled = false; 
     } 

     return isAppInstalled; 
    } 

    public class ViewHolder 
    { 
     ImageView logo; 
     TextView appName; 
     TextView developer; 
     TextView rate; 
     ImageView updateBtn; 
     ImageView installBtn; 
     ImageView openInstalledApp; 

    } 

} 

列表中包含的應用程序列表。在這裏,我檢查設備中是否安裝了應用程序,以便在該項目上顯示打開按鈕,並且如果未安裝應用程序,則會顯示下載按鈕,以便用戶可以下載該應用程序。每件事情都很好,直到我滾動列表。當我滾動列表按鈕與任何想法相互重疊!

+1

你可以發佈你的XML佈局?也許截圖會更好地解決問題。 –

+0

getItemId函數似乎沒用,它用於什麼? – dasjkdj

回答

2

你如果沒有安裝應用程序的holder.openInstalledApp不setVisibility(GONE):

試試這個。編輯爲您的代碼:

public class ListViewAdapter extends BaseAdapter { 

// Declare Variables 
private Context mContext; 
private LayoutInflater inflater; 
private ArrayList<ApplicationPojo> applicationList = null; 
private Activity activity; 
private Bitmap logoImg; 
private ViewHolder holder; 
private String logoName; 
//private String fileName; 
private PackageManager pm; 
private boolean isAppInstalled; 


public ListViewAdapter(Context context, ArrayList<ApplicationPojo> applicationList) { 
    this.mContext = context; 
    this.activity = (Activity)mContext; 
    this.applicationList = applicationList; 
    this.inflater = LayoutInflater.from(mContext); 
} 

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

@Override 
public ApplicationPojo getItem(int position) { 
    return applicationList.get(position); 
} 

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

public View getView(final int position, View view, ViewGroup parent) 
{ 

    if (view == null) 
    { 

     holder = new ViewHolder(); 
     view = inflater.inflate(R.layout.singleapp, null); 

     holder.appName = (TextView) view.findViewById(R.id.appName); 
     holder.developer = (TextView) view.findViewById(R.id.developer); 
     holder.rate = (TextView) view.findViewById(R.id.rateApp); 
     holder.updateBtn = (ImageView) view.findViewById(R.id.updateApp); 
     holder.installBtn = (ImageView) view.findViewById(R.id.install); 
     holder.openInstalledApp = (ImageView) view.findViewById(R.id.openInstalledApp); 

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

    try 
    { 
     final View finalView = view; 

     holder.logo = (ImageView) finalView.findViewById(R.id.appLogo); 
     logoName = applicationList.get(position).getLogo(); 

     Picasso.with(activity) 
       .load(IPClass.SERVERIP + logoName) 
       .into(holder.logo); 


     if(!appInstalled(applicationList.get(position).getFileName().substring(0, applicationList.get(position).getFileName().length() - 4))) 
     { 
      //----------My code ----------- 
      //If app not installed holder.openInstalledApp 
      //must be not visible 

      holder.openInstalledApp.setVisibility(View.GONE); 
      //--------------------- 

      if(new File(Environment.getExternalStorageDirectory() + "/download/appsaraai/" + applicationList.get(position).getFileName()).exists()) 
      { 

       //----------My code ----------- 
       //If app not must be installed (not updated) 
       //holder.updateBtn must be not visible 
       holder.updateBtn.setVisibility(View.GONE); 
       //--------------------- 

       holder.installBtn.setVisibility(View.VISIBLE); 
       holder.installBtn.setOnClickListener(new View.OnClickListener() 
       { 
        @Override 
        public void onClick(View v) { 
         Intent intent = new Intent(Intent.ACTION_VIEW); 
         intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/appsaraai/" + applicationList.get(position).getFileName())), "application/vnd.android.package-archive"); 
         activity.startActivity(intent); 

         for (int i = 0; i < DownloadLists.list.size(); i++) { 

          if (DownloadLists.list.get(i).getName().equals(applicationList.get(position).getName())); 
          { 
           DownloadLists.list.remove(i); 
          } 
         } 
        } 
       }); 
      } 

      else 
      { 

       //----------My code ----------- 
       //If app not must be updated 
       //holder.updateBtn must be not visible 

       holder.installBtn.setVisibility(View.GONE); 
       //---------- ---------- 

       holder.updateBtn.setVisibility(view.VISIBLE); 
       holder.updateBtn.setOnClickListener(new View.OnClickListener() 
       { 
        @Override 
        public void onClick(View arg0) 
        { 
         String id = applicationList.get(position).getId(); 
         int category = applicationList.get(position).getCategory(); 

         Intent rec1Intent = new Intent(activity,AppView.class); 
         activity.startActivity(rec1Intent); 

         AppView appView = new AppView(); 
         appView.setParameters(id,category); 
        } 
       }); 
      } 
     } 

     else 
     { 

      //----------My code ----------- 
      //If app installed and updated 
      //buttons what no needed must be not visible 

      holder.installBtn.setVisibility(View.GONE); 
      holder.updateBtn.setVisibility(View.GONE); 
      //--------------------- 

      holder.openInstalledApp.setVisibility(View.VISIBLE); 
      holder.openInstalledApp.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        Context ctx = mContext; 
        Intent mIntent = ctx.getPackageManager().getLaunchIntentForPackage(applicationList.get(position).getFileName()); 
        String mainActivity = mIntent.getComponent().getClassName(); 
        Intent intent = new Intent("android.intent.category.LAUNCHER"); 
        intent.setClassName(applicationList.get(position).getFileName(), mainActivity); 
        activity.startActivity(intent); 
       } 
      }); 
     } 

     holder.appName.setText(applicationList.get(position).getName()); 
     holder.developer.setText(applicationList.get(position).getDeveloper()); 

    } 
    catch (Exception ex) 
    { 
     Log.d("Adapter Exception", ex.toString()); 
    } 
    return view; 
} 

public boolean appInstalled(String checkApp) 
{ 
    pm = activity.getPackageManager(); 

    try 
    { 
     pm.getPackageInfo(checkApp, PackageManager.GET_ACTIVITIES); 
     isAppInstalled = true; 
    } 
    catch (PackageManager.NameNotFoundException e) 
    { 
     isAppInstalled = false; 
    } 

    return isAppInstalled; 
} 

public class ViewHolder 
{ 
    ImageView logo; 
    TextView appName; 
    TextView developer; 
    TextView rate; 
    ImageView updateBtn; 
    ImageView installBtn; 
    ImageView openInstalledApp; 

} 

} 

記住convertView保存它以前的狀態,需要爲每個項目進行更改。

+0

謝謝親愛的回覆!但如何改變視圖的任何線索? – Shoaib

+0

您的代碼發生了一些變化。現在改正 – sunnyday

+0

你真是太棒了!謝謝,完美的作品!現在有一個問題,我得到NullPointer異常:String mainActivity = mIntent.getComponent()。getClassName();雖然我用它在其他地方打開應用程序,但在這裏它不工作! – Shoaib