CustomListView 中的向下滚动问题
Posted
技术标签:
【中文标题】CustomListView 中的向下滚动问题【英文标题】:Scrolldown issue in CustomListView 【发布时间】:2015-03-27 01:27:23 【问题描述】:我正在开发关于 CustomListView 的 android 应用程序,首先我从 web servis 解析数据,然后我将此数据添加到 ListView 但是当我下拉到 scrollView 时出现此错误:
at android.widget.ListView.makeAndAddView(ListView.java:1792)
at android.widget.ListView.fillDown(ListView.java:676)
at android.widget.ListView.fillGap(ListView.java:640)
at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4911)
at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:2923)
at android.widget.AbsListView.onTouchEvent(AbsListView.java:3234)
at android.view.View.dispatchTouchEvent(View.java:5553)
主活动:
public class MainActivity extends Activity implements OnRefreshListener
ListView list;
String[] web;
Integer[] imageId = R.drawable.enveloppe, R.drawable.enveloppe,
R.drawable.enveloppe, R.drawable.enveloppe, R.drawable.enveloppe,
R.drawable.enveloppe, R.drawable.enveloppe
;
SwipeRefreshLayout swipeLayout;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.list);
web = new String[13];
swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
swipeLayout.setOnRefreshListener(this);
swipeLayout.setColorScheme(android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
AsyncCallBanner banner = new AsyncCallBanner();
// Call execute
banner.execute();
list.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
Toast.makeText(MainActivity.this,
"You Clicked at " + web[+position], Toast.LENGTH_SHORT)
.show();
);
// TODO Auto-generated catch block
// TODO Auto-generated catch block
@Override
public void onRefresh()
new Handler().postDelayed(new Runnable()
@Override
public void run()
swipeLayout.setRefreshing(false);
, 5000);
// ////////// for Parsing data
private class AsyncCallBanner extends AsyncTask<Void, Void, String>
ProgressDialog dialog;
@Override
protected String doInBackground(Void... params)
Log.i("TAG", "doInBackground");
Connect();
return null;
@Override
protected void onPostExecute(String result)
Log.i("TAG", "onPostExecute");
// txt1.setText(Icerik);
CustomList adapter = new CustomList(MainActivity.this, web, imageId);
list.setAdapter(adapter);
dialog.dismiss();
@Override
protected void onPreExecute()
Log.i("TAG", "onPreExecute");
dialog = new ProgressDialog(MainActivity.this);
dialog.setMessage("Lütfen bekleyiniz");
dialog.setIndeterminate(true);
dialog.show();
private void Connect()
// TODO Auto-generated method stub
JSONObject returndata = null;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://api.androidhive.info/contacts/");
httppost.setHeader("Content-type", "application/json");
JSONObject jsonparameter = new JSONObject();
try
// jsonparameter.put("AnketID", "3");
httppost.setEntity(new StringEntity(jsonparameter.toString(),
"UTF-8"));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity);
Log.i("@banner_responseString", "" + responseString);
try
returndata = new JSONObject(responseString);
JSONArray jsonMainNode = returndata.optJSONArray("contacts");
int lengthJsonArr = jsonMainNode.length();
Log.i("@lengthJson", "" + lengthJsonArr);
int sorusayisi = lengthJsonArr;
for (int i = 0; i < sorusayisi; i++)
if (i == sorusayisi)
break;
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
try
web[i] = jsonChildNode.optString("name").toString(); // URL
catch (Exception e)
catch (JSONException e)
catch (Exception e)
自定义类:
public class CustomList extends ArrayAdapter<String>
private final Activity context;
private final String[] web;
private final Integer[] imageId;
public CustomList(Activity context, String[] web, Integer[] imageId)
super(context, R.layout.list_single, web);
this.context = context;
this.web = web;
this.imageId = imageId;
@Override
public View getView(int position, View view, ViewGroup parent)
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.list_single, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
txtTitle.setText(web[position]);
imageView.setImageResource(imageId[position]);
return rowView;
【问题讨论】:
你应该使用 getView() 方法返回的 view 来扩展你的项目布局。 我试过了,但又遇到了同样的错误。 public View getView(int position, View view, ViewGroup parent) LayoutInflater inflater = context.getLayoutInflater();视图 = inflater.inflate(R.layout.list_single, null, true); TextView txtTitle = (TextView) view.findViewById(R.id.txt); ImageView imageView = (ImageView) view.findViewById(R.id.img); txtTitle.setText(web[位置]); imageView.setImageResource(imageId[位置]);返回视图; view = inflater.inflate(R.layout.list_single, parent, false); @santalu 我试过了,但是没用。 【参考方案1】:请将您的 getView 函数更改为以下内容并对其进行测试并反馈给我
@Override
public View getView(int position, View view, ViewGroup parent)
TextView txtTitle=null;
ImageView imageView=null;
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (view== null)
view= mInflater.inflate(R.layout.list_single, null);
txtTitle = (TextView) view.findViewById(R.id.txt);
imageView = (ImageView) view.findViewById(R.id.img);
txtTitle.setText(web[position]);
imageView.setImageResource(imageId[position]);
return view;
【讨论】:
以上是关于CustomListView 中的向下滚动问题的主要内容,如果未能解决你的问题,请参考以下文章
jquery 让DIV相当于body定位,然后随滚动条向下滚动
UICollectionView 中的图像在向下滚动时会自动翻转?