我想我有点知道答案(不能这样做),但是如果有人遇到过这样的话,我仍然喜欢听SO社区:< / p>
我有一个我希望在列表/回收站视图中显示的项目的ArrayList。所以我有一个适配器读取这个后备阵列。现在我想根据数组中项目的某些属性隐藏一些项目。如何设置适配器以跳过此阵列中的某些项目?
我尝试从持有者引用父视图,并设置其可见性。这使视图不可见,但列表仍显示空行。
答案 0 :(得分:1)
这样做,你需要覆盖适配器中的getItemViewType
你的Pojo
Hello.java
{
boolean needHide =false;
}
适配器:
int ITEM_TYPE_USER_HIDE =0;
int ITEM_TYPE_USER_UNHIDE=1;
@Override
public int getItemViewType(int position) {
return mList.get(position).needHide? ITEM_TYPE_USER_HIDE : ITEM_TYPE_UNHIDE;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == ITEM_TYPE_UNHIDE) {
View view = LayoutInflater.from(mContext).inflate(R.layout.layout_not_hide, parent, false);
return new NotHideHolder(view);
} else {
View view = LayoutInflater.from(mContext).inflate(R.layout.layout_hide_type, parent, false);
return new HideViewHolder(view);
}
}
答案 1 :(得分:0)
不幸的是,正如@Ben Trengrove建议的那样,设置View.GONE的可见性不适用于ListView,因为它仍然在列表中保留空行作为空行。
但是我正在研究一个项目并面临类似的问题,幸运的是我终于找到了解决方案。 我使用ListView和ArrayAdapter。我的策略是每当我们想要隐藏视图时,我们将返回一个视图,而不会将其从ArrayAdapter的getView()方法中膨胀。 这是我的ArrayAdapter的完整工作代码:
public class CustomArrayAdapter extends ArrayAdapter<String> {
private Context context;
private Typeface typeface;
private ArrayList<String> list;
private LayoutInflater layoutInflater;
public CustomArrayAdapter(@NonNull Context context, ArrayList<String> list) {
super(context, R.layout.simple_list_view_1, list);
this.context = context;
this.list = list;
typeface = Typeface.createFromAsset(context.getAssets(), "krdv010.ttf");
layoutInflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@NonNull
@Override
/*Every time ListView needs to show a new row on screen, it will call
this getView() method.*/
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
ViewHolder viewHolder = null;
//Hide some views here...
//Use position or whatever for the condition.
//Provide a new View and return it without inflating it.
//but still set a ViewHolder tag to it.
if (position ==0 || position == 6 || position ==26){
viewHolder = new ViewHolder();
viewHolder.isEmpty = true;
convertView = new View(context);
convertView.setTag(viewHolder);
return convertView;
}
//ListView is not asking you to recycle a view - normal workout
if(convertView == null){
convertView = layoutInflater.inflate(R.layout.simple_list_view_1, null);
viewHolder = new ViewHolder();
viewHolder.textView = (TextView)convertView.findViewById(R.id.text1);
viewHolder.isEmpty = false;
convertView.setTag(viewHolder);
}
//Here ListView is asking you to recycle the row layout.
//But there may be the one which was not inflated.
//So we will check for it and do what is required.
else{
viewHolder = (ViewHolder)convertView.getTag();
if(viewHolder.isEmpty){
//This is the one which wasn't inflated.
convertView = layoutInflater.inflate(R.layout.simple_list_view_1, null);
viewHolder.textView = (TextView)convertView.findViewById(R.id.text1);
viewHolder.isEmpty = false;
convertView.setTag(viewHolder);
}
}
//Your normal business here
String string = list.get(position);
viewHolder.textView.setText(string);
viewHolder.textView.setTypeface(typeface);
viewHolder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "Position: " + position, Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
//The View Holder Class
private static class ViewHolder{
TextView textView;
Boolean isEmpty;
}
}
这是屏幕截图,注意没有空行@ 0和@ 6位置它们被隐藏成功: