我想将动态文本视图添加到listview的水平线性布局中,我已成功实现了它。
我有listview搜索功能,当我过滤我创建的所有动态文本视图都附加到列表视图的现有线性布局
下面是我的listview行xml:
<LinearLayout
<!-- All the other widgets -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/business_header"
android:id="@+id/predicate_layout"
android:orientation="horizontal"/>
</LinearLayout
这是与之关联的适配器代码:
public class MarketPlaceAdapter extends BaseAdapter implements Filterable {
private Context context;
private List<Result> businesses;
private List<Result> orig;
//obtain the context and list of business data
public MarketPlaceAdapter(Context context)
{
this.context = context;
businesses = new ArrayList<>();
}
public void updateMarketPlaceList(List<Result> newList) {
businesses.addAll(newList);
this.notifyDataSetChanged();
}
@Override
public int getCount() {
return businesses.size();
}
@Override
public Result getItem(int position) {
return businesses.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
BusinessesViewHolder businessesViewHolder;
//Implementing view holder pattern for smooth listing of businesses in list
//initialize views once and store them in class
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.list_row_marketplace, null);
businessesViewHolder = new BusinessesViewHolder();
businessesViewHolder.businessImage = (ImageView) convertView.findViewById(R.id.business_image);
businessesViewHolder.businessTitle = (TextView) convertView.findViewById(R.id.business_title);
businessesViewHolder.businessCategory = (TextView) convertView.findViewById(R.id.business_category);
businessesViewHolder.linearLayout = (LinearLayout) convertView.findViewById(R.id.predicate_layout);
convertView.setTag(businessesViewHolder);
}
//reuse views from view holder if already initialised
else {
businessesViewHolder = (BusinessesViewHolder) convertView.getTag();
}
Result result = businesses.get(position);
//obtain data object from Array list passed and set the corresponding values to widgets
//to create list row
if(result != null)
{
String businessName = result.getProperties().getName();
if(result.getProperties().getThumbnail()!=null){
Picasso.with(context).load(result.getProperties().getThumbnail()).
placeholder(R.drawable.intro_business).into(businessesViewHolder.businessImage);
}
else{
businessesViewHolder.businessImage.setImageDrawable(MediaUtils.getTextIcon(businessName));
}
businessesViewHolder.businessTitle.setText(businessName);
businessesViewHolder.businessCategory.setText(result.getProperties().getDomain());
List<String> partnerTypes = result.getProperties().getPartner_types();
if(!partnerTypes.isEmpty()) partnerTypes.add(0,"Need:");
boolean isFirstPosition = false;
for(String partnerType: partnerTypes){
TextView partnerTypeTextView = new TextView(context);
partnerTypeTextView.setText(partnerType);
float pixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 6, context.getResources().getDisplayMetrics());
partnerTypeTextView.setTextSize(pixels);
if(isFirstPosition){
partnerTypeTextView.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.rounded_text_view));
}
isFirstPosition = true;
partnerTypeTextView.setSingleLine(true);
businessesViewHolder.linearLayout.addView(partnerTypeTextView);
}
}
return convertView;
}
//filters the adapter with the business names based on the search query given in search widget
//at Market place fragment action bar
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
final FilterResults filterResults = new FilterResults();
final List<Result> intermediateResults = new ArrayList<Result>();
if(orig == null)
orig = businesses;
if(constraint != null)
{
if(orig!=null && orig.size() > 0){
for (final Result g : orig) {
if (g.getProperties().getName().toLowerCase(Locale.ENGLISH).contains(constraint.toString()))
intermediateResults.add(g);
}
}
filterResults.values = intermediateResults;
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
businesses = (ArrayList<Result>) results.values;
notifyDataSetChanged();
}
};
}
class BusinessesViewHolder {
ImageView businessImage;
TextView businessTitle;
TextView businessCategory;
LinearLayout linearLayout;
}
}
我的问题是我在过滤的行中获取其他列表行动态文本视图。
请帮我解决这个问题。
答案 0 :(得分:0)
因为您使用的是ViewHolder模式,所以当您致电时,所有动态添加的Views
都会被回收:
businessesViewHolder = (BusinessesViewHolder) convertView.getTag();
我会跟着这条线:
businessesViewHolder.linearLayout.removeAllViews();
这应该清除每个TextViews
项中LinearLayout
中动态添加的ListView
。