让我们说这是我的代码, 我想根据某些条件将2个不同的视图扩展到listView。
public View getView(int position, View convertView, ViewGroup parent) {
if (condition) {
view = layoutInflater.inflate(R.layout.layout1, parent, false);
} else {
view = layoutInflater.inflate(R.layout.layout2, parent, false);
}
return view;
}
我想使用convertView来回收返回的视图,但是我的适配器将如何知道要回收的两种类型中的哪一种?
答案 0 :(得分:2)
答案:
您应该将此方法添加到适配器,这将使适配器将正确的转换视图提供给当前索引。 只要您保持与正确类型保持一致,返回的数字并不重要。
ggplot(data, aes(x=xval, y=yval_LWTW, fill=SNP)) +
scale_fill_manual(values=c(GG="blue",CC="red",NN="green")) +
geom_bar(stat="identity", width=1) +
theme(axis.title.x=element_blank()) +
coord_cartesian(ylim=c(50,90))
答案 1 :(得分:0)
您需要在Adapter
界面中实现另外两种方法:
/**
* <p>
* Returns the number of types of Views that will be created by
* {@link #getView}. Each type represents a set of views that can be
* converted in {@link #getView}. If the adapter always returns the same
* type of View for all items, this method should return 1.
* </p>
* <p>
* This method will only be called when when the adapter is set on the
* the {@link AdapterView}.
* </p>
*
* @return The number of types of Views that will be created by this adapter
*/
int getViewTypeCount();
/**
* Get the type of View that will be created by {@link #getView} for the specified item.
*
* @param position The position of the item within the adapter's data set whose view type we
* want.
* @return An integer representing the type of View. Two views should share the same type if one
* can be converted to the other in {@link #getView}. Note: Integers must be in the
* range 0 to {@link #getViewTypeCount} - 1. {@link #IGNORE_ITEM_VIEW_TYPE} can
* also be returned.
* @see #IGNORE_ITEM_VIEW_TYPE
*/
int getItemViewType(int position);
即。有两种不同类型的观点:
@Override
public int getItemViewType(int position) {
if(condition(position)){
return 0;
}
return 1;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(condition(position)){
//handle first type of view 0
} else {
//handle second type of view 1
}
}
答案 2 :(得分:0)
private class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, List<String> list) {
super(context, 0, list);
}
@Override
public int getItemViewType(int position) {
return position % 2;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
int type = getItemViewType(position);
if (type == 1){
view = layoutInflater.inflate(R.layout.layout1, parent, false);
} else {
view = layoutInflater.inflate(R.layout.layout2, parent, false);
}
return view;
}
}
答案 3 :(得分:0)
您可以解决以下问题:
将两个视图添加为相同布局的子项(item_example.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<View
android:id="@+id/v_first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone">
<View
android:id="@+id/v_second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone">
</LinearLayout>
并根据您所需的条件决定在运行时显示哪一个:
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View exampleView = convertView;
if (exampleView == null) {
LayoutInflater inflater = fragment.getActivity().getLayoutInflater();
exampleView = inflater.inflate(R.layout.item_example, parent, false);
}
View view1 = exampleView.findViewById(R.id.v_first);
View view2 = exampleView.findViewById(R.id.v_second);
if (/* ... show view1 ? ... */) {
view1.setVisibility(View.VISIBLE);
view2.setVisibility(View.GONE);
} else {
view2.setVisibility(View.VISIBLE);
view1.setVisibility(View.GONE);
}
return exampleView;
}