w.d*_*hue 341 android listview listviewitem
我试图确定一个包含每行不同布局的单个ListView的最佳方法.我知道如何创建自定义行+自定义数组适配器以支持整个列表视图的自定义行,但是如何在ListView中实现许多不同的行样式?
Cri*_*ian 410
既然您知道有多少种类型的布局 - 可以使用这些方法.
getViewTypeCount() - 此方法返回有关列表中有多少行类型的信息
getItemViewType(int position) - 根据位置返回您应使用的布局类型的信息
然后,只有当布局为空并且确定使用类型时,才会对布局进行膨胀getItemViewType.
查看本教程以获取更多信息.
为了实现您在评论中描述的结构优化,我建议:
ViewHolder.它会提高速度,因为您不必findViewById()每次都在getView方法中调用.请参阅API演示中的List14.我希望这会对你有所帮助.如果您可以提供一些XML存根以及您的数据结构和信息,您希望如何将其映射到行中,我将能够为您提供更精确的建议.按像素.
Ber*_*t F 62
我知道如何创建自定义行+自定义数组适配器以支持整个列表视图的自定义行.但是listview如何支持许多不同的行样式?
你已经了解了基础知识.您只需要让自定义适配器根据提供的行/光标信息返回不同的布局/视图.
A ListView可以支持多行样式,因为它派生自AdapterView:
AdapterView是一个视图,其子项由适配器确定.
如果查看适配器,您将看到考虑使用特定于行的视图的方法:
abstract int getViewTypeCount()
// Returns the number of types of Views that will be created ...
abstract int getItemViewType(int position)
// Get the type of View that will be created ...
abstract View getView(int position, View convertView, ViewGroup parent)
// Get a View that displays the data ...
Run Code Online (Sandbox Code Playgroud)
后两种方法提供了位置,因此您可以使用它来确定应该用于该行的视图类型.
当然,您通常不直接使用AdapterView和Adapter,而是使用或派生自其子类之一.Adapter的子类可以添加其他功能,以更改如何获取不同行的自定义布局. 由于用于给定行的视图由适配器驱动,因此技巧是让适配器返回给定行的所需视图.如何执行此操作会因具体适配器而异.
例如,要使用ArrayAdapter,
getView()以膨胀,填充和返回给定位置的所需视图.该getView()方法包括通过convertView参数的机会重用视图.但要使用CursorAdapter的衍生物,
newView()以膨胀,填充和返回当前光标状态的所需视图(即当前"行")[您还需要覆盖bindView以便窗口小部件可以重用视图]但是,要使用SimpleCursorAdapter,
SimpleCursorAdapter.ViewBinder使用setViewValue()方法定义a ,以便为给定行(当前光标状态)和数据"列"充气,填充和返回所需视图.该方法可以只定义"特殊"视图,并遵循SimpleCursorAdapter的"普通"绑定的标准行为.查找最终使用的适配器类型的具体示例/教程.
小智 42
看看下面的代码.
首先,我们创建自定义布局.在这种情况下,有四种类型.
even.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#ff500000"
android:layout_height="match_parent">
<TextView
android:id="@+id/text"
android:textColor="@android:color/white"
android:layout_width="match_parent"
android:layout_gravity="center"
android:textSize="24sp"
android:layout_height="wrap_content" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
odd.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#ff001f50"
android:gravity="right"
android:layout_height="match_parent">
<TextView
android:id="@+id/text"
android:textColor="@android:color/white"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:textSize="28sp"
android:layout_height="wrap_content" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
white.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#ffffffff"
android:gravity="right"
android:layout_height="match_parent">
<TextView
android:id="@+id/text"
android:textColor="@android:color/black"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:textSize="28sp"
android:layout_height="wrap_content" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
black.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#ff000000"
android:layout_height="match_parent">
<TextView
android:id="@+id/text"
android:textColor="@android:color/white"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:textSize="33sp"
android:layout_height="wrap_content" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
然后,我们创建listview项.在我们的例子中,使用字符串和类型.
public class ListViewItem {
private String text;
private int type;
public ListViewItem(String text, int type) {
this.text = text;
this.type = type;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
}
Run Code Online (Sandbox Code Playgroud)
之后,我们创建了一个视图持有者.强烈建议使用Android操作系统,因为Android操作系统会保留布局参考,以便在项目消失后再次显示在屏幕上.如果您不使用此方法,则每次您的项目出现在屏幕上Android操作系统都会创建一个新项目并导致您的应用程序泄漏内存.
public class ViewHolder {
TextView text;
public ViewHolder(TextView text) {
this.text = text;
}
public TextView getText() {
return text;
}
public void setText(TextView text) {
this.text = text;
}
}
Run Code Online (Sandbox Code Playgroud)
最后,我们创建自定义适配器,覆盖getViewTypeCount()和getItemViewType(int position).
public class CustomAdapter extends ArrayAdapter {
public static final int TYPE_ODD = 0;
public static final int TYPE_EVEN = 1;
public static final int TYPE_WHITE = 2;
public static final int TYPE_BLACK = 3;
private ListViewItem[] objects;
@Override
public int getViewTypeCount() {
return 4;
}
@Override
public int getItemViewType(int position) {
return objects[position].getType();
}
public CustomAdapter(Context context, int resource, ListViewItem[] objects) {
super(context, resource, objects);
this.objects = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
ListViewItem listViewItem = objects[position];
int listViewItemType = getItemViewType(position);
if (convertView == null) {
if (listViewItemType == TYPE_EVEN) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.type_even, null);
} else if (listViewItemType == TYPE_ODD) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.type_odd, null);
} else if (listViewItemType == TYPE_WHITE) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.type_white, null);
} else {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.type_black, null);
}
TextView textView = (TextView) convertView.findViewById(R.id.text);
viewHolder = new ViewHolder(textView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.getText().setText(listViewItem.getText());
return convertView;
}
}
Run Code Online (Sandbox Code Playgroud)
我们的活动是这样的:
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // here, you can create a single layout with a listview
listView = (ListView) findViewById(R.id.listview);
final ListViewItem[] items = new ListViewItem[40];
for (int i = 0; i < items.length; i++) {
if (i == 4) {
items[i] = new ListViewItem("White " + i, CustomAdapter.TYPE_WHITE);
} else if (i == 9) {
items[i] = new ListViewItem("Black " + i, CustomAdapter.TYPE_BLACK);
} else if (i % 2 == 0) {
items[i] = new ListViewItem("EVEN " + i, CustomAdapter.TYPE_EVEN);
} else {
items[i] = new ListViewItem("ODD " + i, CustomAdapter.TYPE_ODD);
}
}
CustomAdapter customAdapter = new CustomAdapter(this, R.id.text, items);
listView.setAdapter(customAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView adapterView, View view, int i, long l) {
Toast.makeText(getBaseContext(), items[i].getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
现在在mainactivity.xml中创建一个listview,就像这样
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.shivnandan.gygy.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="100dp" />
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
Jem*_*ems 14
在您的自定义数组适配器中,您可以覆盖getView()方法,正如您所熟悉的那样.然后,您所要做的就是使用switch语句或if语句返回某个自定义视图,具体取决于传递给getView方法的位置参数.Android很聪明,它只会为你的位置/行提供适当类型的convertView; 你不需要检查它是否是正确的类型.您可以通过适当地覆盖getItemViewType()和getViewTypeCount()方法来帮助Android.
| 归档时间: |
|
| 查看次数: |
193055 次 |
| 最近记录: |