我正在使用列表视图请求android的权限。我创建了一个自定义适配器。问题在于,在列表视图中,只有适配器数组的第一个元素是可见的(TextView),其余项是不可见的。
下面是代码参考:
自定义适配器:
private class ListViewAdapter extends ArrayAdapter<String>{
private int layout;
private ListViewAdapter(Context context, int resource, List<String> objects){
super(context,resource,objects);
layout = resource;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
ViewHolder mainViewHolder = null;
if(convertView == null){
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(layout,parent,false);
ViewHolder holder = new ViewHolder();
holder.textView = (TextView)convertView.findViewById(R.id.permision_list_view_text);
holder.switchCompat = (SwitchCompat)convertView.findViewById(R.id.permision_list_view_switch);
convertView.setTag(holder);
}else{
mainViewHolder = (ViewHolder)convertView.getTag();
mainViewHolder.textView.setText(getItem(position));
}
return convertView;
}
}
初始化列表视图:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_permission_request_dialog, container, false);
ListView lv = (ListView)view.findViewById(R.id.permission_request_list_view);
getPermissionArray();
Log.d("permissions",permissions.toString());
lv.setAdapter(new ListViewAdapter(getContext(),R.layout.permission_dialog_listview_row,permissions));
return view;
}
阵列适配器:
public void getPermissionArray(){
permissions.add("Request 1");
permissions.add("Request 2");
permissions.add("Request 3");
}
列表视图行项目布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:paddingHorizontal="20dp"
android:paddingVertical="10dp">
<TextView
android:id="@+id/permision_list_view_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-condensed"
android:textColor="@color/colorBlack" />
<android.support.v7.widget.SwitchCompat
android:id="@+id/permision_list_view_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
</RelativeLayout>
我不明白为什么列表视图不完整。
下面是我的移动设备(Motorola G3)上问题的屏幕快照
任何帮助将不胜感激。
答案 0 :(得分:2)
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View rowView = convertView;
if(rowView == null){
LayoutInflater inflater = LayoutInflater.from(getContext());
rowView = inflater.inflate(layout,parent,false);
ViewHolder holder = new ViewHolder();
holder.textView = (TextView)rowView.findViewById(R.id.permision_list_view_text);
holder.switchCompat = (SwitchCompat)rowView.findViewById(R.id.permision_list_view_switch);
rowView.setTag(holder);
}
ViewHolder holder = (ViewHolder) rowView.getTag();
// Set your permission data here using holder
return rowView;
}
答案 1 :(得分:2)
您将列表项的文本设置在错误的位置:
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
ViewHolder mainViewHolder = null;
if(convertView == null){
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(layout,parent,false);
ViewHolder holder = new ViewHolder();
holder.textView = (TextView)convertView.findViewById(R.id.permision_list_view_text);
holder.switchCompat = (SwitchCompat)convertView.findViewById(R.id.permision_list_view_switch);
convertView.setTag(holder);
}else{
mainViewHolder = (ViewHolder)convertView.getTag();
//mainViewHolder.textView.setText(getItem(position)); // not here. This else section is reached only when the rows are getting recycled.
}
mainViewHolder.textView.setText(getItem(position)); // here
return convertView;
}
}