问题描述
我的工作有不同的布局列表。但是,在一个getView调用我必须消耗从数据表,而不是一两个元素。我基本上有两种不同的意见。在第一个布局我消耗一个数据元素,排在第二的布局我消耗三个数据元素。我应该怎么做正确?
伪code:
getView()
{
如果(第一个布局)
{
数据[位置]
//做工作吧
}
如果(第二个布局)
{
数据[位置]
数据[位置+ 1]
数据[位置+ 2]
// 做一些工作
}
}
很认真落实你的基地适配器的每个方法。这需要一点数学。
考虑您的数据阵列的数据是[]有n个元素。
您必须让你的相应的分区。
考虑有10个元素,然后下面的映射将发生:
数据位置,列出项目的位置是:
0 - > 0,1 - > 1,2 - > 1,3 - > 1 .... 8 - > 4,9 - > 5
列表项因此总数为6;
所以,你的基础转接器应该是这样的:
公共类TestAdapter延伸BaseAdapter { 私有静态最终诠释VIEW_TYPE_SINGE = 0;
私有静态最终诠释VIEW_TYPE_TRIPLE = 1; 的String []数据= {A,B,C,D,E,F,G,H,I,J}; LayoutInflater充气; 公共TestAdapter(上下文的背景下){
this.inflator =(LayoutInflater)上下文
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
} //考虑到你的第一个位置使用单个元素和2使用三重
@覆盖
公众诠释的getCount(){
诠释计数= 0;
INT positionIndex =(data.length - 1)4%;
INT positionOffset =(data.length - 1)/ 4;
如果(positionIndex == 0){
计数=(2 * positionOffset)+ 1;
}其他{
计数=(2 * positionOffset)+2;
}
返回计数;
} @覆盖
公共对象的getItem(INT位置){
返回getItemData(位置);
} @覆盖
公众诠释getViewTypeCount(){
返回2;
} @覆盖
公众诠释getItemViewType(INT位置){
如果(位置%2 == 0){
返回VIEW_TYPE_SINGE;
}其他{
返回VIEW_TYPE_TRIPLE;
}
} @覆盖
众长getItemId(INT位置){
返回的位置;
} @覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){ 如果(getItemViewType(位置)== VIEW_TYPE_SINGE){
如果(convertView == NULL){
convertView = inflator.inflate(R.layout.test_item_layout_1,
空,假);
}
的String []数据= getItemData(位置);
TextView的视图=(TextView中)convertView.findViewById(R.id.one);
如果(数据[0]!= NULL)
view.setText(数据[0]);
返回convertView;
}其他{
如果(convertView == NULL){
convertView = inflator.inflate(R.layout.test_item_layout_2,
空,假);
} 的String []数据= getItemData(位置);
TextView的视图1 =(TextView中)convertView.findViewById(R.id.one);
如果(数据[0]!= NULL)
view1.setText(数据[0]); TextView的视图2 =(TextView中)convertView.findViewById(R.id.two);
如果(数据[1]!= NULL)
view2.setText(数据[1]); TextView的视图3 =(TextView中)convertView.findViewById(R.id.three);
如果(数据[2]!= NULL)
view3.setText(数据[2]); 返回convertView;
}
} 私有String [] getItemData(INT位置){
的String []数据= NULL;
INT positionBaseIndex =位置/ 2 * 4;
如果(getItemViewType(位置)== VIEW_TYPE_SINGE){
数据=新的String [1];
数据[0] = this.data [positionBaseIndex];
}其他{
数据=新的String [3];
如果(this.data.length> positionBaseIndex + 1)的
数据[0] = this.data [positionBaseIndex + 1〕;
如果(this.data.length> positionBaseIndex + 2)
数据[1] = this.data [positionBaseIndex + 2];
如果(this.data.length> positionBaseIndex + 3)的
数据[2] = this.data [positionBaseIndex + 3];
}
返回的数据;
}}
有这样做的许多方面。您可以在构造函数将您的数据在两个列表然后实现基本适配器的方法。
谢谢,我希望这会有所帮助。 :)
I'm working on list with different layouts. But in one getView call I must consume two elements from data list instead of one. I basically have two different views. In first layout I consume one data element, in second layout I consume three data elements. How should I do it properly ?
Pseudo code:
getView()
{
if(first layout)
{
data[position]
// do the work with it
}
if(second layout)
{
data[position]
data[position+1]
data[position+2]
// do some work
}
}
Implement every method of your base adapter very carefully. It requires a bit of maths.
Consider your data array is data[] has n number of elements.
You have to make partition of your accordingly.
consider there are 10 elements then following mapping will take place:
data position to list item position :
0 -> 0, 1 -> 1, 2 -> 1, 3 -> 1 .... 8 -> 4, 9 -> 5.
Hence total count of list item will be 6;
So your base adapter should look like this:
public class TestAdapter extends BaseAdapter {
private static final int VIEW_TYPE_SINGE = 0;
private static final int VIEW_TYPE_TRIPLE = 1;
String[] data = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j"};
LayoutInflater inflator;
public TestAdapter(Context context) {
this.inflator = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
// Considering your first position use single element and 2nd use triple
@Override
public int getCount() {
int count = 0;
int positionIndex = (data.length - 1) % 4;
int positionOffset = (data.length - 1) / 4;
if (positionIndex == 0) {
count = (2 * positionOffset) + 1;
} else {
count = (2 * positionOffset) + 2;
}
return count;
}
@Override
public Object getItem(int position) {
return getItemData(position);
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
if (position % 2 == 0) {
return VIEW_TYPE_SINGE;
} else {
return VIEW_TYPE_TRIPLE;
}
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (getItemViewType(position) == VIEW_TYPE_SINGE) {
if (convertView == null) {
convertView = inflator.inflate(R.layout.test_item_layout_1,
null, false);
}
String[] data = getItemData(position);
TextView view = (TextView) convertView.findViewById(R.id.one);
if (data[0] != null)
view.setText(data[0]);
return convertView;
} else {
if (convertView == null) {
convertView = inflator.inflate(R.layout.test_item_layout_2,
null, false);
}
String[] data = getItemData(position);
TextView view1 = (TextView) convertView.findViewById(R.id.one);
if (data[0] != null)
view1.setText(data[0]);
TextView view2 = (TextView) convertView.findViewById(R.id.two);
if (data[1] != null)
view2.setText(data[1]);
TextView view3 = (TextView) convertView.findViewById(R.id.three);
if (data[2] != null)
view3.setText(data[2]);
return convertView;
}
}
private String[] getItemData(int position) {
String[] data = null;
int positionBaseIndex = position / 2 * 4;
if (getItemViewType(position) == VIEW_TYPE_SINGE) {
data = new String[1];
data[0] = this.data[positionBaseIndex];
} else {
data = new String[3];
if (this.data.length > positionBaseIndex + 1)
data[0] = this.data[positionBaseIndex + 1];
if (this.data.length > positionBaseIndex + 2)
data[1] = this.data[positionBaseIndex + 2];
if (this.data.length > positionBaseIndex + 3)
data[2] = this.data[positionBaseIndex + 3];
}
return data;
}
}
There are many ways of doing so. You can divide your data in two lists in constructor then implement method of base adapter.
Thanks, I hope this will help. :)
这篇关于取的多个数据单元中适配器getView()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!