2013-05-17 3 views
2

나는 세 가지 다른 레이아웃을 부풀리기 위해 custom listView를 가지고있다.ListView에서 세 가지 레이아웃을 어떻게 확장 할 수 있습니까?

내가 많은 질문을 보았다 그러나 그들은 짝수와 홀수 위치를하지만 내 목록보기에서 모두 목록보기로 팽창한다 레이아웃이 다른 조건에 따라 달라

같은 동적 경우 (내가 == 0) 첫 번째 레이아웃이 비정상적으로 늘어나고 두 번째 것 (i == 1)이 두 번째 등등이됩니다.

변수 "i"는 주 활동에서 얻는 값과 같습니다.

public class SocialListAdapter extends ArrayAdapter<Item> { 


private Activity activity; 
private List<Item> items; 
private Item objBean; 
private int row; 
private int i; 


public SocialListAdapter(Activity act, int resource, List<Item> arrayList) { 
    super(act, resource, arrayList); 
    // TODO Auto-generated constructor stub 
    this.activity= act; 
    this.items = arrayList; 
    this.row = resource; 

} 

@Override 
public int getViewTypeCount() { 
    return 2; 
} 

@Override 
    public int getItemViewType(int position) { 
    return position; 


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

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    View view = convertView; 
    ViewHolder holder; 
    objBean = items.get(position); 


     i = objBean.getI(); 


      if (view == null) { 

       LayoutInflater inflater = (LayoutInflater) activity 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 



     if(i == 0){ 

      view = inflater.inflate(R.layout.list, null); 

     } else if (i == 1){ 
      view = inflater.inflate(R.layout.row1, null); 
     } 
      holder = new ViewHolder(); 
      view.setTag(holder); 
      } else { 
       holder = (ViewHolder) view.getTag(); 
      } 
이 코드 만 힘 작업을 시도

... 내가

+1

3 가지 레이아웃을 지원하려면 'getViewTypeCount()'에 2를 반환하는 것이 좋습니다. 또한,'getItemViewType (int position)'은'position'에 의해 주어진 행에 표시하고자하는 유형에 고유 한 값을 반환해야합니다. 즉, 타입 수가 3이면, * n *이 아닌 해당 메소드에서 3 가지 값만 반환해야합니다. –

답변

0

이 시도리스트 뷰 arrayoutofbondexception 스크롤 할 때

은 내가 오류가 발생했습니다 :

public class CustomAdapter extends BaseAdapter { 

    private ArrayList<String> comments; 
    Context mContext; 

    public CustomAdapter(Context context, ArrayList<String> comments) { 
     this.mContext = context; 
     this.comments = comments;  
    } 


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

     String item = comments.get(position); 

     if (getItemViewType(position) == 0) { 

      View v = convertView; 
      if (v == null) { 

       //GET View 1 
       LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       ViewGroup viewGroup = (ViewGroup)inflater.inflate(R.layout.item_comment2, null); 

       v = viewGroup; 
      } 

      //Fill Data for Ist view 
      TextView comm = (TextView) v.findViewById(R.id.comment); 
      comm.setText(item); 

      return v; 


     } else if (getItemViewType(position) == 1) { 


      View v = convertView; 
      if (v == null) { 

       //GET View 2 
       LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       ViewGroup viewGroup = (ViewGroup)inflater.inflate(R.layout.item_comment1, null); 


       v = viewGroup; 
      } 

      //Fill Data for IInd view 
      TextView comm = (TextView) v.findViewById(R.id.comment); 
      comm.setText(item); 

      return v; 

     } else { 

      //GET View 3 
      View v = convertView; 
      if (v == null) { 
       LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       ViewGroup viewGroup = (ViewGroup)inflater.inflate(R.layout.item_comment3, null); 


       v = viewGroup; 
      } 

      //Fill Data for IInd view 
      TextView comm = (TextView) v.findViewById(R.id.comment); 
      comm.setText(item); 

      return v; 

     } 

    } 

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

    @Override 
    public Object getItem(int position) {  
     return comments.get(position); 
    } 

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

    @Override 
    public int getViewTypeCount() { 
     return 2; 
    } 

    @Override 
    public int getItemViewType(int position) { 

     if(position == 0) 
      return 0; 
     else if (position == 1) 
      return 1; 
     else 
      return 2; 
    } 
} 

당신은 최적화 할 수 있습니다 뷰 홀더를 정의하여 스크롤을 부드럽게 만들 수 있습니다. 시도 this

getItemViewType에서 어떤 유형의보기를 표시할지 여부에 대한 조건을 설정할 수 있습니다.

1

getItemViewType은 0에서 getViewTypeCount까지의 숫자를 반환해야합니다.

0

사용

0

당신은 상태를 확인하고 원하는 레이아웃을 부풀려 수의 getView 방법 내부 "(위치 == 0)하는 경우". 다음은 예제입니다. 나는 그것에 따라 세 가지 범주가 있는데 나는 다른 레이아웃을 부 풀린다.

public class ApplicationAdapter extends BaseAdapter 
    { 
     private Context cntx; 

     /** 
     * 0 - featured 1- Top rated 2- other 
     */ 
     private int whichCategory; 

     /** 
     * Constructor 
     * 
     * @param context 
     * @param iwhichCategory 
     *   0 - featured 1- Top rated 2- other 
     */ 

     public ApplicationAdapter(Context context, int iwhichCategory) 
      { 
       cntx = context; 

       whichCategory = iwhichCategory; 

      } 

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

     @Override 
     public Object getItem(int position) 
      { 
       return null; 
      } 

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


     @Override 
     public View getView(int position, View convertView, ViewGroup parent) 
      { 
       if (convertView == null) 
        { 
         // This a new view we inflate the new layout 
         LayoutInflater inflater = (LayoutInflater) cntx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
         if (whichCategory == 0) 
          convertView = inflater.inflate(R.layout.featured_app_grid_item, parent, false); 
         if (whichCategory == 1) 
          convertView = inflater.inflate(R.layout.other_app_grid_item, parent, false); 
         if (whichCategory == 2) 
          convertView = inflater.inflate(R.layout.other_app_grid_item1, parent, false); 
        } 
      } 
    } 
+0

위치에 따라 레이아웃을 변경하고 싶지 않습니다 ... – Saad

+0

지금 확인하십시오. – Sandeep

관련 문제