항상 동일한 행을 만들기 위해 이전에 사용한 다음 어댑터를 만들었습니다. 나는 textviews와 imageviews의 생성을 제거했습니다.
내가 달성하고 싶은 것은 키에 따라 다른 행을 만드는 것입니다. 행에는 텍스트와 이미지가 포함될 수 있지만 다른 행에는 텍스트 만있을 수 있습니다. 어떻게 할 수 있습니까?
public class DetailsListAdapter extends ArrayAdapter<ArrayList<String>> {
private Context context;
private ArrayList<String> keys;
public DetailsListAdapter(Context context, ArrayList<String> keys) {
super(context,R.layout.details);
this.context = context;
this.keys = keys;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.details, null);
return v;
}
@Override
public int getCount(){
return keys.size();
}
}
행에 대해 다른 레이아웃을 확장하려면 getViewItemType
및 을 재정의해야합니다 getViewTypeCount
.
링크에있는 비디오를 봐야합니다.
http://www.youtube.com/watch?v=wDBM6wVEO70
private static final int TYPE_ITEM1 = 0;
private static final int TYPE_ITEM2 = 1;
private static final int TYPE_ITEM3 = 2;
그때
int type;
@Override
public int getItemViewType(int position) {
if (position== 0){
type = TYPE_ITEM1;
} else if (position == 1){
type = TYPE_ITEM2;
}
else
{
type= TYPE_ITEM3 ;
}
return type;
}
@Override
public int getViewTypeCount() {
return 3;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
LayoutInflater inflater = null;
int type = getItemViewType(position);
// instead of if else you can use a case
if (convertView == null) {
if (type == TYPE_ITEM1 {
//infalte layout of type1
convertView = mInflater.inflate(R.layout.layouttype1,
parent, false);
}
if (type == TYPE_ITEM2) {
//infalte layout of type2
convertView = mInflater.inflate(R.layout.layouttype2,
parent, false);
} else {
//infalte layout of normaltype
convertView = mInflater.inflate(R.layout.layouttype3,
parent, false);
}
...// rest of the code
return convertView;
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다