2012-11-01 108 views
0

我用listview的Base適配器,當滾動,位置變化時,如何結束這個, 這是我的Base Adapter Class。如何在android中滾動時listview的主要位置

Hello, 

我使用的基礎適配器列表視圖,並滾動時,當前位置

變化,如何在走到這一步, 這是我的基本適配器類。

public class MyCustomBaseAdapter extends BaseAdapter { 
     MyCustomBaseAdapter custom; 
     private OnClickListener onCheckClick; 
     private OnCheckedChangeListener OnTextClick; 
     Context context; 
     ViewHolder holder; 

     public void setOnClickListeners(OnClickListener 

onCheckClick) { 
      // TODO Auto-generated method stub 
      this.onCheckClick = onCheckClick; 
     } 

     public void setOnCheckedChangeListener 

(OnCheckedChangeListener OnTextClick) { 
      // TODO Auto-generated method stub 
      this.OnTextClick = OnTextClick; 
     } 
     private static ArrayList<SearchResults> searchArrayList; 

     private LayoutInflater mInflater; 

     public MyCustomBaseAdapter(Context context, 

ArrayList<SearchResults> results) { 
      this.context = context; 
      searchArrayList = results; 
      mInflater = LayoutInflater.from(context); 
     } 

     public int getCount() { 
      return searchArrayList.size(); 
     } 

     public Object getItem(int position) { 
      return searchArrayList.get(position); 
     } 

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

     public View getView(final int position, View convertView, 

ViewGroup parent) { 

      SearchResults planet = (SearchResults) this.getItem( 

position); 

      if (convertView == null) { 

      convertView = mInflater.inflate 

(R.layout.custom_row_view, null); 

      holder = new ViewHolder(); 

      holder.txtName = (TextView) 

convertView.findViewById(R.id.mobileNumber); 
      holder.txtCityState = (TextView) 

convertView.findViewById(R.id.messageContent); 
      holder.id_txtView = (TextView) 

convertView.findViewById(R.id.id_textView); 
      holder.checkBox = (CheckBox) 

convertView.findViewById(R.id.checkBox1); 
      holder.datetime = (TextView) 

convertView.findViewById(R.id.date_time); 
      holder.type = (TextView) convertView.findViewById 

(R.id.type); 
      holder.custom_layout = (LinearLayout) 

convertView.findViewById(R.id.custom_layout);  

      holder.checkBox.setOnClickListener(new 

OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method 

stub 
       ((SMS)context).function(position); 
      } 
     }); 
      holder.checkBox .setTag(Integer.valueOf(position)); 
      convertView.setTag(holder); 

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

      holder.txtName.setText(searchArrayList.get 

(position).getName()); 
      holder.txtCityState.setText(searchArrayList.get 

(position).getCityState()); 
      holder.id_txtView.setText(searchArrayList.get 

(position).getPhone()); 
      holder.datetime.setText(searchArrayList.get 

(position).getDatetime()); 

      //holder.type.setText(searchArrayList.get 

(position).getType()); 

      holder.checkBox .setTag(Integer.valueOf(position)); 

      //(2).This line is for avoiding checkbox selection for 

other checkboxess 
      /*holder.checkBox.setChecked(planet.isChecked());*/ 

      holder.checkBox.setOnCheckedChangeListener 

(OnTextClick); 

      holder.checkBox.setChecked(searchArrayList.get 

(position).isSelected());  

      return convertView; 
     } 

     static class ViewHolder { 
      TextView txtName; 
      TextView txtCityState; 
      TextView id_txtView; 
      CheckBox checkBox; 
      TextView datetime; 
      TextView type; 
      LinearLayout custom_layout; 
      LinearLayout main_layout; 
      TextView empty_txt_msg; 

     } 

     Integer[] imgid = { 
        R.drawable.ic_launcher, 
        R.drawable.ic_launcher, 
       }; 
    } 

請幫我解決這個問題。

回答

0

您可以使用ListView的方法setOnScrollListener

你每次滾動,你可以得到firstvisible位置,lastvisible位置的TOTALNUMBER你position.now可以在代碼中使用它的時間。

listview.setOnScrollListener(new OnScrollListener() { 
     @Override 
     public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { 
      //get the showed item num, and if its equal to total, you can hide the view 
      //get a reference to your viewid 
      //viewid.setVisibility(View.GONE); 
     } 
     @Override 
     public void onScrollStateChanged(AbsListView view, int scrollState) { 
     } 
    }); 

編輯: 裏面你在getview方法適配器,你可以指定下面的代碼:

holder.chkselect = (CheckBox) convertView 
       .findViewById(R.id.txtselect); 
    holder.chkselect.setTag(position); 
    holder.chkselect.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Override 
public void onCheckedChanged(CompoundButton buttonView, 
     boolean isChecked) { 
    if (isChecked) { 

     int position = Integer.parseInt(buttonView.getTag() 
       .toString()); 
     OrderDetail.acceptposition.add(String 
       .valueOf(position)); 
    } else { 
     int position = Integer.parseInt(buttonView.getTag() 
       .toString()); 

     if (OrderDetail.acceptposition.contains(String 
       .valueOf(position))) { 
      OrderDetail.acceptposition.remove(String 
        .valueOf(position)); 
     } 
    } 

} 

});

這裏在ArralyList 接受你有所有檢查的位置。嘗試一下。

+0

如果我選擇複選框,一個位置越來越,如果我滾動和取消選擇相同的複選框,其他位置越來越,但我需要相同的位置..如何得到這個? – Vishnu

+0

我編輯了我的答案。嘗試一下。 –

相關問題