2017-02-23 2 views
0

저는 listview에서 일하고 있는데 두 가지 뷰를 보여주고 싶은 어댑터가 있습니다. 이를 위해 사용자 정의 기본 어댑터와 함께 listview을 사용하고 있습니다. 내 코드가 잘 작동하지만 어떤 값을 삽입해야하는지 모르겠다. getViewType().안드로이드에 getViewType에 값을 삽입하는 방법은 무엇입니까?

코드 : 당신이 다음 다른 첫 번째 일 즉 다른보기를 원하는 경우, 신분증하여 원하는 뷰를 관리 할 수있는 모든의

private Context mContext; 
private List<NewsSnippet> mItems; 
private String TAG = CNewsAdpter.class.getSimpleName(); 

public CNewsAdpter(Context m_Context, List<NewsSnippet> items) { 
    this.mContext = m_Context; 
    mItems = items; 
} 

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

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

@Override 
public int getCount() {// get total arraylist size 
    return mItems.size(); 
} 

@Override 
public Object getItem(int position) {// get item position in array list 
    return mItems.get(position); 
} 

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

@SuppressWarnings("deprecation") 
@SuppressLint({"SetTextI18n", "InflateParams"}) 
@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    View v = convertView; 
    int type = getItemViewType(position); 
    if (v == null) { 
     // Inflate the layout according to the view type 
     LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     if (type == 0) { 
      // Inflate the layout with image 
      v = inflater.inflate(R.layout.news_item_for_layout, parent, false); 
     } else { 
      v = inflater.inflate(R.layout.vcom_layout, parent, false); 
     } 
    } 

    final NewsSnippet m = mItems.get(position); 
    ImageView m_Icon = (ImageView) v.findViewById(R.id.imgNewsIcon); 
    TextView m_Titletext = (TextView) v.findViewById(R.id.txtNewsTitle); 
    TextView m_Description = (TextView) v.findViewById(R.id.description); 
    Button m_ActionButton = (Button) v.findViewById(R.id.actionBtn); 
    if (type == 0) { 
     RatingBar m_RatingBar = (RatingBar) v.findViewById(R.id.pop_ratingbar); 

     String s = String.valueOf((float) m.s_szrating); 
     if (!s.equalsIgnoreCase("")) { 
      m_RatingBar.setRating((float) m.s_szrating); 
     } 
    } 

    m_Titletext.setText(m.s_sztitle); 
    m_Description.setText(m.s_szdescription); 
    m_ActionButton.setText(m.s_szcta); 

    m_ActionButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent browserIntent = new Intent(Intent.ACTION_VIEW, 
        Uri.parse(m.s_szlandingUrl)); 
      v.getContext().startActivity(browserIntent); 
     } 
    }); 
    try { 
     String url = m.s_szicon; 
     if (url != null && !url.equals(m_Icon.getTag())) { 
      m_Icon.setTag(url); 

      new CDownloadImageTask(m_Icon) 
        .execute(m.s_szicon); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 


    return v; 
} 
+0

처럼보다 당신은 조건 –

답변

0

첫째. 당신은 당신이

int type = getItemViewType(position); 
    if (v == null) { 
     // Inflate the layout according to the view type 
     LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     if (type == 0) { 
      // Inflate the layout with image 
      v = inflater.inflate(R.layout.news_item_for_layout, parent, false); 
     } else { 
      v = inflater.inflate(R.layout.vcom_layout, parent, false); 
     } 
    } 

변경을 수행 한 view.ie 팽창 할 때 이 지금

@Override 
    public int getItemViewType(int position) { 
     if(postion%2==0){ 
      return 2; 
     } 
     return 1; 
    } 

도움이 될 것입니다 경우 조건의 경우 (유형 == 2) 그 밖의 다른보기. 두 개의 서로 다른 의견이있는 경우

그리고 당신은 getItemType 일부 또는 문자열 변화 상태를 관리하는 경우() 메소드

0

에 따라 다음 기본적으로 두 가지보기 유형이있다.

따라서 어댑터에서 두 가지보기 유형을 정의하십시오.

private static final VIEW_TYPE_FIRST = 0; 
private static final VIEW_TYPE_SECOND = 1; 

이렇게 두보기 유형을 식별 할 수 있습니다.

이제 getItemViewType()을 사용하여 어느 뷰가 어떤 위치에 대해 팽창되는지 알아낼 수 있습니다.

첫 번째 항목 만 첫 번째보기 만 표시하고 나머지 항목은 모두 두 번째 항목으로 표시한다고 가정합니다.

@Override 
public int getItemViewType(int position) { 
if(position == 0){ 
    return VIEW_TYPE_FIRST; 
} 
    return VIEW_TYPE_SECOND; 
} 

따라서 어댑터는 첫 번째보기 유형 만 첫 번째 위치에만 적용하고 다른보기 유형은 다른 모든보기 유형에만 적용합니다.

+0

경우에 그것을 확인하실 수 있습니다 이 대답으로 당신의 문제가 해결 되었습니까? 그런 다음이 대답을 수락하고 회신하는 것을 고려하십시오. –

0

당신이 위치 0에 레이아웃을 변경하려는 경우 ...이 때 레이아웃이 getItemViewType에서 그 위치를 반환 변경할 위치에서이

@Override 
    public int getItemViewType(int position) { 

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



     @Override 
     public View getView(final int position, View convertView, ViewGroup parent) { 
      View v = convertView; 
      int type = getItemViewType(position);// **this return 0 if position is 0 else return 1** 

      if (v == null) { 
       // Inflate the layout according to the view type 
       LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       if (type == 0) { 
        // Inflate the layout with image 
        v = inflater.inflate(R.layout.news_item_for_layout, parent, false); 
       } else { 
        v = inflater.inflate(R.layout.vcom_layout, parent, false); 
       } 
      } 

      //other code 

     } 
관련 문제