实际的问题是,我从服务器加载了一个网格,我在页面顶部有一个搜索字段,搜索后假设显示了一个列表,我的键盘出现在屏幕上,但是单击后退按钮时隐藏键盘整个视图(GridView)正在被清除。
这是代码
public class ViewClass extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.viewclass, container, false);
mylist = (GridView) v.findViewById(R.id.grid_view);
endlessScrollListener = new EndlessScrollListener();
mylist.setOnScrollListener(endlessScrollListener);
adapter = new CustomAdapter(getActivity());
// Instance of ImageAdapter Class
mylist.setAdapter(adapter);
searchflag = 0;
_data.clear();
index = 0;
dialogflag = 0;
callviewData(index, "");
search = (EditText) v.findViewById(R.id.search);
search.addTextChangedListener(new TextWatcher() {
private int length_pre;
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
System.out.println("count :" + s.length() + " length_pre:"
+ length_pre);
if (s.length() == 0 && length_pre == 1) {
endlessScrollListener.clear();
searchflag = 1;
index = 0;
dialogflag = 0;
callviewData(index, s.toString());
} else if (s.length() > 0) {
endlessScrollListener.clear();
searchflag = 1;
index = 0;
dialogflag = 0;
callviewData(index, s.toString());
}
length_pre = s.length();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
return v;
}
这是无尽的滚动监听器
公共类 EndlessScrollListener 实现 OnScrollListener {
private int visibleThreshold = 5;
private int currentPage = 0;
private int previousTotal = 0;
private boolean loading = true;
public EndlessScrollListener() {
}
public void clear() {
// TODO Auto-generated method stub
visibleThreshold = 5;
currentPage = 0;
previousTotal = 0;
loading = true;
}
public EndlessScrollListener(int visibleThreshold) {
this.visibleThreshold = visibleThreshold;
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (loading) {
System.out.println("httscrollllllloadingg");
if (totalItemCount > previousTotal) {
loading = false;
previousTotal = totalItemCount;
currentPage++;
}
System.out.println("htvvv:"
+ (totalItemCount - visibleItemCount) + "::"
+ (firstVisibleItem + visibleThreshold));
}
if (!loading
&& (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
// I load the next page of gigs using a background task,
// but you can call any function here.
System.out.println("httscrollllll");
index = index + 1;
appPref.SaveData(String.valueOf(index), "index");
dialogflag = 1;
callviewData(index, search.getText().toString());
loading = true;
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
}
这是自定义适配器
public class CustomAdapter extends BaseAdapter {
Context context;
private Filter planetFilter;
ArrayList<RowItem> rowItems;
public CustomAdapter(Context context) {
this.context = context;
rowItems = _data;
// this.rowItems = rowItems;
}
@Override
public int getCount() {
System.out.println("list data:" + _data.size());
return _data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
/* private view holder class */
private class ViewHolder {
ImageView pic;
TextView title;
TextView desc;
LinearLayout rl;
}
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.product_holder, null);
holder = new ViewHolder();
holder.rl = (LinearLayout) convertView.findViewById(R.id.rl);
holder.title = (TextView) convertView.findViewById(R.id.tName);
holder.pic = (ImageView) convertView.findViewById(R.id.iItem);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
// convertView.setTag(holder);
}
try {
imgj = _data.get(position).getPro_pic().toString();
if (imgj.equals("")) {
imgj = "no_photo.png";
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
imageLoader.displayImage(upurl + imgj.replace(" ", "%20"),
holder.pic, doption_two, animateFirstListener);
holder.title.setText(_data.get(position).getTitle());
System.out.println("list position pid:"
+ _data.get(position).getPid());
holder.rl.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
// int pos =
// Integer.parseInt(v.getTag().toString());
System.out.println("list position pid:"
+ _data.get(position).getPid());
appPref.SaveData(_data.get(position).getPid(),
"pdet_id");
// TODO Auto-generated method stub
search.setText("");
appPref.SaveData(String.valueOf(position),
"sel_pos");
addFragment.addFragment(new DetailView(), true,
FragmentTransaction.TRANSIT_NONE,
"detview", R.id.tabContent, getActivity());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return convertView;
}
} }
这是填充数据的异步任务
public void callviewData(final int ind, final String value) {
new AsyncTask<Void, Void, Void>() {
String result = "";
ProgressDialog progressDialog = null;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
if (searchflag == 1) {
_data.clear();
}
if (dialogflag == 0) {
progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("Loading , Please wait...");
progressDialog.setIndeterminate(true);
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
}
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
String url = "http://dummy.com/json/product_view.php?index="
+ ind + "&value=" + value;
System.out.println("url---" + url);
try {
JSONObject jsonObject = jpass.getJSONFromUrl(url);
result = jsonObject.getString("result");
if (result.equals("success")) {
JSONArray Details = jsonObject.getJSONArray("details");
for (int i = 0; i <= Details.length() - 1; i++) {
RowItem row = new RowItem();
JSONObject detail = Details.getJSONObject(i);
row.setTitle(detail.getString("title"));
row.setDesc(detail.getString("description"));
row.setPro_pic(detail.optString("image", "c"));
row.setPid(detail.getString("p_id"));
_data.add(row);
}
}
} catch (Exception e) {
dialogflag = 0;
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void arg) {
try {
// TODO Auto-generated method stub
System.out.println("exception post execute");
if (dialogflag == 0) {
progressDialog.dismiss();
}
if (result.equals("success")) {
System.out.println("sixe is " + _data.size());
appPref.SaveData(String.valueOf(_data.size()), "siz");
adapter.notifyDataSetChanged();
}
super.onPostExecute(arg);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.execute();
}