我在Android应用程序中工作,我有一个带有子标题的ListView和该子标题内的项目。我正在使用海关清单视图来实现这一点。现在,当下面的子标题滚动到ListView上方时,我想替换顶部子标题文本。 Android LinkedIn中也实现了相同的功能。
以下是我的Adapter类:
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
import com.evero.android.domain.Consumer;
/**
* @author Arun PS
*
*/
public class CaseManagerAdaptor extends BaseAdapter implements Filterable {
private ValueFilter valueFilter = null;
private ArrayList<Consumer> mData = new ArrayList<Consumer>();
private ArrayList<Consumer> mDatabackup = new ArrayList<Consumer>();
private LayoutInflater mInflater;
public CaseManagerAdaptor(Context context, ArrayList<Consumer> consumers) {
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mData = mDatabackup = consumers;
getFilter();
}
@Override
public int getCount() {
return mDatabackup.size();
}
@Override
public String getItem(int position) {
return mData.get(position).toString();
}
@Override
public long getItemId(int position) {
return position;
}
public static class ViewHolder {
public TextView textView1 = null;
public TextView textView2 = null;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
holder = new ViewHolder();
if (!mDatabackup.get(position).IsHeader) {
convertView = mInflater.inflate(R.layout.subhead_listitem, null);
holder.textView1 = (TextView) convertView
.findViewById(R.id.clientname_textView);
holder.textView2 = (TextView) convertView
.findViewById(R.id.serviceCompletedCount_textView);
} else {
convertView = mInflater.inflate(R.layout.head_list_item, null);
holder.textView1 = (TextView) convertView
.findViewById(R.id.head_list_Text);
}
if (mDatabackup.get(position).IsHeader)
holder.textView1.setText(mDatabackup.get(position).EmployeeName);
else {
convertView.setTag(mDatabackup.get(position));
holder.textView1.setText(mDatabackup.get(position).ClientName);
holder.textView2
.setText(mDatabackup.get(position).ServiceCompletedCount);
}
return convertView;
}
// Returns a filter that can be used to constrain data with a filtering
// pattern.
@Override
public Filter getFilter() {
if (valueFilter == null) {
valueFilter = new ValueFilter();
}
return valueFilter;
}
private Boolean isFiltered = null;
private class ValueFilter extends Filter {
// Invoked in a worker thread to filter the data according to the
// constraint.
@Override
protected FilterResults performFiltering(CharSequence constraint) {
isFiltered = false;
FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
ArrayList<Consumer> filterList = new ArrayList<Consumer>();
for (int i = 0; i < mData.size(); i++) {
if (!mData.get(i).IsHeader
&& mData.get(i).ClientName.startsWith(constraint
.toString())) {
filterList.add(mData.get(i));
}
}
results.count = filterList.size() + 1;
results.values = filterList;
isFiltered = true;
} else {
results.count = mData.size();
results.values = mData;
}
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
mDatabackup = (ArrayList<Consumer>) results.values;
if (isFiltered && mData.size() > mDatabackup.size()) {
Consumer consumer = new Consumer();
consumer.IsHeader = true;
consumer.EmployeeName = (mDatabackup.size() == 0 ? "No record found"
: "Individual");
consumer.ClientName = "";
mDatabackup.add(0, consumer);
}
notifyDataSetChanged();
}
}
}